Skip to content

Commit 57d520d

Browse files
committed
Add useConfig and a config flag
1 parent c5ed940 commit 57d520d

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

bin/run.js

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
// This code is governed by the BSD License found in the LICENSE file.
55

66
var args = require('minimist')(process.argv.slice(2), {
7-
default: {
8-
runner: 'node',
9-
reporter: 'simple',
10-
threads: 4,
11-
},
127
alias: {
138
consoleCommand: 'e',
149
consolePrintCommand: 'p',
1510
runner: 'r',
1611
reporter: 'R',
1712
threads: 't',
18-
batch: 'b'
13+
batch: 'b',
14+
config: 'c'
1915
}
2016
});
2117

@@ -28,68 +24,58 @@ var fs = require('fs');
2824
var path = require('path');
2925
var jss = require('JSONStream').stringify();
3026
var Tributary = require('stream-bifurcate')
31-
3227
var scenarios = require('../lib/scenarios');
3328
var readFile = _.wrapCallback(fs.readFile);
3429
var DEFAULT_BATCH_SIZE = 75;
30+
var t262 = require('../index');
3531

36-
if(args.config) {
37-
require(path.join(process.cwd(), args.config));
38-
}
39-
40-
var t262 = require('../index')
41-
console.log(t262.config);
42-
43-
console.log()
44-
45-
var Runner = loadRunner();
46-
console.log(Runner);
32+
if(args.config) require(path.join(process.cwd(), args.config));
33+
t262.useConfig(args);
4734

48-
// default to console runner if passing console command
49-
if(args.consoleCommand && args.runner === 'node') {
50-
args.runner = 'console';
51-
}
35+
var Runner = t262.loadRunner();
5236

53-
// apply default batch size
54-
if(args.batch === true) args.batch = DEFAULT_BATCH_SIZE;
37+
// apply defaults
38+
if(t262.config.batch === true) t262.config.batch = DEFAULT_BATCH_SIZE;
39+
if(!t262.config.threads) t262.config.threads = 4;
40+
if(!t262.config.reporter) t262.config.reporter = 'simple';
5541

5642
var start = Date.now();
5743

58-
var files = _(args._.map(globStream)).merge();
44+
var files = _(t262.config._.map(globStream)).merge();
5945
var contents = files.fork().map(readFile).sequence();
6046
var tests = contents.zip(files.fork()).map(function(d) {
6147
return parser.parseFile({ contents: d[0].toString('utf8'), file: d[1]});
6248
});
63-
var getScenarios = scenarios(args);
49+
var getScenarios = scenarios(t262.config);
6450
var scenarios = tests.flatMap(scenarioStream);
65-
if(args.batch) scenarios = _(scenarios).batch(args.batch);
51+
if(t262.config.batch) scenarios = _(scenarios).batch(t262.config.batch);
6652

6753
var trb = scenarios.pipe(new Tributary());
6854
var results = _(function(push) {
69-
for(var i = 0; i < args.threads; i++) push(null, run(trb.fork()));
55+
for(var i = 0; i < t262.config.threads; i++) push(null, run(trb.fork()));
7056
push(null, _.nil);
7157
}).merge();
7258

7359
results.on('end', function() {
7460
console.log("Took " + ((Date.now() - start) / 1000) + " seconds");
7561
})
7662

77-
if(args.reporter === 'json') {
63+
if(t262.config.reporter === 'json') {
7864
results.pipe(jss).pipe(process.stdout);
79-
} else if(args.reporter === 'tap') {
65+
} else if(t262.config.reporter === 'tap') {
8066
results.pipe(tapify).pipe(process.stdout);
81-
} else if(args.reporter === 'simple') {
67+
} else if(t262.config.reporter === 'simple') {
8268
results.pipe(simpleReporter);
8369
}
8470

8571
// takes a test collateral stream.
8672
// Returns test results stream.
8773
function run(tests) {
88-
var runner = new Runner(args);
74+
var runner = new Runner(t262.config);
8975

9076
return _(tests).map(function(test) {
9177
return _(function(push) {
92-
if(args.batch) {
78+
if(t262.config.batch) {
9379
runner.runBatch(test, function() {
9480
test.forEach(function(t) {
9581
push(null, t);
@@ -129,15 +115,3 @@ function scenarioStream(test) {
129115
push(null, _.nil);
130116
})
131117
}
132-
133-
// Load the runner
134-
function loadRunner() {
135-
if(t262.config.runner) return t262.config.runner;
136-
137-
try {
138-
return require('../lib/runners/' + args.runner);
139-
} catch(e) {
140-
if(e.code === 'MODULE_NOT_FOUND') throw new Error('Runner ' + args.runner + ' not found.');
141-
throw e;
142-
}
143-
}

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,26 @@ exports.useConfig = function(conf) {
1010
config[k] = conf[k];
1111
})
1212
}
13+
14+
exports.loadRunner = function loadRunner() {
15+
if(typeof config.runner === 'string')
16+
return requireRunner(config.runner);
17+
18+
if(typeof config.runner === 'function')
19+
return config.runner;
20+
21+
if(config.consoleCommand)
22+
return requireRunner('console')
23+
24+
return requireRunner('node');
25+
}
26+
27+
function requireRunner(name) {
28+
try {
29+
return require('./lib/runners/' + name);
30+
} catch(e) {
31+
if(e.code === 'MODULE_NOT_FOUND')
32+
throw new Error('Runner ' + name + ' not found.');
33+
throw e;
34+
}
35+
}

0 commit comments

Comments
 (0)