Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit ee73f2d

Browse files
CrispusDHcnishina
authored andcommitted
chore(promises) move test_util out of q (#5036)
1 parent 13a8668 commit ee73f2d

File tree

2 files changed

+110
-105
lines changed

2 files changed

+110
-105
lines changed

scripts/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ const passingTests = [
5555
// 'node spec/install/test.js'
5656
];
5757

58-
var executor = new Executor();
58+
const executor = new Executor();
5959

60-
passingTests.forEach(function(passing_test) {
60+
passingTests.forEach((passing_test) => {
6161
executor.addCommandlineTest(passing_test)
6262
.assertExitCodeOnly();
6363
});

scripts/test/test_util.js

Lines changed: 108 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
#!/usr/bin/env node
22

3-
var child_process = require('child_process'),
4-
q = require('q'),
5-
fs = require('fs');
6-
7-
var CommandlineTest = function(command) {
8-
var self = this;
9-
this.command_ = command;
10-
this.expectedExitCode_ = 0;
11-
this.expectedErrors_ = [];
12-
this.assertExitCodeOnly_ = false;
13-
this.testLogStream = undefined;
3+
const child_process = require('child_process');
4+
const fs = require('fs');
5+
6+
class CommandlineTest {
7+
constructor(command) {
8+
this.command = command;
9+
this.expectedExitCode = 0;
10+
this.expectedErrors = [];
11+
this.isExitCode = false;
12+
this.testLogStream = undefined;
13+
this.expectedMinTestDuration = undefined;
14+
this.expectedMaxTestDuration = undefined;
15+
}
16+
1417

1518
// Only assert the exit code and not failures.
1619
// This must be true if the command you're running does not support
1720
// the flag '--resultJsonOutputFile'.
18-
this.assertExitCodeOnly = function() {
19-
self.assertExitCodeOnly_ = true;
20-
return self;
21-
};
21+
assertExitCodeOnly() {
22+
this.isExitCode = true;
23+
return this;
24+
}
2225

23-
this.setTestLogFile = function(filename) {
24-
self.testLogStream = fs.createWriteStream(filename, {flags: 'a'});
25-
};
26+
setTestLogFile(filename) {
27+
this.testLogStream = fs.createWriteStream(filename, {flags: 'a'});
28+
}
2629

2730
// Set the expected exit code for the test command.
28-
this.expectExitCode = function(exitCode) {
29-
self.expectedExitCode_ = exitCode;
30-
return self;
31-
};
31+
expectExitCode(exitCode) {
32+
this.expectedExitCode = exitCode;
33+
return this;
34+
}
3235

3336
// Set the expected total test duration in milliseconds.
34-
this.expectTestDuration = function(min, max) {
35-
self.expectedMinTestDuration_ = min;
36-
self.expectedMaxTestDuration_ = max;
37-
return self;
38-
};
37+
expectTestDuration(min, max) {
38+
this.expectedMinTestDuration = min;
39+
this.expectedMaxTestDuration = max;
40+
return this;
41+
}
3942

4043
/**
4144
* Add expected error(s) for the test command.
@@ -45,75 +48,75 @@ var CommandlineTest = function(command) {
4548
* stackTrace: string, //optional regex
4649
* }
4750
*/
48-
this.expectErrors = function(expectedErrors) {
51+
expectErrors(expectedErrors) {
4952
if (expectedErrors instanceof Array) {
50-
self.expectedErrors_ = self.expectedErrors_.concat(expectedErrors);
53+
this.expectedErrors = this.expectedErrors.concat(expectedErrors);
5154
} else {
52-
self.expectedErrors_.push(expectedErrors);
55+
this.expectedErrors.push(expectedErrors);
5356
}
54-
return self;
55-
};
57+
return this;
58+
}
5659

57-
this.run = function() {
58-
var start = new Date().getTime();
59-
var testOutputPath = 'test_output_' + start + '.tmp';
60-
var output = '';
60+
async run() {
61+
const start = new Date().getTime();
62+
const testOutputPath = `test_output_${start}.tmp`;
63+
let output = '';
6164

62-
var flushAndFail = function(errorMsg) {
65+
const flushAndFail = (errorMsg) => {
6366
process.stdout.write(output);
6467
throw new Error(errorMsg);
6568
};
6669

67-
return q.promise(function(resolve, reject) {
68-
if (!self.assertExitCodeOnly_) {
69-
self.command_ = self.command_ + ' --resultJsonOutputFile ' + testOutputPath;
70-
}
71-
var args = self.command_.split(/\s/);
72-
73-
var test_process;
70+
try {
7471

75-
test_process = child_process.spawn(args[0], args.slice(1));
76-
77-
var processData = function(data) {
78-
process.stdout.write('.');
79-
output += data;
80-
if (self.testLogStream) {
81-
self.testLogStream.write(data);
72+
let exitCode = await new Promise((resolve, reject) => {
73+
if (!this.assertExitCodeOnly) {
74+
this.command = `${this.command} --resultJsonOutputFile ${testOutputPath}`;
8275
}
83-
};
76+
const args = this.command.split(/\s/);
77+
const test_process = child_process.spawn(args[0], args.slice(1));
78+
79+
const processData = (data) => {
80+
process.stdout.write('.');
81+
output += data;
82+
if (this.testLogStream) {
83+
this.testLogStream.write(data);
84+
}
85+
};
8486

85-
test_process.stdout.on('data', processData);
86-
test_process.stderr.on('data', processData);
87+
test_process.stdout.on('data', processData);
88+
test_process.stderr.on('data', processData);
8789

88-
test_process.on('error', function(err) {
89-
reject(err);
90-
});
90+
test_process.on('error', (err) => {
91+
reject(err);
92+
});
9193

92-
test_process.on('exit', function(exitCode) {
93-
resolve(exitCode);
94+
test_process.on('exit', function(exitCode) {
95+
resolve(exitCode);
96+
});
9497
});
95-
}).then(function(exitCode) {
96-
if (self.expectedExitCode_ !== exitCode) {
97-
flushAndFail('expecting exit code: ' + self.expectedExitCode_ +
98+
99+
if (this.expectedExitCode !== exitCode) {
100+
flushAndFail('expecting exit code: ' + this.expectedExitCode +
98101
', actual: ' + exitCode);
99102
}
100103

101-
if (self.testLogStream) {
102-
self.testLogStream.end();
104+
if (this.testLogStream) {
105+
this.testLogStream.end();
103106
}
104107

105108
// Skip the rest if we are only verify exit code.
106109
// Note: we're expecting a file populated by '--resultJsonOutputFile' after
107110
// this point.
108-
if (self.assertExitCodeOnly_) {
111+
if (this.assertExitCodeOnly) {
109112
return;
110113
}
111114

112-
var raw_data = fs.readFileSync(testOutputPath);
113-
var testOutput = JSON.parse(raw_data);
115+
const raw_data = fs.readFileSync(testOutputPath);
116+
const testOutput = JSON.parse(raw_data);
114117

115-
var actualErrors = [];
116-
var duration = 0;
118+
let actualErrors = [];
119+
let duration = 0;
117120
testOutput.forEach(function(specResult) {
118121
duration += specResult.duration;
119122
specResult.assertions.forEach(function(assertion) {
@@ -123,9 +126,9 @@ var CommandlineTest = function(command) {
123126
});
124127
});
125128

126-
self.expectedErrors_.forEach(function(expectedError) {
127-
var found = false;
128-
for (var i = 0; i < actualErrors.length; ++i) {
129+
this.expectedErrors.forEach((expectedError) => {
130+
let found = false;
131+
for (let i = 0; i < actualErrors.length; ++i) {
129132
var actualError = actualErrors[i];
130133

131134
// if expected message is defined and messages don't match
@@ -167,25 +170,25 @@ var CommandlineTest = function(command) {
167170
flushAndFail('failed with ' + actualErrors.length + ' unexpected failures');
168171
}
169172

170-
if (self.expectedMinTestDuration_
171-
&& duration < self.expectedMinTestDuration_) {
173+
if (this.expectedMinTestDuration
174+
&& duration < this.expectedMinTestDuration) {
172175
flushAndFail('expecting test min duration: ' +
173-
self.expectedMinTestDuration_ + ', actual: ' + duration);
176+
this.expectedMinTestDuration + ', actual: ' + duration);
174177
}
175-
if (self.expectedMaxTestDuration_
176-
&& duration > self.expectedMaxTestDuration_) {
178+
if (this.expectedMaxTestDuration
179+
&& duration > this.expectedMaxTestDuration) {
177180
flushAndFail('expecting test max duration: ' +
178-
self.expectedMaxTestDuration_ + ', actual: ' + duration);
181+
this.expectedMaxTestDuration + ', actual: ' + duration);
179182
}
180-
}).fin(function() {
183+
} finally {
181184
try {
182185
fs.unlinkSync(testOutputPath);
183186
} catch (err) {
184187
// don't do anything
185188
}
186-
});
187-
};
188-
};
189+
}
190+
}
191+
}
189192

190193
/**
191194
* Util for running tests and testing functionalities including:
@@ -195,34 +198,36 @@ var CommandlineTest = function(command) {
195198
* For now, this means protractor tests (jasmine/mocha).
196199
*/
197200
exports.Executor = function() {
198-
var tests = [];
199-
this.addCommandlineTest = function(command) {
200-
var test = new CommandlineTest(command);
201+
let tests = [];
202+
this.addCommandlineTest = (command) => {
203+
let test = new CommandlineTest(command);
201204
tests.push(test);
202205
return test;
203206
};
204207

205-
this.execute = function(logFile) {
206-
var failed = false;
207-
208-
(function runTests(i) {
209-
if (i < tests.length) {
210-
console.log('running: ' + tests[i].command_);
208+
this.runTests = async function(i, logFile, failed) {
209+
if (i < tests.length) {
210+
try {
211+
console.log('running: ' + tests[i].command);
211212
if (logFile) {
212213
tests[i].setTestLogFile(logFile);
213214
}
214-
tests[i].run().then(function() {
215-
console.log('\n>>> \033[1;32mpass\033[0m');
216-
}, function(err) {
217-
failed = true;
218-
console.log('\n>>> \033[1;31mfail: ' + err.toString() + '\033[0m');
219-
}).fin(function() {
220-
runTests(i + 1);
221-
}).done();
222-
} else {
223-
console.log('Summary: ' + (failed ? 'fail' : 'pass'));
224-
process.exit(failed ? 1 : 0);
215+
await tests[i].run();
216+
console.log('\n>>> \033[1;32mpass\033[0m');
217+
} catch (err) {
218+
failed = true;
219+
console.log('\n>>> \033[1;31mfail: ' + err.toString() + '\033[0m');
220+
} finally {
221+
this.runTests(i + 1, logFile, failed);
225222
}
226-
}(0));
223+
} else {
224+
console.log('Summary: ' + (failed ? 'fail' : 'pass'));
225+
process.exit(failed ? 1 : 0);
226+
}
227+
};
228+
229+
this.execute = (logFile) => {
230+
let failed = false;
231+
this.runTests(0, logFile, failed);
227232
};
228233
};

0 commit comments

Comments
 (0)