generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcli.test.ts
More file actions
182 lines (158 loc) · 5.03 KB
/
cli.test.ts
File metadata and controls
182 lines (158 loc) · 5.03 KB
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
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { main, parseCliArgs } from '../lib/cli';
let stdoutMock: jest.SpyInstance;
let stderrMock: jest.SpyInstance;
beforeEach(() => {
stdoutMock = jest.spyOn(process.stdout, 'write').mockImplementation(() => {
return true;
});
stderrMock = jest.spyOn(process.stderr, 'write').mockImplementation(() => {
return true;
});
});
afterEach(() => {
stdoutMock.mockReset();
stderrMock.mockReset();
});
afterAll(() => {
stdoutMock.mockRestore();
stderrMock.mockRestore();
});
describe.each([
['cli-wrapper', ['--unstable', 'deprecated-cli-engine']],
['toolkit-lib', ['--unstable', 'toolkit-lib-engine']],
['toolkit-lib', []], // default
])('Test discovery with engine %s', (_engine: string, engineArgs: string[]) => {
const currentCwd = process.cwd();
beforeAll(() => {
process.chdir(path.join(__dirname, '..'));
});
afterAll(() => {
process.chdir(currentCwd);
});
const cli = (args: string[]) => main([...engineArgs, ...args]);
test('find by default pattern', async () => {
await cli(['--list', '--directory=test/test-data']);
// Expect nothing to be found since this directory doesn't contain files with the default pattern
expect(stdoutMock.mock.calls).toEqual([['\n']]);
});
test('find by custom pattern', async () => {
await cli(['--list', '--directory=test/test-data', '--language=javascript', '--test-regex="^xxxxx\.integ-test[12]\.js$"']);
expect(stdoutMock.mock.calls).toEqual([[
[
'xxxxx.integ-test1.js',
'xxxxx.integ-test2.js',
'',
].join('\n'),
]]);
});
test('list only shows explicitly provided tests', async () => {
await cli([
'xxxxx.integ-test1.js',
'xxxxx.integ-test2.js',
'--list',
'--directory=test/test-data',
'--language=javascript',
'--test-regex="^xxxxx\..*\.js$"',
]);
expect(stdoutMock.mock.calls).toEqual([[
[
'xxxxx.integ-test1.js',
'xxxxx.integ-test2.js',
'',
].join('\n'),
]]);
});
test('find only TypeScript files', async () => {
await cli(['--list', '--language', 'typescript', '--directory=test']);
expect(stdoutMock.mock.calls).toEqual([[
'language-tests/integ.typescript-test.ts\n',
]]);
});
test('can run with no tests detected', async () => {
await cli(['whatever.js', '--directory=test/test-data']);
expect(stdoutMock.mock.calls).toEqual([]);
});
test('app and test-regex override default presets', async () => {
await cli([
'--list',
'--directory=test/test-data',
'--app="node {filePath}"',
'--test-regex="^xxxxx\.integ-test[12]\.js$"',
]);
expect(stdoutMock.mock.calls).toEqual([[
[
'xxxxx.integ-test1.js',
'xxxxx.integ-test2.js',
'',
].join('\n'),
]]);
});
test('cannot use --test-regex by itself with more than one language preset', async () => {
await expect(() => cli([
'--list',
'--directory=test/test-data',
'--language=javascript',
'--language=typescript',
'--test-regex="^xxxxx\.integ-test[12]\.js$"',
])).rejects.toThrow('Only a single "--language" can be used with "--test-regex". Alternatively provide both "--app" and "--test-regex" to fully customize the configuration.');
});
test('cannot use --app by itself with more than one language preset', async () => {
await expect(() => cli([
'--list',
'--directory=test/test-data',
'--language=javascript',
'--language=typescript',
'--app="node --prof {filePath}"',
])).rejects.toThrow('Only a single "--language" can be used with "--app". Alternatively provide both "--app" and "--test-regex" to fully customize the configuration.');
});
test('cannot use --strict with --exclude', async () => {
await expect(() => cli([
'xxxxx.integ-test1.js',
'--language=javascript',
'--strict',
'--exclude',
])).rejects.toThrow('Cannot use --strict with --exclude');
});
});
describe('CLI config file', () => {
const configFile = 'integ.config.json';
const withConfig = (settings: any, fileName = configFile) => {
fs.writeFileSync(fileName, JSON.stringify(settings, null, 2), { encoding: 'utf-8' });
};
const currentCwd = process.cwd();
beforeEach(() => {
process.chdir(os.tmpdir());
});
afterEach(() => {
process.chdir(currentCwd);
});
test('options are read from config file', async () => {
// WHEN
withConfig({
list: true,
maxWorkers: 3,
parallelRegions: [
'eu-west-1',
'ap-southeast-2',
],
});
const options = parseCliArgs();
// THEN
expect(options.list).toBe(true);
expect(options.maxWorkers).toBe(3);
expect(options.testRegions).toEqual([
'eu-west-1',
'ap-southeast-2',
]);
});
test('cli options take precedent', async () => {
// WHEN
withConfig({ maxWorkers: 3 });
const options = parseCliArgs(['--max-workers', '20']);
// THEN
expect(options.maxWorkers).toBe(20);
});
});