Jelajahi Sumber

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).
Solly Ross 11 tahun lalu
induk
melakukan
b11bb5c385
1 mengubah file dengan 44 tambahan dan 18 penghapusan
  1. 44 18
      tests/run_from_console.js

+ 44 - 18
tests/run_from_console.js

@@ -17,8 +17,9 @@ program
   .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('-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: "<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>"; },
     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(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<script>mocha.setup('bdd');</script>";
 
 
   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");
     });
   });
 }