Skip to content

Commit 35d9d0d

Browse files
committed
Better factoring of batch mode runners
They all will share a similar structure, and I think this design will work for any hosts that 1) have some mechanism to return the global of the new environment and 2) some mechanism to evaluate a test in that env
1 parent 19478c1 commit 35d9d0d

File tree

2 files changed

+106
-63
lines changed

2 files changed

+106
-63
lines changed

lib/runners/console.js

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,71 @@ var errorFn = function $ERROR(err) {
1919
else $LOG("test262/error Error: " + err);
2020
}.toString()
2121

22+
var batchDoneFn = function $DONE(err) {
23+
if(err) { if(typeof err === "object" && err !== null && "name" in err)
24+
$LOG("test262/error " + err.name + ": " + err.message)
25+
else $LOG("test262/error Error: " + err);
26+
}
27+
$LOG('test262/done');
28+
$LOG('test262/test-end');
29+
if(tests.length > 0) runNext();
30+
}.toString()
31+
32+
2233
function ConsoleRunner(args) {
2334
this.command = args.consoleCommand;
2435
this.printCommand = args.consolePrintCommand || "console.log";
25-
this.deps = [
26-
doneFn,
27-
errorFn,
28-
'function $LOG(str) { ' + this._print('str') + '}',
29-
]
36+
37+
if(args.batch) {
38+
// Done comes from the parent context
39+
this.deps = [
40+
errorFn,
41+
this.logFn
42+
]
43+
} else {
44+
this.deps = [
45+
doneFn,
46+
errorFn,
47+
this.logFn
48+
]
49+
}
3050
if(!this.command) throw "--consoleCommand option required for console runner";
3151

52+
3253
Runner.apply(this, arguments);
3354
}
3455
ConsoleRunner.prototype = Object.create(Runner.prototype);
3556
ConsoleRunner.prototype._print = function(str) {
3657
return this.printCommand + '(' + str + ');\n';
3758
}
59+
Object.defineProperty(ConsoleRunner.prototype, 'logFn', {
60+
get: memoize(function() {
61+
return 'function $LOG(str) { ' + this._print('str') + '}';
62+
})
63+
});
64+
65+
Object.defineProperty(ConsoleRunner.prototype, 'runNextFn', {
66+
get: memoize(function() {
67+
if(!this._createEnv) throw "Don't know how to create an environment";
68+
if(!this._runBatched) throw "Don't know how to run a batched tests";
69+
70+
var runNextFn = function runNext() {
71+
var test = tests.shift();
72+
var env = $1;
73+
env.$DONE = $DONE;
74+
75+
try {
76+
$LOG('test262/test-start')
77+
$2;
78+
} catch(e) {
79+
$DONE(e);
80+
}
81+
}.toString();
82+
83+
return runNextFn.replace("$1", this._createEnv)
84+
.replace("$2", this._runBatched)
85+
})
86+
});
3887

3988
ConsoleRunner.prototype.execute = function(test, cb) {
4089
var runner = this;
@@ -58,3 +107,53 @@ ConsoleRunner.prototype.execute = function(test, cb) {
58107
});
59108
}
60109

110+
ConsoleRunner.prototype.executeBatch = function(batch, batchDone) {
111+
var runner = this;
112+
var scriptFile = '__tmp' + counter++ + '.js';
113+
var script = this.logFn + '\n' +
114+
batchDoneFn + '\n' +
115+
this.runNextFn + '\n';
116+
117+
script += 'var tests = ' + JSON.stringify(batch.map(function(test, i) {
118+
return test.contents
119+
})) + '\n';
120+
121+
script += 'runNext();'
122+
123+
fs.writeFileSync(scriptFile, script);
124+
125+
cp.exec(this.command + " " + scriptFile, function(err, stdout, stderr) {
126+
var results = { log: [] };
127+
var lines = stdout.split(/\r?\n/);
128+
var index = 0;
129+
130+
lines.forEach(function(line) {
131+
switch(line) {
132+
case 'test262/test-start':
133+
break;
134+
case 'test262/test-end':
135+
var test = batch[index++];
136+
runner.validateResult(test, results);
137+
results = { log: [] };
138+
139+
break;
140+
default:
141+
results.log.push(line);
142+
}
143+
})
144+
145+
//fs.unlink(scriptFile);
146+
batchDone();
147+
});
148+
}
149+
150+
151+
152+
function memoize(getter) {
153+
var val = null;
154+
155+
return function() {
156+
if(val) return val;
157+
return val = getter.apply(this);
158+
}
159+
}

lib/runners/jsshell.js

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,8 @@ function JSShellRunner(args) {
1212
args.consolePrintCommand = args.consolePrintCommand || "print";
1313

1414
ConsoleRunner.apply(this, arguments);
15-
16-
if(args.batch) {
17-
this.batchTestStart =
18-
'var env = newGlobal();\n' +
19-
'try {\n';
20-
21-
this.batchTestEnd =
22-
'} catch(e) {\n' +
23-
'if(e.hasOwnProperty("name")) ' +
24-
this._print('"test262/error " + e.name + " " + e.message') +
25-
'else ' + this._print('"test262/error " + e') +
26-
'}\n';
27-
}
28-
2915
}
3016

3117
JSShellRunner.prototype = Object.create(ConsoleRunner.prototype);
32-
33-
JSShellRunner.prototype.executeBatch = function(batch, batchDone) {
34-
var runner = this;
35-
var baseFile = '__tmp' + counter++ + '_';
36-
var script = '';
37-
38-
// write test files to disk
39-
batch.forEach(function(test, i) {
40-
script += this._print('"test262/test-start"')
41-
+ this.batchTestStart
42-
+ 'env.evaluate(' + JSON.stringify(test.contents) + ');\n'
43-
+ this.batchTestEnd
44-
+ this._print('"test262/test-end"');
45-
}, this);
46-
47-
var scriptFile = baseFile + 'main.js';
48-
49-
fs.writeFileSync(scriptFile, script);
50-
51-
cp.exec(this.command + " " + scriptFile, function(err, stdout, stderr) {
52-
var results = { log: [] };
53-
var lines = stdout.split(/\r?\n/);
54-
var index = 0;
55-
56-
lines.forEach(function(line) {
57-
switch(line) {
58-
case 'test262/test-start':
59-
break;
60-
case 'test262/test-end':
61-
var test = batch[index++];
62-
runner.validateResult(test, results);
63-
results = { log: [] };
64-
65-
break;
66-
default:
67-
results.log.push(line);
68-
}
69-
})
70-
71-
fs.unlink(scriptFile);
72-
batchDone();
73-
});
74-
}
75-
18+
JSShellRunner.prototype._createEnv = 'newGlobal()';
19+
JSShellRunner.prototype._runBatched = 'env.evaluate(test);'

0 commit comments

Comments
 (0)