Skip to content

Closes Codeception/CodeceptJS#9 , add the test folder to the codecept.json object #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
coverage
.vscode
coverage
site
site
.idea
22 changes: 11 additions & 11 deletions lib/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

let Mocha = require('mocha/lib/mocha');
let fsPath = require('path');
let readFileSync = require('fs').readFileSync;
let readFileSync = require('fs').readFileSync;
let readdirSync = require('fs').readdirSync;
let statSync = require('fs').statSync;
let container = require('./container');
Expand All @@ -14,29 +14,29 @@ const scenarioUi = fsPath.join(__dirname, './interfaces/codeceptjs.js');
class Codecept {

constructor(config, opts) {
this.opts = opts;
this.config = config;
this.opts = opts;
this.config = config;
this.mocha = new Mocha(Object.assign(config.mocha, opts));
this.mocha.ui(scenarioUi);
this.mocha.reporter(reporter, opts);
this.testFiles = [];
this.mocha.reporter(reporter, opts);
this.testFiles = [];
}

init(dir) {
global.codecept_dir = dir;
container.create(this.config);
container.create(this.config);
require('./listener/steps');
require('./listener/helpers');
require('./listener/helpers');
}

loadTests(pattern) {
pattern = pattern || this.config.tests;
glob.sync(fsPath.join(codecept_dir, pattern)).forEach((file) => {
this.testFiles.push(fsPath.resolve(file));
});
}

run(test) {
run(test) {
this.mocha.files = this.testFiles;
if (test) {
this.mocha.files = this.mocha.files.filter((t) => fsPath.basename(t, '_test.js') == test || fsPath.basename(t, '.js') == test || fsPath.basename(t) == test);
Expand All @@ -45,7 +45,7 @@ class Codecept {
event.dispatcher.emit(event.all.result, this);
});
}

static version() {
return JSON.parse(readFileSync(__dirname + '/../package.json', 'utf8')).version;
}
Expand Down
97 changes: 48 additions & 49 deletions lib/command/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,62 @@ let fs = require('fs');
let path = require('path');
let fileExists = require('../utils').fileExists;
let inquirer = require("inquirer");
let getTestRoot = require("./utils").getTestRoot;
let getTestRoot = require("./utils").getTestRoot;

let defaultConfig = {
"tests": "./*_test.js",
"timeout": 10000,
"tests": "./*_test.js",
"timeout": 10000,
"output": null,
"helpers": {
},
"include": {
},
"mocha": {
}
}
"helpers": {},
"include": {},
"mocha": {}
};

let defaultActor = `
'use strict';
// in this file you can append custom step methods to 'I' object

module.exports = function() {
return require('codeceptjs/actor')({

// Define custom steps here, use 'this' to access default methods of I.
// It is recommended to place a general 'login' function here.
// It is recommended to place a general 'login' function here.

});
}


`;

module.exports = function(initPath) {
module.exports = function (initPath) {
let testsPath = getTestRoot(initPath);

print();
print(` Welcome to ${colors.magenta.bold('CodeceptJS')} initialization tool`);
print(" It will prepare and configure a test environment for you");
print();


if (!path) {
print(`No test root specified.`);
print(`Test root is assumed to be ${colors.yellow.bold(testsPath)}`);
print('----------------------------------');
} else {
print(`Installing to ${colors.bold(testsPath)}`);
}
}

if (!fileExists(testsPath)) {
print(`Directory ${testsPath} does not exist, creating...`);
fs.mkdirSync(testsPath);
}
}

let configFile = path.join(testsPath, 'codecept.json');
if (fileExists(configFile)) {
error(`Config is already created at ${configFile}`);
return;
}
}



