Commit b11bb5c3 authored by Solly Ross's avatar Solly Ross

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).
parent 0b0b0433
...@@ -17,8 +17,9 @@ program ...@@ -17,8 +17,9 @@ program
.option('-p, --provider <name>', 'Use the given provider (defaults to "casper"). Currently, may be "casper" or "zombie"', 'casper') .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 .js tests).') .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, --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('-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); .parse(process.argv);
if (program.tests.length === 0) { if (program.tests.length === 0) {
...@@ -31,6 +32,27 @@ var file_paths = []; ...@@ -31,6 +32,27 @@ var file_paths = [];
var all_js = program.tests.reduce(function(a,e) { return a && e.slice(-3) == '.js'; }, true); 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) { if (all_js && !program.autoInject) {
var all_modules = {}; var all_modules = {};
...@@ -44,7 +66,7 @@ if (all_js && !program.autoInject) { ...@@ -44,7 +66,7 @@ if (all_js && !program.autoInject) {
var eol = content.indexOf('\n', ind); var eol = content.indexOf('\n', ind);
var modules = content.slice(ind, eol).split(/,\s*/); var modules = content.slice(ind, eol).split(/,\s*/);
modules.forEach(function (mod) { 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) { ...@@ -54,7 +76,7 @@ if (all_js && !program.autoInject) {
var fakes_eol = content.indexOf('\n', fakes_ind); var fakes_eol = content.indexOf('\n', fakes_ind);
var fakes_modules = content.slice(fakes_ind, fakes_eol).split(/,\s*/); var fakes_modules = content.slice(fakes_ind, fakes_eol).split(/,\s*/);
fakes_modules.forEach(function (mod) { 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) { ...@@ -67,27 +89,27 @@ if (program.autoInject) {
temp.track(); temp.track();
var template = { var template = {
header: "<html>\n<head>\n<meta charset='utf-8' />\n<link rel='stylesheet' href='" + path.resolve(__dirname, '../node_modules/mocha/mocha.css') + "'/>\n</head>\n<body><div id='mocha'></div>", header: "<html>\n<head>\n<meta charset='utf-8' />\n<link rel='stylesheet' href='" + get_path('node_modules/mocha/mocha.css') + "'/>\n</head>\n<body><div id='mocha'></div>",
script_tag: function(p) { return "<script src='" + p + "'></script>"; }, script_tag: function(p) { return "<script src='" + p + "'></script>"; },
footer: "<script>\nmocha.checkLeaks();\nmocha.globals(['navigator', 'create', 'ClientUtils', '__utils__']);\nmocha.run(function () { window.__mocha_done = true; });\n</script>\n</body>\n</html>" footer: "<script>\nmocha.checkLeaks();\nmocha.globals(['navigator', 'create', 'ClientUtils', '__utils__']);\nmocha.run(function () { window.__mocha_done = true; });\n</script>\n</body>\n</html>"
}; };
template.header += "\n" + template.script_tag(path.resolve(__dirname, '../node_modules/chai/chai.js')); template.header += "\n" + template.script_tag(get_path('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(get_path('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(get_path('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(get_path('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/sinon-chai/lib/sinon-chai.js'));
template.header += "\n<script>mocha.setup('bdd');</script>"; template.header += "\n<script>mocha.setup('bdd');</script>";
template.header = program.autoInject.reduce(function(acc, sn) { 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); }, template.header);
file_paths = program.tests.map(function(jsn, ind) { file_paths = program.tests.map(function(jsn, ind) {
var templ = template.header; var templ = template.header;
templ += "\n"; templ += "\n";
templ += template.script_tag(path.resolve(process.cwd(), jsn)); templ += template.script_tag(get_path_cwd(jsn));
templ += template.footer; templ += template.footer;
var tempfile = temp.openSync({ prefix: 'novnc-zombie-inject-', suffix: '-file_num-'+ind+'.html' }); var tempfile = temp.openSync({ prefix: 'novnc-zombie-inject-', suffix: '-file_num-'+ind+'.html' });
...@@ -118,15 +140,19 @@ if (program.outputHtml) { ...@@ -118,15 +140,19 @@ if (program.outputHtml) {
return; return;
} }
if (use_ansi) {
cursor cursor
.bold() .bold()
.write(program.tests[path_ind]) .write(program.tests[path_ind])
.reset() .reset()
.write("\n") .write("\n")
.write(Array(program.tests[path_ind].length+1).join('=')) .write(Array(program.tests[path_ind].length+1).join('='))
.write("\n\n") .write("\n\n");
}
cursor
.write(data) .write(data)
.write("\n"); .write("\n\n");
}); });
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment