summaryrefslogtreecommitdiff
path: root/tests/run_from_console.js
blob: d7d13da2320890605a8cf4a467f1b43a79096d0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env node
var ansi = require('ansi');
var program = require('commander');
var path = require('path');
var fs = require('fs');

var make_list = function(val) {
  return val.split(',');
};

program
  .option('-t, --tests <testlist>', 'Run the specified html-file-based test(s). \'testlist\' should be a comma-separated list', make_list, [])
  .option('-a, --print-all', 'Print all tests, not just the failures')
  .option('--disable-color', 'Explicitly disable color')
  .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 .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('--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')
  .option('--debugger <port>', 'Enable the remote debugger for CasperJS')
  .parse(process.argv);

if (program.tests.length === 0) {
  program.tests = fs.readdirSync(__dirname).filter(function(f) { return (/^test\.(\w|\.|-)+\.js$/).test(f); });
  program.tests = program.tests.map(function (f) { return path.resolve(__dirname, f); }); // add full paths in
  console.log('using files %s', program.tests);
}

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 = {};

  // 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[get_path('core/', mod) + '.js'] = 1;
      });
    }

    var fakes_ind = content.indexOf('requires test modules: ');
    if (fakes_ind > -1) {
      fakes_ind += 'requires test modules: '.length;
      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[get_path('tests/', mod) + '.js'] = 1;
      });
    }
  });

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

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

  var template = {
    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(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(get_path_cwd(sn));
  }, template.header);

  file_paths = program.tests.map(function(jsn, ind) {
    var templ = template.header;
    templ += "\n";
    templ += template.script_tag(get_path_cwd(jsn));
    templ += template.footer;

    var tempfile = temp.openSync({ prefix: 'novnc-zombie-inject-', suffix: '-file_num-'+ind+'.html' });
    fs.writeSync(tempfile.fd, templ);
    fs.closeSync(tempfile.fd);
    return tempfile.path;
  });

}
else {
  file_paths = program.tests.map(function(fn) {
    return path.resolve(process.cwd(), fn);
  });
}

var use_ansi = false;
if (program.color) use_ansi = true;
else if (program.disableColor) use_ansi = false;
else if (process.stdout.isTTY) use_ansi = true;

var cursor = ansi(process.stdout, { enabled: use_ansi });

if (program.outputHtml) {
  file_paths.forEach(function(path, path_ind) {
    fs.readFile(path, function(err, data) {
      if (err) {
        console.warn(error.stack);
        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
        .write(data)
        .write("\n\n");
    });
  });
}

if (program.generateHtml) {
  var open_browser;
  if (program.openInBrowser) {
    open_browser = require('open');
  }

  file_paths.forEach(function(path, path_ind) {
    cursor
      .bold()
      .write(program.tests[path_ind])
      .write(": ")
      .reset()
      .write(path)
      .write("\n");

    if (program.openInBrowser) {
      open_browser(path);
    }
  });
  console.log('');
}

if (program.generateHtml) {
  process.stdin.resume();  // pause until C-c
  process.on('SIGINT', function() {
    process.stdin.pause();  // exit
  });
}

if (!program.outputHtml && !program.generateHtml) {
  var failure_count = 0;

  var prov = require(path.resolve(__dirname, 'run_from_console.'+program.provider+'.js'));

  cursor
    .write("Running tests ")
    .bold()
    .write(program.tests.join(', '))
    .reset()
    .grey()
    .write(' using provider '+prov.name)
    .reset()
    .write("\n");
  //console.log("Running tests %s using provider %s", program.tests.join(', '), prov.name);

  var provider = prov.provide_emitter(file_paths, program.debugger);
  provider.on('test_ready', function(test_json) {
    console.log('');

    filename = program.tests[test_json.file_ind];

    cursor.bold();
    console.log('Results for %s:', filename);
    console.log(Array('Results for :'.length+filename.length+1).join('='));
    cursor.reset();

    console.log('');

    cursor
      .write(''+test_json.num_tests+' tests run, ')
      .green()
      .write(''+test_json.num_passes+' passed');
    if (test_json.num_slow > 0) {
      cursor
        .reset()
        .write(' (');
      cursor
        .yellow()
        .write(''+test_json.num_slow+' slow')
        .reset()
        .write(')');
    }
    cursor
      .reset()
      .write(', ');
    cursor
      .red()
      .write(''+test_json.num_fails+' failed');
    if (test_json.num_skipped > 0) {
      cursor
        .reset()
        .write(', ')
        .grey()
        .write(''+test_json.num_skipped+' skipped');
    }
    cursor
      .reset()
      .write(' -- duration: '+test_json.duration+"s\n");

    console.log('');

    if (test_json.num_fails > 0 || program.printAll) {
      var extract_error_lines = function (err) {
        // the split is to avoid a weird thing where in PhantomJS where we get a stack trace too
        var err_lines = err.split('\n');
        if (err_lines.length == 1) {
          return err_lines[0];
        } else {
          var ind;
          for (ind = 0; ind < err_lines.length; ind++) {
            var at_ind = err_lines[ind].trim().indexOf('at ');
            if (at_ind === 0) {
              break;
            }
          }

          return err_lines.slice(0, ind).join('\n');
        }
      };

      var traverse_tree = function(indentation, node) {
        if (node.type == 'suite') {
          if (!node.has_subfailures && !program.printAll) return;

          if (indentation === 0) {
            cursor.bold();
            console.log(node.name);
            console.log(Array(node.name.length+1).join('-'));
            cursor.reset();
          }
          else {
            cursor
              .write(Array(indentation+3).join('#'))
              .bold()
              .write(' '+node.name+' ')
              .reset()
              .write(Array(indentation+3).join('#'))
              .write("\n");
          }

          console.log('');

          for (var i = 0; i < node.children.length; i++) {
            traverse_tree(indentation+1, node.children[i]);
          }
        }
        else {
          if (!node.pass) {
            cursor.magenta();
            console.log('- failed: '+node.text+test_json.replay);
            cursor.red();
            console.log('          '+extract_error_lines(node.error));
            cursor.reset();
            console.log('');
          }
          else if (program.printAll) {
            if (node.skipped) {
              cursor
                .grey()
                .write('- skipped: '+node.text);
            }
            else {
              if (node.slow) cursor.yellow();
              else cursor.green();

              cursor
                .write('- pass: '+node.text)
                .grey()
                .write(' ('+node.duration+') ');
            }
            /*if (node.slow) cursor.yellow();
            else cursor.green();*/
            cursor
              //.write(test_json.replay)
              .reset()
              .write("\n");
            console.log('');
          }
        }
      };

      for (var i = 0; i < test_json.suites.length; i++) {
        traverse_tree(0, test_json.suites[i]);
      }
    }

    if (test_json.num_fails === 0) {
      cursor.fg.green();
      console.log('all tests passed :-)');
      cursor.reset();
    }
  });

  if (program.debug) {
    provider.on('console', function(line) {
      // log to stderr
      console.error(line);
    });
  }

  provider.on('error', function(line) {
    // log to stderr
    console.error('ERROR: ' + line);
  });

  /*gprom.finally(function(ph) {
    ph.exit();
    // exit with a status code that actually gives information
    if (program.exitWithFailureCount) process.exit(failure_count);
  });*/
}