1 | #!/usr/bin/env node
|
2 |
|
3 | var
|
4 | fs = require('fs'),
|
5 | path = require('path');
|
6 |
|
7 |
|
8 | //require.paths.push(process.cwd());
|
9 | var args = (process.ARGV || process.argv).slice(2);
|
10 |
|
11 | var files = [];
|
12 |
|
13 | var testrunner,
|
14 | config_file,
|
15 | config_param_found = false,
|
16 | output_param_found = false,
|
17 | reporter_file = 'default',
|
18 | reporter_param_found = false,
|
19 | testspec_param_found = false,
|
20 | testFullSpec_param_found = false;
|
21 |
|
22 | var usage = "Usage: nodeunit [options] testmodule1.js testfolder [...] \n" +
|
23 | "Options:\n\n" +
|
24 | " --config FILE the path to a JSON file with options\n" +
|
25 | " --reporter FILE optional path to a reporter file to customize the output\n" +
|
26 | " --list-reporters list available build-in reporters\n" +
|
27 | " -r recursively run tests in sub-directories\n" +
|
28 | " -t testName, specify a test to run\n" +
|
29 | " -f fullTestName, specify a specific test to run. fullTestName is built so: \"outerGroup - .. - innerGroup - testName\"\n" +
|
30 | " -h, --help display this help and exit\n" +
|
31 | " -v, --version output version information and exit";
|
32 |
|
33 |
|
34 |
|
35 | // load default options
|
36 | var content = fs.readFileSync(__dirname + '/nodeunit.json', 'utf8');
|
37 | var options = JSON.parse(content);
|
38 |
|
39 | // a very basic pseudo --options parser
|
40 | args.forEach(function (arg) {
|
41 | if (arg.slice(0, 9) === "--config=") {
|
42 | config_file = arg.slice(9);
|
43 | } else if (arg === '--config') {
|
44 | config_param_found = true;
|
45 | } else if (config_param_found) {
|
46 | config_file = arg;
|
47 | config_param_found = false;
|
48 | } else if (arg.slice(0, 9) === "--output=") {
|
49 | options.output = arg.slice(9);
|
50 | } else if (arg === '--output') {
|
51 | output_param_found = true;
|
52 | } else if (output_param_found) {
|
53 | options.output = arg;
|
54 | output_param_found = false;
|
55 | } else if (arg.slice(0, 11) === "--reporter=") {
|
56 | reporter_file = arg.slice(11);
|
57 | } else if (arg === '--reporter') {
|
58 | reporter_param_found = true;
|
59 | } else if (reporter_param_found) {
|
60 | reporter_file = arg;
|
61 | reporter_param_found = false;
|
62 | } else if (arg === '-r') {
|
63 | options.recursive = true;
|
64 | } else if (arg === '-t') {
|
65 | testspec_param_found = true;
|
66 | } else if (testspec_param_found) {
|
67 | options.testspec = arg;
|
68 | testspec_param_found = false;
|
69 | } else if (arg === '-f') {
|
70 | testFullSpec_param_found = true;
|
71 | } else if (testFullSpec_param_found) {
|
72 | options.testFullSpec= arg;
|
73 | testFullSpec_param_found = false;
|
74 | } else if (arg === '--list-reporters') {
|
75 | var reporters = fs.readdirSync(__dirname + '/../lib/reporters');
|
76 | reporters = reporters.filter(function (reporter_file) {
|
77 | return (/\.js$/).test(reporter_file);
|
78 | }).map(function (reporter_file) {
|
79 | return reporter_file.replace(/\.js$/, '');
|
80 | }).filter(function (reporter_file) {
|
81 | return reporter_file !== 'index';
|
82 | });
|
83 | console.log('Built-in reporters: ');
|
84 | reporters.forEach(function (reporter_file) {
|
85 | var reporter = require('../lib/reporters/' + reporter_file);
|
86 | console.log(' * ' + reporter_file + (reporter.info ? ': ' + reporter.info : ''));
|
87 | });
|
88 | process.exit(0);
|
89 | } else if ((arg === '-v') || (arg === '--version')) {
|
90 | var content = fs.readFileSync(__dirname + '/../package.json', 'utf8');
|
91 | var pkg = JSON.parse(content);
|
92 | console.log(pkg.version);
|
93 | process.exit(0);
|
94 | } else if ((arg === '-h') || (arg === '--help')) {
|
95 | console.log(usage);
|
96 | process.exit(0);
|
97 | } else {
|
98 | files.push(arg);
|
99 | }
|
100 | });
|
101 |
|
102 | // defaults to `test`
|
103 | if (files.length === 0) {
|
104 | files = ['test'];
|
105 | }
|
106 |
|
107 | if (config_file) {
|
108 | content = fs.readFileSync(config_file, 'utf8');
|
109 | var custom_options = JSON.parse(content);
|
110 |
|
111 | for (var option in custom_options) {
|
112 | if (typeof option === 'string') {
|
113 | options[option] = custom_options[option];
|
114 | }
|
115 | }
|
116 | }
|
117 |
|
118 | var builtin_reporters = require(__dirname + '/../lib/reporters');
|
119 | if (reporter_file in builtin_reporters) {
|
120 | testrunner = builtin_reporters[reporter_file];
|
121 | }
|
122 | else {
|
123 | testrunner = require(reporter_file);
|
124 | }
|
125 |
|
126 | testrunner.run(files, options, function(err) {
|
127 | process.exit(err ? 1 : 0);
|
128 | });
|