-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathmain_spec.ts
204 lines (182 loc) · 6.71 KB
/
main_spec.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { existsSync, mkdtempSync, readFileSync, realpathSync, unlinkSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { basename, dirname, join } from 'path';
import { main } from './main';
// We only care about the write method in these mocks of NodeJS.WriteStream.
class MockWriteStream {
lines: string[] = [];
write(str: string) {
// Strip color control characters.
this.lines.push(str.replace(/[^\x20-\x7F]\[\d+m/g, ''));
return true;
}
}
describe('benchmark binary', () => {
const benchmarkScript = require.resolve(join(__dirname, './test/fibonacci.js'));
const exitCodeOneScript = require.resolve(join(__dirname, './test/exit-code-one.js'));
const benchmarkWatchScript = require.resolve(join(__dirname, './test/watch-test-cmd.js'));
const watchTriggerScript = require.resolve(join(__dirname, './test/watch-test-script.js'));
const outputFileRoot = mkdtempSync(join(realpathSync(tmpdir()), 'benchmark-binary-spec-'));
const outputFile = join(outputFileRoot, 'output.log');
let stdout: MockWriteStream, stderr: MockWriteStream;
beforeEach(() => {
stdout = new MockWriteStream();
stderr = new MockWriteStream();
});
afterEach(() => {
if (existsSync(outputFile)) {
unlinkSync(outputFile);
}
});
it('works', async () => {
const args = ['--', 'node', benchmarkScript, '30'];
const res = await main({ args, stdout, stderr });
expect(stdout.lines).toContain('[benchmark] Process Stats\n');
expect(res).toEqual(0);
});
it('fails with no command', async () => {
const args: string[] = [];
const res = await main({ args, stdout, stderr });
expect(stderr.lines).toContain('[benchmark] Missing command, see benchmark --help for help.\n');
expect(res).toEqual(1);
});
it('fails with when exit code is not expected', async () => {
const args: string[] = ['--', 'node', exitCodeOneScript];
const res = await main({ args, stdout, stderr });
expect(stderr.lines).toContain(
'[benchmark] Maximum number of retries (5) for command was exceeded.\n',
);
expect(res).toEqual(1);
});
it('prints help', async () => {
const args = ['--help'];
const res = await main({ args, stdout, stderr });
// help is a multiline write.
expect(stdout.lines[0]).toContain('Options:\n');
expect(res).toEqual(0);
});
it('uses verbose', async () => {
const args = ['--verbose', '--', 'node', benchmarkScript, '30'];
const res = await main({ args, stdout, stderr });
expect(stdout.lines).toContain('[benchmark] Run #1: finished successfully\n');
expect(res).toEqual(0);
});
it('uses exit code', async () => {
const args = ['--exit-code', '1', '--', 'node', exitCodeOneScript];
const res = await main({ args, stdout, stderr });
expect(stdout.lines).toContain('[benchmark] Process Stats\n');
expect(res).toEqual(0);
});
it('uses iterations', async () => {
const args = ['--iterations', '3', '--', 'node', benchmarkScript, '30'];
const res = await main({ args, stdout, stderr });
expect(stdout.lines).toContain(
'[benchmark] Benchmarking process over 3 iterations, with up to 5 retries.\n',
);
expect(res).toEqual(0);
});
it('uses retries', async () => {
const args = ['--retries', '3', '--', 'node', benchmarkScript, '30'];
const res = await main({ args, stdout, stderr });
expect(stdout.lines).toContain(
'[benchmark] Benchmarking process over 5 iterations, with up to 3 retries.\n',
);
expect(res).toEqual(0);
});
it('uses cwd', async () => {
const args = ['--cwd', dirname(benchmarkScript), '--', 'node', basename(benchmarkScript), '30'];
const res = await main({ args, stdout, stderr });
expect(stdout.lines).toContain('[benchmark] Process Stats\n');
expect(res).toEqual(0);
});
it('uses output-file', async () => {
const args = ['--output-file', outputFile, '--', 'node', benchmarkScript, '30'];
const res = await main({ args, stdout, stderr });
expect(res).toEqual(0);
expect(existsSync(outputFile)).toBe(true, 'outputFile exists');
expect(readFileSync(outputFile, 'utf-8')).toContain('[benchmark] Process Stats');
});
it('appends to output-file', async () => {
writeFileSync(outputFile, 'existing line');
const args = ['--output-file', outputFile, '--', 'node', benchmarkScript, '30'];
const res = await main({ args, stdout, stderr });
expect(res).toEqual(0);
expect(existsSync(outputFile)).toBe(true, 'outputFile exists');
expect(readFileSync(outputFile, 'utf-8')).toContain('existing line');
});
it('overwrites output-file', async () => {
writeFileSync(outputFile, 'existing line');
const args = [
'--output-file',
outputFile,
'--overwrite-output-file',
'--',
'node',
benchmarkScript,
'30',
];
const res = await main({ args, stdout, stderr });
expect(res).toEqual(0);
expect(existsSync(outputFile)).toBe(true, 'outputFile exists');
expect(readFileSync(outputFile, 'utf-8')).not.toContain('existing line');
});
it('uses prefix', async () => {
const args = ['--prefix', '[abc]', '--', 'node', benchmarkScript, '30'];
const res = await main({ args, stdout, stderr });
stdout.lines.forEach((line) => expect(line).toMatch(/^\[abc\]/));
expect(res).toEqual(0);
});
it('uses watch-script and watch-matcher', async () => {
const args = [
'--watch-matcher',
'Complete',
'--watch-script',
watchTriggerScript,
'--',
'node',
benchmarkWatchScript,
];
const res = await main({ args, stdout, stderr });
expect(stdout.lines).toContain('[benchmark] Process Stats\n');
expect(res).toEqual(0);
}, 30000);
it('should not fail with exit code', async () => {
const args = [
'--watch-matcher',
'Complete',
'--watch-script',
watchTriggerScript,
'--',
'node',
exitCodeOneScript,
];
const res = await main({ args, stdout, stderr });
expect(stderr.lines).toContain(
'[benchmark] Maximum number of retries (5) for command was exceeded.\n',
);
expect(res).toEqual(1);
});
it('should error when watch-timeout is exceeded', async () => {
const args = [
'--watch-timeout',
'250',
'--watch-matcher',
'Wrong Match',
'--watch-script',
watchTriggerScript,
'--',
'node',
benchmarkWatchScript,
];
const res = await main({ args, stdout, stderr });
expect(stderr.lines).toContain('[benchmark] Timeout has occurred\n');
expect(res).toEqual(1);
}, 30000);
});