|
1 | 1 | var Tyrtle = require('../Tyrtle'),
|
2 | 2 | renderer = require('../renderers/node'),
|
3 |
| - tests = require('./node-tests'), |
4 |
| - t, |
5 |
| - i, l |
| 3 | + fs = require('fs'), |
| 4 | + path = require('path'), |
| 5 | + glob, |
| 6 | + tests, |
| 7 | + args = require('optimist') |
| 8 | + .boolean("monochrome") |
| 9 | + .describe("monochrome", "Output without any colors") |
| 10 | + .boolean("only-errors") |
| 11 | + .describe("only-errors", "Only show tests which fail") |
| 12 | + .string("list") |
| 13 | + .describe("list", "If this is set, then the files specified should contain a JSON object specifying the paths " |
| 14 | + + "of other files to include" |
| 15 | + ) |
| 16 | + .boolean('dry') |
| 17 | + .alias('dry', 'dry-run') |
| 18 | + .describe('dry', "Perform a dry run. In this mode, none of the test modules are loaded. Instead, a list of the " |
| 19 | + + "files which *would* have been loaded is displayed and the program exits. This is useful for " |
| 20 | + + "testing file-matching patterns") |
| 21 | + .usage("[1] : $0 [options] [--] file [file [file ...]]\n" |
| 22 | + + "[2] : $0 [options] --list fileList\n" |
| 23 | + + "\n" |
| 24 | + + "In method [1], wildcards can be used, eg: $0 *.test.js test.*.js\n" |
| 25 | + + "To use recursive matching (through subdirectories) you will need to quote the strings,\n" |
| 26 | + + "eg: $0 \"tests/**.js\" \"vendors/tests/**\"" |
| 27 | + + "\n" |
| 28 | + + "In method [2], paths in the file list are relative to the list itself. Wildcard patterns can also be \n" |
| 29 | + + "used inside a file list, however no extra quoting is required." |
| 30 | + ) |
| 31 | + .wrap(80) |
| 32 | + .check(function (args) { |
| 33 | + // this function throws so that optimist doesn't print out the body of this function |
| 34 | + if (args._.length === 0) { |
| 35 | + if (!args.list && !args.pattern) { |
| 36 | + throw ""; |
| 37 | + } |
| 38 | + } else if (args.list || args.pattern) { |
| 39 | + throw ""; |
| 40 | + } |
| 41 | + }) |
| 42 | + .argv, |
| 43 | + color, |
| 44 | + t, i, l, |
| 45 | + loadFiles, |
| 46 | + inp, inptext, |
| 47 | + runTests, |
| 48 | + oldCwd |
6 | 49 | ;
|
7 |
| -Tyrtle.setRenderer(renderer); |
| 50 | +//console.log(args); |
| 51 | +//process.exit(0); |
8 | 52 |
|
9 |
| -t = new Tyrtle(); |
| 53 | +renderer.setMonochrome(args.monochrome); |
| 54 | +renderer.onlyErrors = args['only-errors']; |
| 55 | +Tyrtle.setRenderer(renderer); |
10 | 56 |
|
11 |
| -if (Tyrtle.isArray(tests)) { |
12 |
| - for (i = 0, l = tests.length; i < l; ++i) { |
13 |
| - t.module(tests[i]); |
| 57 | +color = args.monochrome |
| 58 | + ? function (col, s) { |
| 59 | + return s; |
| 60 | + } |
| 61 | + : (function () { |
| 62 | + var c = { |
| 63 | + red : '31', |
| 64 | + white : '1;37' |
| 65 | + }; |
| 66 | + return function (col, s) { |
| 67 | + return '\u001b[' + c[col] + 'm' + s + '\u001b[0m'; |
| 68 | + }; |
| 69 | + }()) |
| 70 | +; |
| 71 | +runTests = function () { |
| 72 | + t = new Tyrtle(); |
| 73 | + var i, l, val, re_glob, newFiles, run = false; |
| 74 | + re_glob = /[?*]/; |
| 75 | + for (i = 0, l = loadFiles.length; i < l; ++i) { |
| 76 | + val = loadFiles[i]; |
| 77 | + if (re_glob.test(val)) { |
| 78 | + glob = glob || require('glob'); |
| 79 | + newFiles = glob.globSync(val); |
| 80 | + loadFiles.push.apply(loadFiles, newFiles); |
| 81 | + l += newFiles.length; |
| 82 | + } else { |
| 83 | + if (!(/^\.{0,2}\//.test(val))) { |
| 84 | + val = process.cwd() + '/' + val; |
| 85 | + } |
| 86 | + if (args.dry) { |
| 87 | + console.log(fs.realpathSync(val)); |
| 88 | + } else { |
| 89 | + try { |
| 90 | + tests = require(val); |
| 91 | + } catch (e) { |
| 92 | + console.error( |
| 93 | + color('red', "Could not load module ") |
| 94 | + + color('white', "%s"), |
| 95 | + val.replace(/\.js$/, '') + '.js' |
| 96 | + ); |
| 97 | + continue; |
| 98 | + } |
| 99 | + run = true; |
| 100 | + if (Tyrtle.isArray(tests)) { |
| 101 | + for (i = 0, l = tests.length; i < l; ++i) { |
| 102 | + t.module(tests[i]); |
| 103 | + } |
| 104 | + } else { |
| 105 | + t.module(tests); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
14 | 109 | }
|
| 110 | + if (run) { |
| 111 | + t.run(); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | + |
| 116 | +if (args.list) { |
| 117 | + oldCwd = process.cwd(); |
| 118 | + inp = fs.createReadStream(args.list); // TODO: handle the cwd |
| 119 | + process.chdir(path.dirname(fs.realpathSync(args.list))); |
| 120 | + inptext = ""; |
| 121 | + |
| 122 | + inp.setEncoding('utf8'); |
| 123 | + inp.on('data', function (data) { |
| 124 | + inptext += data; |
| 125 | + }); |
| 126 | + inp.on('end', function (close) { |
| 127 | + loadFiles = JSON.parse(inptext); |
| 128 | + runTests(); |
| 129 | + process.chdir(oldCwd); |
| 130 | + }); |
15 | 131 | } else {
|
16 |
| - t.module(tests); |
| 132 | + loadFiles = args._; |
| 133 | + runTests(); |
17 | 134 | }
|
18 |
| -t.run(); |
|
0 commit comments