Skip to content

Commit df2ca20

Browse files
committed
test: extract test runner method
1 parent 7b5de2c commit df2ca20

File tree

1 file changed

+110
-113
lines changed

1 file changed

+110
-113
lines changed

tests/legacy-cli/e2e_runner.ts

+110-113
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,118 @@ 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);
129-
130-
const registryProcess = await createNpmRegistry(httpPort, httpPort);
131-
const secureRegistryProcess = await createNpmRegistry(httpPort, httpsPort, true);
132-
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-
}
124+
Promise.all([findFreePort(), findFreePort()]).then(async ([httpPort, httpsPort]) => {
125+
setGlobalVariable('package-registry', 'http://localhost:' + httpPort);
126+
setGlobalVariable('package-secure-registry', 'http://localhost:' + httpsPort);
127+
128+
const registryProcess = await createNpmRegistry(httpPort, httpPort);
129+
const secureRegistryProcess = await createNpmRegistry(httpPort, httpsPort, true);
130+
131+
let lastTestRun: string | null = null;
132+
133+
try {
134+
for (const [testIndex, test] of testsToRun.entries()) {
135+
await runTest((lastTestRun = test), testIndex);
136+
}
137+
138+
console.log(colors.green('Done.'));
139+
process.exit(0);
140+
} catch (err) {
141+
console.log('\n');
142+
console.error(colors.red(`Test "${lastTestRun}" failed...`));
143+
console.error(colors.red(err.message));
144+
console.error(colors.red(err.stack));
145+
146+
if (argv.debug) {
147+
console.log(`Current Directory: ${process.cwd()}`);
148+
console.log('Will loop forever while you debug... CTRL-C to quit.');
149+
150+
/* eslint-disable no-constant-condition */
151+
while (1) {
152+
// That's right!
234153
}
154+
}
235155

236-
process.exit(1);
237-
},
238-
);
156+
process.exit(1);
157+
} finally {
158+
registryProcess.kill();
159+
secureRegistryProcess.kill();
160+
}
161+
});
162+
163+
function normalizeTestStep(relativeName: string) {
164+
// Make sure this is a windows compatible path.
165+
let absoluteName = path.join(e2eRoot, relativeName);
166+
if (/^win/.test(process.platform)) {
167+
absoluteName = absoluteName.replace(/\\/g, path.posix.sep);
168+
}
169+
170+
const currentFileName = relativeName.replace(/\.ts$/, '');
171+
172+
return { absoluteName, currentFileName };
173+
}
174+
175+
async function runTest(relativeName: string, testIndex: number) {
176+
const { absoluteName, currentFileName } = normalizeTestStep(relativeName);
177+
const start = +new Date();
178+
179+
const module = require(absoluteName);
180+
const originalEnvVariables = {
181+
...process.env,
182+
};
183+
184+
const fn: (skipClean?: () => void) => Promise<void> | void =
185+
typeof module == 'function'
186+
? module
187+
: typeof module.default == 'function'
188+
? module.default
189+
: () => {
190+
throw new Error('Invalid test module.');
191+
};
192+
193+
printHeader(currentFileName, testIndex);
194+
195+
let clean = true;
196+
let previousDir = process.cwd();
197+
try {
198+
// Run the test function with the current file on the logStack.
199+
logStack.push(lastLogger().createChild(currentFileName));
200+
try {
201+
await fn(() => (clean = false));
202+
} finally {
203+
logStack.pop();
204+
}
205+
206+
console.log('----');
207+
208+
// If we're not in a setup, change the directory back to where it was before the test.
209+
// This allows tests to chdir without worrying about keeping the original directory.
210+
if (!allSetups.includes(relativeName) && previousDir) {
211+
process.chdir(previousDir);
212+
213+
// Restore env variables before each test.
214+
console.log(' Restoring original environment variables...');
215+
process.env = originalEnvVariables;
216+
}
217+
218+
// Only clean after a real test, not a setup step. Also skip cleaning if the test
219+
// requested an exception.
220+
if (!allSetups.includes(relativeName) && clean) {
221+
logStack.push(new logging.NullLogger());
222+
try {
223+
await gitClean();
224+
} finally {
225+
logStack.pop();
226+
}
227+
}
228+
229+
printFooter(currentFileName, start);
230+
} catch (err) {
231+
printFooter(currentFileName, start);
232+
console.error(err);
233+
throw err;
234+
}
235+
}
239236

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

0 commit comments

Comments
 (0)