Skip to content

Commit ae891e7

Browse files
committed
test: extract test runner method
1 parent 1c8f0a6 commit ae891e7

File tree

1 file changed

+102
-109
lines changed

1 file changed

+102
-109
lines changed

tests/legacy-cli/e2e_runner.ts

+102-109
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ function lastLogger() {
7070
}
7171

7272
const testGlob = argv.glob || 'tests/**/*.ts';
73-
let currentFileName = null;
7473

7574
const e2eRoot = path.join(__dirname, 'e2e');
7675
const allSetups = glob.sync('setup/**/*.ts', { nodir: true, cwd: e2eRoot }).sort();
@@ -122,120 +121,114 @@ setGlobalVariable('argv', argv);
122121
setGlobalVariable('ci', process.env['CI']?.toLowerCase() === 'true' || process.env['CI'] === '1');
123122
setGlobalVariable('package-manager', argv.yarn ? 'yarn' : 'npm');
124123

125-
Promise.all([findFreePort(), findFreePort()])
126-
.then(async ([httpPort, httpsPort]) => {
127-
setGlobalVariable('package-registry', 'http://localhost:' + httpPort);
128-
setGlobalVariable('package-secure-registry', 'http://localhost:' + httpsPort);
124+
Promise.all([findFreePort(), findFreePort()]).then(async ([httpPort, httpsPort]) => {
125+
setGlobalVariable('package-registry', 'http://localhost:' + httpPort);
126+
setGlobalVariable('package-secure-registry', 'http://localhost:' + httpsPort);
129127

128+
let lastTestRun: string | null = null;
129+
130+
try {
131+
// NPM registries for the lifetime of the test execution
130132
const registryProcess = await createNpmRegistry(httpPort, httpPort);
131133
const secureRegistryProcess = await createNpmRegistry(httpPort, httpsPort, true);
134+
try {
135+
for (const [testIndex, test] of testsToRun.entries()) {
136+
await runTest((lastTestRun = test), testIndex);
137+
}
138+
} finally {
139+
registryProcess.kill();
140+
secureRegistryProcess.kill();
141+
}
142+
143+
console.log(colors.green('Done.'));
144+
process.exit(0);
145+
} catch (err) {
146+
console.log('\n');
147+
console.error(colors.red(`Test "${lastTestRun}" failed...`));
148+
console.error(colors.red(err.message));
149+
console.error(colors.red(err.stack));
132150

133-
return testsToRun
134-
.reduce((previous, relativeName, testIndex) => {
135-
// Make sure this is a windows compatible path.
136-
let absoluteName = path.join(e2eRoot, relativeName);
137-
if (/^win/.test(process.platform)) {
138-
absoluteName = absoluteName.replace(/\\/g, path.posix.sep);
139-
}
140-
141-
return previous.then(() => {
142-
currentFileName = relativeName.replace(/\.ts$/, '');
143-
const start = +new Date();
144-
145-
const module = require(absoluteName);
146-
const originalEnvVariables = {
147-
...process.env,
148-
};
149-
150-
const fn: (skipClean?: () => void) => Promise<void> | void =
151-
typeof module == 'function'
152-
? module
153-
: typeof module.default == 'function'
154-
? module.default
155-
: () => {
156-
throw new Error('Invalid test module.');
157-
};
158-
159-
let clean = true;
160-
let previousDir = null;
161-
162-
return Promise.resolve()
163-
.then(() => printHeader(currentFileName, testIndex))
164-
.then(() => (previousDir = process.cwd()))
165-
.then(() => logStack.push(lastLogger().createChild(currentFileName)))
166-
.then(() => fn(() => (clean = false)))
167-
.then(
168-
() => logStack.pop(),
169-
(err) => {
170-
logStack.pop();
171-
throw err;
172-
},
173-
)
174-
.then(() => console.log('----'))
175-
.then(() => {
176-
// If we're not in a setup, change the directory back to where it was before the test.
177-
// This allows tests to chdir without worrying about keeping the original directory.
178-
if (!allSetups.includes(relativeName) && previousDir) {
179-
process.chdir(previousDir);
180-
181-
// Restore env variables before each test.
182-
console.log(' Restoring original environment variables...');
183-
process.env = originalEnvVariables;
184-
}
185-
})
186-
.then(() => {
187-
// Only clean after a real test, not a setup step. Also skip cleaning if the test
188-
// requested an exception.
189-
if (!allSetups.includes(relativeName) && clean) {
190-
logStack.push(new logging.NullLogger());
191-
return gitClean().then(
192-
() => logStack.pop(),
193-
(err) => {
194-
logStack.pop();
195-
throw err;
196-
},
197-
);
198-
}
199-
})
200-
.then(
201-
() => printFooter(currentFileName, start),
202-
(err) => {
203-
printFooter(currentFileName, start);
204-
console.error(err);
205-
throw err;
206-
},
207-
);
208-
});
209-
}, Promise.resolve())
210-
.finally(() => {
211-
registryProcess.kill();
212-
secureRegistryProcess.kill();
213-
});
214-
})
215-
.then(
216-
() => {
217-
console.log(colors.green('Done.'));
218-
process.exit(0);
219-
},
220-
(err) => {
221-
console.log('\n');
222-
console.error(colors.red(`Test "${currentFileName}" failed...`));
223-
console.error(colors.red(err.message));
224-
console.error(colors.red(err.stack));
225-
226-
if (argv.debug) {
227-
console.log(`Current Directory: ${process.cwd()}`);
228-
console.log('Will loop forever while you debug... CTRL-C to quit.');
229-
230-
/* eslint-disable no-constant-condition */
231-
while (1) {
232-
// That's right!
233-
}
151+
if (argv.debug) {
152+
console.log(`Current Directory: ${process.cwd()}`);
153+
console.log('Will loop forever while you debug... CTRL-C to quit.');
154+
155+
/* eslint-disable no-constant-condition */
156+
while (1) {
157+
// That's right!
234158
}
159+
}
235160

236-
process.exit(1);
237-
},
238-
);
161+
process.exit(1);
162+
}
163+
});
164+
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);
170+
}
171+
172+
const currentFileName = relativeName.replace(/\.ts$/, '');
173+
const start = +new Date();
174+
175+
const module = require(absoluteName);
176+
const originalEnvVariables = {
177+
...process.env,
178+
};
179+
180+
const fn: (skipClean?: () => void) => Promise<void> | void =
181+
typeof module == 'function'
182+
? module
183+
: typeof module.default == 'function'
184+
? module.default
185+
: () => {
186+
throw new Error('Invalid test module.');
187+
};
188+
189+
printHeader(currentFileName, testIndex);
190+
191+
let clean = true;
192+
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+
}
201+
202+
console.log('----');
203+
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);
208+
209+
// Restore env variables before each test.
210+
console.log(' Restoring original environment variables...');
211+
process.env = originalEnvVariables;
212+
}
213+
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+
}
223+
}
224+
225+
printFooter(currentFileName, start);
226+
} catch (err) {
227+
printFooter(currentFileName, start);
228+
console.error(err);
229+
throw err;
230+
}
231+
}
239232

240233
function printHeader(testName: string, testIndex: number) {
241234
const text = `${testIndex + 1} of ${testsToRun.length}`;

0 commit comments

Comments
 (0)