Skip to content

Commit

Permalink
Support 'requires' Line in Test Runner
Browse files Browse the repository at this point in the history
If the files passed to the '-t' option are all '.js' files (or
the 'run all tests' option is used) and the '-i' option is not
passed, all tests will be search for the string
'require local modules: '.  Only the first instance of this string
will be used.  Following the colon should be a list of either local
modules (i.e. files in the '../include/' folder relative to the
test runner's directory, without the '.js' extension) or paths
to other Javascript files.  The list of modules and/or files should
be comma-separated.  These files will then be included in the generated
HTML file for the appropriate tests as if the '-i' option had been used.
  • Loading branch information
DirectXMan12 committed Dec 17, 2013
1 parent d823e89 commit 85e8991
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions tests/run_from_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ program
.option('-c, --color', 'Explicitly enable color (default is to use color when not outputting to a pipe)')
.option('-i, --auto-inject <includefiles>', 'Treat the test list as a set of mocha JS files, and automatically generate HTML files with which to test test. \'includefiles\' should be a comma-separated list of paths to javascript files to include in each of the generated HTML files', make_list, null)
.option('-p, --provider <name>', 'Use the given provider (defaults to "casper"). Currently, may be "casper" or "zombie"', 'casper')
.option('-g, --generate-html', 'Instead of running the tests, just return the path to the generated HTML file, then wait for user interaction to exit (should be used with -i)')
.option('-o, --output-html', 'Instead of running the tests, just output the generated HTML source to STDOUT (should be used with -i)')
.option('-g, --generate-html', 'Instead of running the tests, just return the path to the generated HTML file, then wait for user interaction to exit (should be used with .js tests)')
.option('-o, --output-html', 'Instead of running the tests, just output the generated HTML source to STDOUT (should be used with .js tests)')
.option('-d, --debug', 'Show debug output (the "console" event) from the provider')
.parse(process.argv);

Expand All @@ -27,6 +27,29 @@ if (program.tests.length === 0) {

var file_paths = [];

var all_js = program.tests.reduce(function(a,e) { return a && e.slice(-3) == '.js'; }, true);

if (all_js && !program.autoInject) {
var all_modules = {};

// uses the first instance of the string 'requires local modules: '
program.tests.forEach(function (testname) {
var full_path = path.resolve(process.cwd(), testname);
var content = fs.readFileSync(full_path).toString();
var ind = content.indexOf('requires local modules: ');
if (ind > -1) {
ind += 'requires local modules: '.length;
var eol = content.indexOf('\n', ind);
var modules = content.slice(ind, eol).split(/,\s*/);
modules.forEach(function (mod) {
all_modules[path.resolve(__dirname, '../include/', mod)+'.js'] = 1;
});
}
});

program.autoInject = Object.keys(all_modules);
}

if (program.autoInject) {
var temp = require('temp');
temp.track();
Expand Down

0 comments on commit 85e8991

Please sign in to comment.