forked from angular/protractor-console-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_util.js
225 lines (198 loc) · 6.94 KB
/
test_util.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env node
var child_process = require('child_process'),
q = require('q'),
fs = require('fs');
var CommandlineTest = function(command) {
var self = this;
this.command_ = command;
this.expectedExitCode_ = 0;
this.stdioOnlyOnFailures_ = true;
this.expectedErrors_ = [];
this.assertExitCodeOnly_ = false;
// If stdioOnlyOnFailures_ is true, do not stream stdio unless test failed.
// This is to prevent tests with expected failures from polluting the output.
this.alwaysEnableStdio = function() {
self.stdioOnlyOnFailures_ = false;
return self;
};
// Only assert the exit code and not failures.
// This must be true if the command you're running does not support
// the flag '--resultJsonOutputFile'.
this.assertExitCodeOnly = function() {
self.assertExitCodeOnly_ = true;
return self;
};
// Set the expected exit code for the test command.
this.expectExitCode = function(exitCode) {
self.expectedExitCode_ = exitCode;
return self;
};
// Set the expected total test duration in milliseconds.
this.expectTestDuration = function(min, max) {
self.expectedMinTestDuration_ = min;
self.expectedMaxTestDuration_ = max;
return self;
};
/**
* Add expected error(s) for the test command.
* Input is an object or list of objects of the following form:
* {
* message: string, // optional regex
* stackTrace: string, //optional regex
* }
*/
this.expectErrors = function(expectedErrors) {
if (expectedErrors instanceof Array) {
self.expectedErrors_ = self.expectedErrors_.concat(expectedErrors);
} else {
self.expectedErrors_.push(expectedErrors);
}
return self;
};
this.run = function() {
var start = new Date().getTime();
var testOutputPath = 'test_output_' + start + '.tmp';
var output = '';
var flushAndFail = function(errorMsg) {
process.stdout.write(output);
throw new Error(errorMsg);
};
return q.promise(function(resolve, reject) {
if (!self.assertExitCodeOnly_) {
self.command_ = self.command_ + ' --resultJsonOutputFile ' + testOutputPath;
}
var args = self.command_.split(/\s/);
var test_process;
if (self.stdioOnlyOnFailures_) {
test_process = child_process.spawn(args[0], args.slice(1));
test_process.stdout.on('data', function(data) {
output += data;
});
test_process.stderr.on('data', function(data) {
output += data;
});
} else {
test_process = child_process.spawn(args[0], args.slice(1), {stdio: 'inherit'});
}
test_process.on('error', function(err) {
reject(err);
});
test_process.on('exit', function(exitCode) {
resolve(exitCode);
});
}).then(function(exitCode) {
if (self.expectedExitCode_ !== exitCode) {
flushAndFail('expecting exit code: ' + self.expectedExitCode_ +
', actual: ' + exitCode);
}
// Skip the rest if we are only verify exit code.
// Note: we're expecting a file populated by '--resultJsonOutputFile' after
// this point.
if (self.assertExitCodeOnly_) {
return;
}
var raw_data = fs.readFileSync(testOutputPath);
var testOutput = JSON.parse(raw_data);
var actualErrors = [];
var duration = 0;
testOutput.forEach(function(specResult) {
duration += specResult.duration;
specResult.assertions.forEach(function(assertion) {
if (!assertion.passed) {
actualErrors.push(assertion);
}
});
});
self.expectedErrors_.forEach(function(expectedError) {
var found = false;
for (var i = 0; i < actualErrors.length; ++i) {
var actualError = actualErrors[i];
// if expected message is defined and messages don't match
if (expectedError.message) {
if (!actualError.errorMsg ||
!actualError.errorMsg.match(new RegExp(expectedError.message))) {
continue;
}
}
// if expected stackTrace is defined and stackTraces don't match
if (expectedError.stackTrace) {
if (!actualError.stackTrace ||
!actualError.stackTrace.match(new RegExp(expectedError.stackTrace))) {
continue;
}
}
found = true;
break;
}
if (!found) {
if (expectedError.message && expectedError.stackTrace) {
flushAndFail('did not fail with expected error with message: [' +
expectedError.message + '] and stackTrace: [' +
expectedError.stackTrace + ']');
} else if (expectedError.message) {
flushAndFail('did not fail with expected error with message: [' +
expectedError.message + ']');
} else if (expectedError.stackTrace) {
flushAndFail('did not fail with expected error with stackTrace: [' +
expectedError.stackTrace + ']');
}
} else {
actualErrors.splice(i, 1);
}
});
if (actualErrors.length > 0) {
flushAndFail('failed with ' + actualErrors.length + ' unexpected failures');
}
if (self.expectedMinTestDuration_
&& duration < self.expectedMinTestDuration_) {
flushAndFail('expecting test min duration: ' +
self.expectedMinTestDuration_ + ', actual: ' + duration);
}
if (self.expectedMaxTestDuration_
&& duration > self.expectedMaxTestDuration_) {
flushAndFail('expecting test max duration: ' +
self.expectedMaxTestDuration_ + ', actual: ' + duration);
}
}).fin(function() {
try {
fs.unlinkSync(testOutputPath);
} catch (err) {
// don't do anything
}
});
};
};
/**
* Util for running tests and testing functionalities including:
* exitCode, test durations, expected errors, and expected stackTrace
* Note, this will work with any commandline tests, but only if it supports
* the flag '--resultJsonOutputFile', unless only exitCode is being tested.
* For now, this means protractor tests (jasmine/mocha/cucumber).
*/
exports.Executor = function() {
var tests = [];
this.addCommandlineTest = function(command) {
var test = new CommandlineTest(command);
tests.push(test);
return test;
};
this.execute = function() {
var failed = false;
(function runTests(i) {
if (i < tests.length) {
console.log('running: ' + tests[i].command_);
tests[i].run().then(function() {
console.log('>>> \033[1;32mpass\033[0m');
}, function(err) {
failed = true;
console.log('>>> \033[1;31mfail: ' + err.toString() + '\033[0m');
}).fin(function() {
runTests(i + 1);
}).done();
} else {
console.log('Summary: ' + (failed ? 'fail' : 'pass'));
process.exit(failed ? 1 : 0);
}
}(0));
};
};