inquirer.prompt(
[
{
Expand All @@ -84,7 +81,7 @@ module.exports = function(initPath) {
name: "output",
default: "./output",
message: "Where should logs, screenshots, and reports to be stored?"
},
},
{
name: "steps",
type: "confirm",
Expand All @@ -96,67 +93,69 @@ module.exports = function(initPath) {
type: "input",
message: "Where would you like to place custom steps?",
default: "./steps_file.js",
when: function(answers) {
when: function (answers) {
return answers.steps;
}
}
], (result) => {
let config = defaultConfig;
config.name = testsPath.split(path.sep).pop();
config.output = result.output;
config.tests = result.tests;
result.helpers.forEach((helper) => config.helpers[helper] = {});
if (result.steps_file) config.include.I = result.steps_file;
if (result.steps_file) config.include.I = result.steps_file;

let helperConfigs = [];

result.helpers.forEach((helperName) => {
try {
let Helper = require('../helper/'+helperName);
if (!Helper._config) return;
try {
let Helper = require('../helper/' + helperName);
if (!Helper._config) return;
helperConfigs = helperConfigs.concat(Helper._config().map((config) => {
config.message = `[${helperName}] ${config.message}`;
config.name = `${helperName}_${config.name}`;
config.type = config.type || 'input';
return config;
}));
return config;
}));
} catch (err) {
error(err);
}
});

if (result.helpers.length) {
if (result.helpers.length) {
print("Configure helpers...");
}

inquirer.prompt(helperConfigs, (helperResult) => {

Object.keys(helperResult).forEach((key) => {
let helperName, configName;
let parts = key.split('_');
helperName = parts[0];
configName = parts[1];
configName = parts[1];
if (!configName) return;
config.helpers[helperName][configName] = helperResult[key];
})
config.helpers[helperName][configName] = helperResult[key];
});

if (result.steps_file) {
let stepFile = path.join(testsPath, result.steps_file);
if (!fileExists(path.dirname(stepFile))) fs.mkdirSync(path.dirname(stepFile));
if (!fileExists(path.dirname(stepFile))) fs.mkdirSync(path.dirname(stepFile));
fs.writeFileSync(stepFile, defaultActor);
config.include.I = result.steps_file;
success('Steps file created at '+stepFile);
success('Steps file created at ' + stepFile);
}

fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
success(`Config created at ${configFile}`);


if (config.output) {
success(`Config created at ${configFile}`);


if (config.output) {

fs.mkdirSync(path.join(testsPath, config.output));
success("Directory for temporaty output files created at `_output`");
success("Directory for temporaty output files created at `_output`");
}

success("Almost done! Create your first test by executing `codeceptjs gt` (generate test) command");
});
});
}
});
});
};
34 changes: 17 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ var fs = require('fs');
module.exports.fileExists = function (filePath) {
try {
fs.statSync(filePath);
} catch(err) {
} catch (err) {
if (err.code == 'ENOENT') return false;
}
return true;
};

module.exports.getParamNames = function(fn) {
module.exports.getParamNames = function (fn) {
if (fn.isSinonProxy) return [];
var funStr = fn.toString();
return funStr.slice(funStr.indexOf('(') + 1, funStr.indexOf(')')).match(/([^\s,]+)/g);
}
};

module.exports.methodsOfObject = function (obj, className) {
var methods = [];

const standard = [
'constructor',
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable'
]
'propertyIsEnumerable'
];

while (obj.constructor.name != className) {
while (obj.constructor.name != className) {
Object.getOwnPropertyNames(obj).forEach((prop) => {
if (typeof(obj[prop]) != 'function') return;
if (standard.indexOf(prop) >= 0) return;
Expand All @@ -40,22 +40,22 @@ module.exports.methodsOfObject = function (obj, className) {
if (!obj || !obj.constructor) break;
}
return methods;
}
};

module.exports.template = function (template, data) {
return template.replace(/{{([^{}]*)}}/g, function (a, b) {
var r = data[b];
return typeof r === 'string' ? r : a;
});
return template.replace(/{{([^{}]*)}}/g, function (a, b) {
var r = data[b];
return typeof r === 'string' ? r : a;
});
};

module.exports.ucfirst = function(str) {
module.exports.ucfirst = function (str) {
return str.charAt(0).toUpperCase() + str.substr(1);
}
};

module.exports.lcfirst = function(str) {
module.exports.lcfirst = function (str) {
return str.charAt(0).toLowerCase() + str.substr(1);
}
};

module.exports.xpathLocator = {
literal: (string) => {
Expand All @@ -71,4 +71,4 @@ module.exports.xpathLocator = {
combine: (locators) => {
return locators.join(' | ');
}
}
};