-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
164 lines (151 loc) · 3.98 KB
/
index.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
const assert = require('node:assert');
const { exec } = require('node:child_process');
const { existsSync, readFileSync } = require('node:fs');
const { basename, resolve, sep } = require('node:path');
const process = require('node:process');
const { copySync, removeSync, statSync } = require('fs-extra');
const {
normaliseOutput,
runTestSuiteWithSamples,
assertDirectoriesAreEqual
} = require('../utils.js');
const cwd = process.cwd();
removeSync(resolve(__dirname, 'node_modules'));
copySync(resolve(__dirname, 'node_modules_rename_me'), resolve(__dirname, 'node_modules'));
runTestSuiteWithSamples(
'cli',
resolve(__dirname, 'samples'),
/**
* @param {import('../types').TestConfigCli} config
*/
(directory, config) => {
(config.skip ? it.skip : config.solo ? it.only : it)(
basename(directory) + ': ' + config.description,
async () => {
process.chdir(config.cwd || directory);
const command = config.command.replace(
/(^| )rollup($| )/g,
`node ${resolve(__dirname, '../../dist/bin')}${sep}rollup `
);
try {
await runTest(config, command);
} catch (error) {
if (config.retry) {
console.error('Initial test run failed: ' + error.message + '\nRETRYING...\n');
return runTest(config, command);
}
throw error;
}
}
).timeout(80_000);
},
() => process.chdir(cwd)
);
async function runTest(config, command) {
if (config.before) {
await config.before();
}
return new Promise((resolve, reject) => {
const childProcess = exec(
command,
{
timeout: 40_000,
env: { ...process.env, FORCE_COLOR: '0', ...config.env },
killSignal: 'SIGKILL'
},
async (error, code, stderr) => {
if (config.after) {
await config.after(error, code, stderr);
}
if (error && !error.killed) {
if (config.error) {
if (!config.error(error)) {
return resolve();
}
} else {
return reject(error);
}
}
if (childProcess.signalCode === 'SIGKILL') {
return reject(new Error('Test aborted due to timeout.'));
}
if ('stderr' in config) {
const shouldContinue = config.stderr(stderr);
if (!shouldContinue) return resolve();
} else if (stderr) {
console.error(stderr);
}
let unintendedError;
if (config.execute) {
try {
const function_ = new Function('require', 'module', 'exports', 'assert', code);
const module = {
exports: {}
};
function_(require, module, module.exports, assert);
if (config.error) {
unintendedError = new Error('Expected an error while executing output');
}
if (config.exports) {
await config.exports(module.exports);
}
} catch (error) {
if (config.error) {
config.error(error);
} else {
unintendedError = error;
}
}
if (config.show || unintendedError) {
console.log(code + '\n\n\n');
}
if (config.solo) console.groupEnd();
return unintendedError ? reject(unintendedError) : resolve();
}
if (config.result) {
try {
config.result(code);
return resolve();
} catch (error) {
return reject(error);
}
}
if (config.test) {
try {
config.test();
return resolve();
} catch (error) {
return reject(error);
}
}
if (existsSync('_expected') && statSync('_expected').isDirectory()) {
try {
assertDirectoriesAreEqual('_actual', '_expected');
return resolve();
} catch (error) {
return reject(error);
}
}
const expected = readFileSync('_expected.js', 'utf8');
try {
assert.equal(normaliseOutput(code), normaliseOutput(expected));
return resolve();
} catch (error) {
return reject(error);
}
}
);
childProcess.stderr.on('data', async data => {
if (config.abortOnStderr) {
try {
if (await config.abortOnStderr(data)) {
childProcess.kill('SIGTERM');
}
} catch (error) {
childProcess.kill('SIGTERM');
reject(error);
}
}
});
});
}