-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathcli.js
executable file
·124 lines (114 loc) · 3.43 KB
/
cli.js
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
#!/usr/bin/env node
var util = require('util'),
argsparser = require('argsparser'),
fs = require('fs');
var root = __dirname + '/..',
args = argsparser.parse(),
testrunner = require(root),
o = testrunner.options,
code, tests, debug,
help;
help = ''
+ '\nUsage: cli [options] value (boolean value can be used)'
+ '\n'
+ '\nOptions:'
+ '\n -c, --code path to code you want to test'
+ '\n -t, --tests path to tests (space separated)'
+ '\n --debug debugging port'
+ '\n -d, --deps dependency paths - files required before code (space separated)'
+ '\n -l, --log logging options, json have to be used'
+ '\n --cov create tests coverage report'
+ '\n -h, --help show this help'
+ '\n -v, --version show module version'
+ '\n';
/**
* Parses a code or dependency argument, returning an object defining the
* specified file path or/and module name.
* The exports of the module will be exposed globally by default. To expose
* exports as a named variable, prefix the resource with the desired variable
* name followed by a colon.
* This allows you to more accurately recreate browser usage of QUnit, for
* tests which are portable between browser runtime environmemts and Node.js.
* @param {string} path to file or module name to require.
* @return {Object} resource
*/
function parsePath(path) {
var parts = path.split(':'),
resource = {
path: path
};
if (parts.length === 2) {
resource.namespace = parts[0];
resource.path = parts[1];
}
return resource;
}
for (var key in args) {
switch(key) {
case 'node':
// Skip the 'node' argument
break;
case '-c':
case '--code':
code = parsePath(args[key]);
break;
case '-t':
case '--tests':
// it's assumed that tests arguments will be file paths whose
// contents are to be made global. This is consistent with use
// of QUnit in browsers.
tests = args[key];
break;
case '--debug':
debug = args[key];
break;
case '-d':
case '--deps':
o.deps = args[key];
if (!Array.isArray(o.deps)) {
o.deps = [o.deps];
}
o.deps = o.deps.map(parsePath);
break;
case '-l':
case '--log':
eval('o.log = ' + args[key]);
break;
case '--cov':
o.coverage = args[key];
break;
case '-p':
case '--paths':
o.paths = args[key];
break;
case '-v':
case '--version':
util.print(
JSON.parse(
fs.readFileSync(__dirname + '/../package.json')
).version + '\n'
);
return;
case '-h':
case '-?':
case '--help':
util.print(help);
return;
}
}
if(!code || !tests) {
util.print(help);
util.print('\nBoth --code and --tests arguments are required\n');
return;
}
debug = debug || process.execArgv.reduce(function (prevArg, curArg) {
return prevArg || curArg.indexOf('--debug') === 0;
}, false);
testrunner.run({ code: code, tests: tests, deps: o.deps, log: o.log, debug: debug }, function(err, stats) {
if (err) {
console.error(err);
process.exit(1);
return;
}
process.exit(stats.failed > 0 ? 1 : 0);
});