Permalink
Browse files

Add support for --chdir argument.

  • Loading branch information...
1 parent 3947a9b commit 4fa18a07d1042b8abe4d1d88378bb0128df08ca0 @Kami Kami committed Mar 26, 2011
Showing with 88 additions and 80 deletions.
  1. +1 −0 README.md
  2. +1 −0 lib/constants.js
  3. +14 −5 lib/run.js
  4. +63 −75 lib/run_test_file.js
  5. +9 −0 lib/util.js
View
@@ -43,6 +43,7 @@ Changes
* Add support for a global initialization file / function (`init` function in
this file is run before all the tests in a main process and can perform
some kind of global initialization)
+ * Add support for `--chdir` argument
* 25.03.2011 - v0.1.0
* Initial release (refactor module out from Cast and move it into a separate
View
@@ -59,6 +59,7 @@ var DEFAULT_OPTIONS = [
var OPTIONS = [
['-t', '--tests STRING', 'Whitespace separated list of tests to run'],
['-i', '--init-file STRING', 'An initialization file which is run before all the tests'],
+ ['-c', '--chdir STRING', 'Directory to which each test process chdirs before running the tests'],
['-v', '--verbosity [NUMBER]', 'Test runner verbosity'],
['-f', '--failfast', 'Exit after the first failure'],
['', '--timeout [NUMBER]', 'How long to wait (ms) for a test file to complete before timing out']
View
@@ -41,17 +41,19 @@ var dateStart;
* Spawn a new child, run a test file and report results.
*
* @param {String} cwd Full Directory where the whiskey binary was ran.
+ * @param {String} chdir A directory to which the test runner chdirs before
+ * running the tests.
* @param {String} filePath Full Absolute path to the test file.
* @param {Boolean} failfast True to exit after first failure.
* @param {Number} verbosity Test runner verbosity (0 = quiet, 3 = very verbose).
* @param {Function} callback Callback which is called on completion.
*/
-var executeTest = function(cwd, filePath, failfast, verbosity, callback) {
+var executeTest = function(cwd, chdir, filePath, failfast, verbosity, callback) {
var fileName = path.basename(filePath);
testUtil.printMsg(sprintf('Running test file: %s', fileName), verbosity, 3);
- var args = ['run_test_file.js', filePath, cwd];
+ var args = ['run_test_file.js', filePath, cwd, chdir];
var child = spawn(process.execPath, args);
var stderr = [];
var stdout = [];
@@ -105,7 +107,7 @@ var executeTest = function(cwd, filePath, failfast, verbosity, callback) {
}, timeout);
};
-var runTests = function(cwd, initFile, tests, failFast, verbosity) {
+var runTests = function(cwd, initFile, chdir, tests, failFast, verbosity) {
dateStart = testUtil.getUnixTimestamp();
// Require initialization and run initialization function
@@ -118,7 +120,7 @@ var runTests = function(cwd, initFile, tests, failFast, verbosity) {
}
async.forEachSeries(tests, function(test, callback) {
- executeTest(cwd, test, failFast, verbosity, callback);
+ executeTest(cwd, chdir, test, failFast, verbosity, callback);
},
function(err) {
@@ -131,6 +133,7 @@ process.addListener('SIGINT', function() {
});
var initFile = null;
+var chdir = null;
var failFast = false;
var verbosity = constants.DEFAULT_VERBOSITY;
var timeout = constants.DEFAULT_TEST_TIMEOUT;
@@ -150,6 +153,12 @@ var run = function(cwd, argv) {
}
});
+ p.on('chdir', function(opt, value) {
+ if (value) {
+ chdir = value;
+ }
+ });
+
p.on('failfast', function(opt, value) {
failFast = true;
});
@@ -178,7 +187,7 @@ var run = function(cwd, argv) {
p.parse(process.argv);
if (tests.length > 0) {
- runTests(cwd, initFile, tests, failFast, verbosity);
+ runTests(cwd, initFile, chdir, tests, failFast, verbosity);
}
else {
sys.puts(p.banner);
View
@@ -17,8 +17,6 @@
var sys = require('sys');
-var async = require('async');
-
var common = require('./common');
var constants = require('./constants');
var testUtil = require('./util');
@@ -27,102 +25,92 @@ var assert = require('./assert');
/**
* Execute the test file after the event loop starts.
*
- * @param {Function} initFunction Function which is called before each test file
- * is run.
*/
-process.nextTick(function(initFunction) {
- if (!initFunction) {
- initFunction = function(callback) {
- callback(null);
- }
- }
-
+process.nextTick(function() {
if (process.argv.length < 3) {
sys.puts('No test file specified');
process.exit(1);
}
- async.series([
- async.apply(initFunction)
- ],
+ var paths_to_require = [];
+ var testPath = process.argv[2];
+ var cwd = (process.argv.length >= 4) ? process.argv[3] : null;
+ var chdir = (process.argv.length === 5) ? process.argv[4] : null;
+ var testModule = testPath.replace(/\.js$/, '');
- function(err) {
- if (err) {
- throw new Error('Error during test configuration');
+ testUtil.addToRequirePaths(testPath, cwd);
+
+ var failed = {};
+ var succeeded = {};
+ var exportedFunctions;
+
+ if (!testUtil.isNull(chdir)) {
+ try {
+ process.chdir(chdir);
}
+ catch (err) {}
+ }
+
+ try {
+ exportedFunctions = require(testModule);
+ }
+ catch (err) {
+ failed['file_does_not_exist'] = err;
+ common.printChildResults(succeeded, failed);
+ return;
+ }
- var paths_to_require = [];
- var testPath = process.argv[2];
- var cwd = (process.argv.length === 4) ? process.argv[3] : null;
- var testModule = testPath.replace(/\.js$/, '');
+ var exportedFunctionsNames = Object.keys(exportedFunctions);
+ var functionsToRun;
+ var setUpFunctionIndex, tearDownFunctionIndex;
+ var testFunction, testFunctionName;
- testUtil.addToRequirePaths(testPath, cwd);
+ functionsToRun = exportedFunctionsNames.filter(common.isValidTestFunctionName);
+ functionsToRunLen = functionsToRun.length;
+ setUpFunctionIndex = exportedFunctionsNames.indexOf(constants.SETUP_FUNCTION_NAME);
+ tearDownFunctionIndex = exportedFunctionsNames.indexOf(constants.TEARDOWN_FUNCTION_NAME);
- var failed = {};
- var succeeded = {};
- var exportedFunctions;
+ // Call setUp function (if present)
+ if (setUpFunctionIndex !== -1) {
+ testFunction = exportedFunctions[constants.SETUP_FUNCTION_NAME];
try {
- exportedFunctions = require(testModule);
+ testFunction();
}
- catch (err) {
- failed['file_does_not_exist'] = err;
+ catch(err) {
+ failed[constants.SETUP_FUNCTION_NAME] = err;
common.printChildResults(succeeded, failed);
return;
}
+ }
- var exportedFunctionsNames = Object.keys(exportedFunctions);
- var functionsToRun;
- var setUpFunctionIndex, tearDownFunctionIndex;
- var testFunction, testFunctionName;
-
- functionsToRun = exportedFunctionsNames.filter(common.isValidTestFunctionName);
- functionsToRunLen = functionsToRun.length;
- setUpFunctionIndex = exportedFunctionsNames.indexOf(constants.SETUP_FUNCTION_NAME);
- tearDownFunctionIndex = exportedFunctionsNames.indexOf(constants.TEARDOWN_FUNCTION_NAME);
-
- // Call setUp function (if present)
- if (setUpFunctionIndex !== -1) {
- testFunction = exportedFunctions[constants.SETUP_FUNCTION_NAME];
-
- try {
- testFunction();
- }
- catch(err) {
- failed[constants.SETUP_FUNCTION_NAME] = err;
- common.printChildResults(succeeded, failed);
- return;
- }
- }
-
- for (var i = 0; i < functionsToRunLen; i++) {
- testFunctionName = functionsToRun[i];
-
- testFunction = exportedFunctions[testFunctionName];
+ for (var i = 0; i < functionsToRunLen; i++) {
+ testFunctionName = functionsToRun[i];
- try {
- testFunction();
- }
- catch (err) {
- failed[testFunctionName] = err;
- continue;
- }
+ testFunction = exportedFunctions[testFunctionName];
- succeeded[testFunctionName] = true;
+ try {
+ testFunction();
+ }
+ catch (err) {
+ failed[testFunctionName] = err;
+ continue;
}
- // Call tearDown function (if present)
- if (tearDownFunctionIndex !== -1) {
- testFunction = exportedFunctions[constants.TEARDOWN_FUNCTION_NAME];
+ succeeded[testFunctionName] = true;
+ }
+
+ // Call tearDown function (if present)
+ if (tearDownFunctionIndex !== -1) {
+ testFunction = exportedFunctions[constants.TEARDOWN_FUNCTION_NAME];
- try {
- testFunction();
- }
- catch(err) {
- failed[constants.TEARDOWN_FUNCTION_NAME] = err;
- }
+ try {
+ testFunction();
}
+ catch(err) {
+ failed[constants.TEARDOWN_FUNCTION_NAME] = err;
+ }
+ }
- common.printChildResults(succeeded, failed);
- });
+ common.printChildResults(succeeded, failed);
});
View
@@ -28,6 +28,14 @@ var printMsg = function(msg, verbosity, minVerbosity) {
}
};
+var isNull = function(value) {
+ if (value === null || (typeof value == 'string') && value === 'null') {
+ return true;
+ }
+
+ return false;
+};
+
/**
* Add a path to the require.paths (if not already there)
*
@@ -77,6 +85,7 @@ var addCharacters = function(string, width, character) {
};
exports.printMsg = printMsg;
+exports.isNull = isNull;
exports.addToRequirePaths = addToRequirePaths;
exports.getUnixTimestamp = getUnixTimestamp;
exports.addCharacters = addCharacters;

0 comments on commit 4fa18a0

Please sign in to comment.