From b11bb5c38595d1cb578c500a74c093ef8d8aa865 Mon Sep 17 00:00:00 2001 From: Solly Ross Date: Mon, 22 Sep 2014 21:42:32 -0400 Subject: [PATCH] Add support for Relative Paths in the Test Runner This patch adds support for using relative paths with the '-r' or '--relative' methods. This can be useful if you want to output HTML (with the '--output-html' option) and use it in a webpage. Additionally, the '-o' was removed from the documentation of '--output-html', since it hasn't worked for that in a while ('-o' means open in browser instead). --- tests/run_from_console.js | 62 +++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/tests/run_from_console.js b/tests/run_from_console.js index bfdd1b69..2a5bb70b 100755 --- a/tests/run_from_console.js +++ b/tests/run_from_console.js @@ -17,8 +17,9 @@ program .option('-p, --provider ', '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 .js tests).') .option('-o, --open-in-browser', 'Open the generated HTML files in a web browser using the "open" module (must be used with the "-g"/"--generate-html" option).') - .option('-o, --output-html', 'Instead of running the tests, just output the generated HTML source to STDOUT (should be used with .js tests)') + .option('--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') + .option('-r, --relative', 'Use relative paths in the generated HTML file') .parse(process.argv); if (program.tests.length === 0) { @@ -31,6 +32,27 @@ var file_paths = []; var all_js = program.tests.reduce(function(a,e) { return a && e.slice(-3) == '.js'; }, true); +var get_path = function (/* arguments */) { + if (program.relative) { + return path.join.apply(null, arguments); + } else { + var args = Array.prototype.slice.call(arguments); + args.unshift(__dirname, '..'); + return path.resolve.apply(null, args); + } +}; + +var get_path_cwd = function (/* arguments */) { + if (program.relative) { + var part_path = path.join.apply(null, arguments); + return path.relative(path.join(__dirname, '..'), path.resolve(process.cwd(), part_path)); + } else { + var args = Array.prototype.slice.call(arguments); + args.unshift(process.cwd()); + return path.resolve.apply(null, args); + } +}; + if (all_js && !program.autoInject) { var all_modules = {}; @@ -44,7 +66,7 @@ if (all_js && !program.autoInject) { 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; + all_modules[get_path('include/', mod) + '.js'] = 1; }); } @@ -54,7 +76,7 @@ if (all_js && !program.autoInject) { var fakes_eol = content.indexOf('\n', fakes_ind); var fakes_modules = content.slice(fakes_ind, fakes_eol).split(/,\s*/); fakes_modules.forEach(function (mod) { - all_modules[path.resolve(__dirname, mod) + '.js'] = 1; + all_modules[get_path('tests/', mod) + '.js'] = 1; }); } }); @@ -67,27 +89,27 @@ if (program.autoInject) { temp.track(); var template = { - header: "\n\n\n\n\n
", + header: "\n\n\n\n\n
", script_tag: function(p) { return ""; }, footer: "\n\n" }; - template.header += "\n" + template.script_tag(path.resolve(__dirname, '../node_modules/chai/chai.js')); - template.header += "\n" + template.script_tag(path.resolve(__dirname, '../node_modules/mocha/mocha.js')); - template.header += "\n" + template.script_tag(path.resolve(__dirname, '../node_modules/sinon/pkg/sinon.js')); - template.header += "\n" + template.script_tag(path.resolve(__dirname, '../node_modules/sinon-chai/lib/sinon-chai.js')); - template.header += "\n" + template.script_tag(path.resolve(__dirname, '../node_modules/sinon-chai/lib/sinon-chai.js')); + template.header += "\n" + template.script_tag(get_path('node_modules/chai/chai.js')); + template.header += "\n" + template.script_tag(get_path('node_modules/mocha/mocha.js')); + template.header += "\n" + template.script_tag(get_path('node_modules/sinon/pkg/sinon.js')); + template.header += "\n" + template.script_tag(get_path('node_modules/sinon-chai/lib/sinon-chai.js')); + template.header += "\n" + template.script_tag(get_path('node_modules/sinon-chai/lib/sinon-chai.js')); template.header += "\n"; template.header = program.autoInject.reduce(function(acc, sn) { - return acc + "\n" + template.script_tag(path.resolve(process.cwd(), sn)); + return acc + "\n" + template.script_tag(get_path_cwd(sn)); }, template.header); file_paths = program.tests.map(function(jsn, ind) { var templ = template.header; templ += "\n"; - templ += template.script_tag(path.resolve(process.cwd(), jsn)); + templ += template.script_tag(get_path_cwd(jsn)); templ += template.footer; var tempfile = temp.openSync({ prefix: 'novnc-zombie-inject-', suffix: '-file_num-'+ind+'.html' }); @@ -118,15 +140,19 @@ if (program.outputHtml) { return; } + if (use_ansi) { + cursor + .bold() + .write(program.tests[path_ind]) + .reset() + .write("\n") + .write(Array(program.tests[path_ind].length+1).join('=')) + .write("\n\n"); + } + cursor - .bold() - .write(program.tests[path_ind]) - .reset() - .write("\n") - .write(Array(program.tests[path_ind].length+1).join('=')) - .write("\n\n") .write(data) - .write("\n"); + .write("\n\n"); }); }); }