Skip to content

Commit 4951c7e

Browse files
committed
test: separate test vs test setup execution
1 parent c4c26a9 commit 4951c7e

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

tests/legacy-cli/e2e_runner.ts

+65-60
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ const tests = allTests.filter((name) => {
9898
});
9999

100100
// Remove tests that are not part of this shard.
101-
const shardedTests = tests.filter((name, i) => shardId === null || i % nbShards == shardId);
102-
const testsToRun = allSetups.concat(shardedTests);
101+
const testsToRun = tests.filter((name, i) => shardId === null || i % nbShards == shardId);
103102

104-
if (shardedTests.length === 0) {
103+
if (testsToRun.length === 0) {
105104
console.log(`No tests would be ran, aborting.`);
106105
process.exit(1);
107106
}
@@ -114,27 +113,26 @@ console.log(testsToRun.join('\n'));
114113
if (testsToRun.length == allTests.length) {
115114
console.log(`Running ${testsToRun.length} tests`);
116115
} else {
117-
console.log(`Running ${testsToRun.length} tests (${allTests.length + allSetups.length} total)`);
116+
console.log(`Running ${testsToRun.length} tests (${allTests.length} total)`);
118117
}
119118

120119
setGlobalVariable('argv', argv);
121120
setGlobalVariable('ci', process.env['CI']?.toLowerCase() === 'true' || process.env['CI'] === '1');
122121
setGlobalVariable('package-manager', argv.yarn ? 'yarn' : 'npm');
123122

123+
let lastTestRun: string | null = null;
124+
124125
Promise.all([findFreePort(), findFreePort()]).then(async ([httpPort, httpsPort]) => {
125126
setGlobalVariable('package-registry', 'http://localhost:' + httpPort);
126127
setGlobalVariable('package-secure-registry', 'http://localhost:' + httpsPort);
127128

128-
let lastTestRun: string | null = null;
129-
130129
try {
131130
// NPM registries for the lifetime of the test execution
132131
const registryProcess = await createNpmRegistry(httpPort, httpPort);
133132
const secureRegistryProcess = await createNpmRegistry(httpPort, httpsPort, true);
134133
try {
135-
for (const [testIndex, test] of testsToRun.entries()) {
136-
await runTest((lastTestRun = test), testIndex);
137-
}
134+
await runSteps(runSetup, allSetups, 'setup');
135+
await runSteps(runTest, testsToRun, 'test');
138136
} finally {
139137
registryProcess.kill();
140138
secureRegistryProcess.kill();
@@ -162,16 +160,43 @@ Promise.all([findFreePort(), findFreePort()]).then(async ([httpPort, httpsPort])
162160
}
163161
});
164162

165-
async function runTest(relativeName: string, testIndex: number) {
166-
// Make sure this is a windows compatible path.
167-
let absoluteName = path.join(e2eRoot, relativeName);
168-
if (/^win/.test(process.platform)) {
169-
absoluteName = absoluteName.replace(/\\/g, path.posix.sep);
163+
async function runSteps(
164+
run: (name: string) => Promise<void> | void,
165+
steps: string[],
166+
type: 'setup' | 'test',
167+
) {
168+
for (const [stepIndex, relativeName] of steps.entries()) {
169+
// Make sure this is a windows compatible path.
170+
let absoluteName = path.join(e2eRoot, relativeName).replace(/\.ts$/, '');
171+
if (/^win/.test(process.platform)) {
172+
absoluteName = absoluteName.replace(/\\/g, path.posix.sep);
173+
}
174+
175+
const name = relativeName.replace(/\.ts$/, '');
176+
const start = Date.now();
177+
178+
printHeader(relativeName, stepIndex, steps.length, type);
179+
180+
// Run the test function with the current file on the logStack.
181+
logStack.push(lastLogger().createChild(absoluteName));
182+
try {
183+
await run((lastTestRun = absoluteName));
184+
} finally {
185+
logStack.pop();
186+
}
187+
188+
console.log('----');
189+
printFooter(name, start);
170190
}
191+
}
192+
193+
async function runSetup(absoluteName: string) {
194+
const module = require(absoluteName);
171195

172-
const currentFileName = relativeName.replace(/\.ts$/, '');
173-
const start = +new Date();
196+
await (typeof module === 'function' ? module : module.default)();
197+
}
174198

199+
async function runTest(absoluteName: string) {
175200
const module = require(absoluteName);
176201
const originalEnvVariables = {
177202
...process.env,
@@ -186,63 +211,43 @@ async function runTest(relativeName: string, testIndex: number) {
186211
throw new Error('Invalid test module.');
187212
};
188213

189-
printHeader(currentFileName, testIndex);
190-
191214
let clean = true;
192215
let previousDir = process.cwd();
193-
try {
194-
// Run the test function with the current file on the logStack.
195-
logStack.push(lastLogger().createChild(currentFileName));
196-
try {
197-
await fn(() => (clean = false));
198-
} finally {
199-
logStack.pop();
200-
}
201216

202-
console.log('----');
217+
await fn(() => (clean = false));
203218

204-
// If we're not in a setup, change the directory back to where it was before the test.
205-
// This allows tests to chdir without worrying about keeping the original directory.
206-
if (!allSetups.includes(relativeName) && previousDir) {
207-
process.chdir(previousDir);
219+
// Change the directory back to where it was before the test.
220+
// This allows tests to chdir without worrying about keeping the original directory.
221+
if (previousDir) {
222+
process.chdir(previousDir);
208223

209-
// Restore env variables before each test.
210-
console.log(' Restoring original environment variables...');
211-
process.env = originalEnvVariables;
212-
}
224+
// Restore env variables before each test.
225+
console.log('Restoring original environment variables...');
226+
process.env = originalEnvVariables;
227+
}
213228

214-
// Only clean after a real test, not a setup step. Also skip cleaning if the test
215-
// requested an exception.
216-
if (!allSetups.includes(relativeName) && clean) {
217-
logStack.push(new logging.NullLogger());
218-
try {
219-
await gitClean();
220-
} finally {
221-
logStack.pop();
222-
}
229+
// Skip cleaning if the test requested an exception.
230+
if (clean) {
231+
logStack.push(new logging.NullLogger());
232+
try {
233+
await gitClean();
234+
} finally {
235+
logStack.pop();
223236
}
224-
225-
printFooter(currentFileName, start);
226-
} catch (err) {
227-
printFooter(currentFileName, start);
228-
console.error(err);
229-
throw err;
230237
}
231238
}
232239

233-
function printHeader(testName: string, testIndex: number) {
234-
const text = `${testIndex + 1} of ${testsToRun.length}`;
235-
const fullIndex =
236-
(testIndex < allSetups.length
237-
? testIndex
238-
: (testIndex - allSetups.length) * nbShards + shardId + allSetups.length) + 1;
239-
const length = tests.length + allSetups.length;
240+
function printHeader(testName: string, testIndex: number, count: number, type: 'setup' | 'test') {
241+
const text = `${testIndex + 1} of ${count}`;
242+
const fullIndex = testIndex * nbShards + shardId + 1;
240243
const shard =
241-
shardId === null
244+
shardId === null || type !== 'test'
242245
? ''
243-
: colors.yellow(` [${shardId}:${nbShards}]` + colors.bold(` (${fullIndex}/${length})`));
246+
: colors.yellow(` [${shardId}:${nbShards}]` + colors.bold(` (${fullIndex}/${tests.length})`));
244247
console.log(
245-
colors.green(`Running "${colors.bold.blue(testName)}" (${colors.bold.white(text)}${shard})...`),
248+
colors.green(
249+
`Running ${type} "${colors.bold.blue(testName)}" (${colors.bold.white(text)}${shard})...`,
250+
),
246251
);
247252
}
248253

0 commit comments

Comments
 (0)