Skip to content

Commit e974b5b

Browse files
committed
test: extract test runner method
1 parent 45b6954 commit e974b5b

File tree

1 file changed

+106
-114
lines changed

1 file changed

+106
-114
lines changed

tests/legacy-cli/e2e_runner.ts

+106-114
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,124 +121,117 @@ 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()]).then(async ([httpPort, httpsPort]) => {
126-
setGlobalVariable('package-registry', 'http://localhost:' + httpPort);
127-
setGlobalVariable('package-secure-registry', 'http://localhost:' + httpsPort);
124+
Promise.all([findFreePort(), findFreePort()])
125+
.then(async ([httpPort, httpsPort]) => {
126+
setGlobalVariable('package-registry', 'http://localhost:' + httpPort);
127+
setGlobalVariable('package-secure-registry', 'http://localhost:' + httpsPort);
128128

129-
const registryProcess = await createNpmRegistry(httpPort, httpPort);
130-
const secureRegistryProcess = await createNpmRegistry(httpPort, httpsPort, true);
129+
let lastTestRun: string | null = null;
131130

132-
return (
133-
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);
131+
// NPM registries for the lifetime of the test execution
132+
const registryProcess = await createNpmRegistry(httpPort, httpPort);
133+
const secureRegistryProcess = await createNpmRegistry(httpPort, httpsPort, true);
134+
135+
try {
136+
for (const [testIndex, test] of testsToRun.entries()) {
137+
await runTest((lastTestRun = test), testIndex);
138+
}
139+
140+
console.log(colors.green('Done.'));
141+
} catch (err) {
142+
console.log('\n');
143+
console.error(colors.red(`Test "${lastTestRun}" failed...`));
144+
console.error(colors.red(err.message));
145+
console.error(colors.red(err.stack));
146+
147+
if (argv.debug) {
148+
console.log(`Current Directory: ${process.cwd()}`);
149+
console.log('Will loop forever while you debug... CTRL-C to quit.');
150+
151+
/* eslint-disable no-constant-condition */
152+
while (1) {
153+
// That's right!
139154
}
155+
}
140156

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-
// Output success vs failure information.
211-
.then(
212-
() => console.log(colors.green('Done.')),
213-
(err) => {
214-
console.log('\n');
215-
console.error(colors.red(`Test "${currentFileName}" failed...`));
216-
console.error(colors.red(err.message));
217-
console.error(colors.red(err.stack));
218-
219-
if (argv.debug) {
220-
console.log(`Current Directory: ${process.cwd()}`);
221-
console.log('Will loop forever while you debug... CTRL-C to quit.');
222-
223-
/* eslint-disable no-constant-condition */
224-
while (1) {
225-
// That's right!
226-
}
227-
}
228-
229-
return Promise.reject(err);
230-
},
231-
)
232-
// Kill the registry processes before exiting.
233-
.finally(() => {
234-
registryProcess.kill();
235-
secureRegistryProcess.kill();
236-
})
237-
.then(
238-
() => process.exit(0),
239-
() => process.exit(1),
240-
)
157+
throw err;
158+
} finally {
159+
registryProcess.kill();
160+
secureRegistryProcess.kill();
161+
}
162+
})
163+
.then(
164+
() => process.exit(0),
165+
() => process.exit(1),
241166
);
242-
});
167+
168+
async function runTest(relativeName: string, testIndex: number) {
169+
// Make sure this is a windows compatible path.
170+
let absoluteName = path.join(e2eRoot, relativeName);
171+
if (/^win/.test(process.platform)) {
172+
absoluteName = absoluteName.replace(/\\/g, path.posix.sep);
173+
}
174+
175+
const currentFileName = relativeName.replace(/\.ts$/, '');
176+
const start = +new Date();
177+
178+
const module = require(absoluteName);
179+
const originalEnvVariables = {
180+
...process.env,
181+
};
182+
183+
const fn: (skipClean?: () => void) => Promise<void> | void =
184+
typeof module == 'function'
185+
? module
186+
: typeof module.default == 'function'
187+
? module.default
188+
: () => {
189+
throw new Error('Invalid test module.');
190+
};
191+
192+
printHeader(currentFileName, testIndex);
193+
194+
let clean = true;
195+
let previousDir = process.cwd();
196+
try {
197+
// Run the test function with the current file on the logStack.
198+
logStack.push(lastLogger().createChild(currentFileName));
199+
try {
200+
await fn(() => (clean = false));
201+
} finally {
202+
logStack.pop();
203+
}
204+
205+
console.log('----');
206+
207+
// If we're not in a setup, change the directory back to where it was before the test.
208+
// This allows tests to chdir without worrying about keeping the original directory.
209+
if (!allSetups.includes(relativeName) && previousDir) {
210+
process.chdir(previousDir);
211+
212+
// Restore env variables before each test.
213+
console.log(' Restoring original environment variables...');
214+
process.env = originalEnvVariables;
215+
}
216+
217+
// Only clean after a real test, not a setup step. Also skip cleaning if the test
218+
// requested an exception.
219+
if (!allSetups.includes(relativeName) && clean) {
220+
logStack.push(new logging.NullLogger());
221+
try {
222+
await gitClean();
223+
} finally {
224+
logStack.pop();
225+
}
226+
}
227+
228+
printFooter(currentFileName, start);
229+
} catch (err) {
230+
printFooter(currentFileName, start);
231+
console.error(err);
232+
throw err;
233+
}
234+
}
243235

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

0 commit comments

Comments
 (0)