Skip to content

Commit 281c69e

Browse files
committed
test: migrate e2e test runner to Node.js styleText API
This commit removes the external 'ansi-colors' dependency from the e2e test runner and its utilities by migrating to the native Node.js 'styleText' API. Changes include: - Removing 'ansi-colors' imports in `tests/legacy-cli/e2e/utils/process.ts` and `tests/legacy-cli/e2e_runner.ts`. - Updating Bazel `BUILD.bazel` files to remove the 'ansi-colors' dependency. - Replacing all `colors.STYLE(...)` calls with `styleText(...)` equivalents. - Adjusting output formatting in `printHeader` and `printFooter` functions in `e2e_runner.ts` for consistency with the new styling.
1 parent 5465262 commit 281c69e

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

tests/legacy-cli/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ ts_project(
1313
],
1414
deps = [
1515
"//:node_modules/@types/node",
16-
"//:node_modules/ansi-colors",
1716
"//:node_modules/fast-glob",
1817
"//packages/angular_devkit/core",
1918
"//packages/angular_devkit/core/node",

tests/legacy-cli/e2e/utils/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ ts_project(
1313
"//:node_modules/@types/jasmine",
1414
"//:node_modules/@types/node",
1515
"//:node_modules/@types/semver",
16-
"//:node_modules/ansi-colors",
1716
"//:node_modules/fast-glob",
1817
"//:node_modules/protractor",
1918
"//:node_modules/semver",

tests/legacy-cli/e2e/utils/process.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import * as ansiColors from 'ansi-colors';
21
import { spawn, SpawnOptions } from 'node:child_process';
32
import * as child_process from 'node:child_process';
43
import { concat, defer, EMPTY, from, lastValueFrom, catchError, repeat } from 'rxjs';
54
import { getGlobalVariable, getGlobalVariablesEnv } from './env';
65
import treeKill from 'tree-kill';
76
import { delimiter, join, resolve } from 'node:path';
8-
import { stripVTControlCharacters } from 'node:util';
7+
import { stripVTControlCharacters, styleText } from 'node:util';
98

109
interface ExecOptions {
1110
silent?: boolean;
@@ -30,9 +29,6 @@ export type ProcessOutput = {
3029
};
3130

3231
function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<ProcessOutput> {
33-
// Create a separate instance to prevent unintended global changes to the color configuration
34-
const colors = ansiColors.create();
35-
3632
const cwd = options.cwd ?? process.cwd();
3733
const env = options.env ?? process.env;
3834

@@ -57,8 +53,10 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
5753
.join(', ')
5854
.replace(/^(.+)$/, ' [$1]'); // Proper formatting.
5955

60-
console.log(colors.blue(`Running \`${cmd} ${args.map((x) => `"${x}"`).join(' ')}\`${flags}...`));
61-
console.log(colors.blue(`CWD: ${cwd}`));
56+
console.log(
57+
styleText(['blue'], `Running \`${cmd} ${args.map((x) => `"${x}"`).join(' ')}\`${flags}...`),
58+
);
59+
console.log(styleText(['blue'], `CWD: ${cwd}`));
6260

6361
const spawnOptions: SpawnOptions = {
6462
cwd,
@@ -123,7 +121,7 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
123121
.toString('utf-8')
124122
.split(/[\n\r]+/)
125123
.filter((line) => line !== '')
126-
.forEach((line) => console.error(colors.yellow(' ' + line)));
124+
.forEach((line) => console.error(styleText(['yellow'], ' ' + line)));
127125
});
128126

129127
childProcess.on('close', (code) => {

tests/legacy-cli/e2e_runner.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { parseArgs } from 'node:util';
1+
import { parseArgs, styleText } from 'node:util';
22
import { createConsoleLogger } from '../../packages/angular_devkit/core/node';
3-
import colors from 'ansi-colors';
43
import glob from 'fast-glob';
54
import * as path from 'node:path';
65
import * as fs from 'node:fs';
@@ -114,9 +113,9 @@ if (process.env.CHROME_BIN) {
114113
const logger = createConsoleLogger(argv.verbose, process.stdout, process.stderr, {
115114
info: (s) => s,
116115
debug: (s) => s,
117-
warn: (s) => colors.bold.yellow(s),
118-
error: (s) => colors.bold.red(s),
119-
fatal: (s) => colors.bold.red(s),
116+
warn: (s) => styleText(['bold', 'yellow'], s),
117+
error: (s) => styleText(['bold', 'red'], s),
118+
fatal: (s) => styleText(['bold', 'red'], s),
120119
});
121120

122121
const logStack = [logger];
@@ -250,21 +249,21 @@ process.env.CHROMEDRIVER_BIN = path.resolve(process.env.CHROMEDRIVER_BIN!);
250249
await runSteps(runTest, testsToRun, 'test');
251250

252251
if (shardId !== null) {
253-
console.log(colors.green(`Done shard ${shardId} of ${nbShards}.`));
252+
console.log(styleText(['green'], `Done shard ${shardId} of ${nbShards}.`));
254253
} else {
255-
console.log(colors.green('Done.'));
254+
console.log(styleText(['green'], 'Done.'));
256255
}
257256

258257
process.exitCode = 0;
259258
} catch (err) {
260259
if (err instanceof Error) {
261260
console.log('\n');
262-
console.error(colors.red(err.message));
261+
console.error(styleText(['red'], err.message));
263262
if (err.stack) {
264-
console.error(colors.red(err.stack));
263+
console.error(styleText(['red'], err.stack));
265264
}
266265
} else {
267-
console.error(colors.red(String(err)));
266+
console.error(styleText(['red'], String(err)));
268267
}
269268

270269
if (argv.debug) {
@@ -281,7 +280,7 @@ process.env.CHROMEDRIVER_BIN = path.resolve(process.env.CHROMEDRIVER_BIN!);
281280
secureRegistryProcess.kill();
282281
}
283282
})().catch((err) => {
284-
console.error(colors.red(`Unkown Error: ${err}`));
283+
console.error(styleText(['red'], `Unkown Error: ${err}`));
285284
process.exitCode = 1;
286285
});
287286

@@ -310,7 +309,7 @@ async function runSteps(
310309
await run(absoluteName);
311310
} catch (e) {
312311
console.log('\n');
313-
console.error(colors.red(`${capsType} "${name}" failed...`));
312+
console.error(styleText(['red'], `${capsType} "${name}" failed...`));
314313

315314
throw e;
316315
} finally {
@@ -363,16 +362,22 @@ function printHeader(
363362
count: number,
364363
type: 'setup' | 'initializer' | 'test',
365364
) {
366-
const text = `${testIndex + 1} of ${count}`;
365+
const text = `(${testIndex + 1} of ${count})`;
367366
const fullIndex = testIndex * nbShards + (shardId ?? 0) + 1;
368367
const shard =
369368
shardId === null || type !== 'test'
370369
? ''
371-
: colors.yellow(` [${shardId}:${nbShards}]` + colors.bold(` (${fullIndex}/${tests.length})`));
370+
: styleText(
371+
['yellow'],
372+
` [${shardId}:${nbShards}]` + styleText(['bold'], ` (${fullIndex}/${tests.length})`),
373+
);
372374
console.log(
373-
colors.green(
374-
`Running ${type} "${colors.bold.blue(testName)}" (${colors.bold.white(text)}${shard})...`,
375-
),
375+
styleText(['green'], `Running ${type} "`) +
376+
styleText(['bold', 'blue'], testName) +
377+
styleText(['green'], '" ') +
378+
styleText(['bold', 'white'], text) +
379+
shard +
380+
styleText(['green'], '...'),
376381
);
377382
}
378383

@@ -382,9 +387,11 @@ function printFooter(testName: string, type: 'setup' | 'initializer' | 'test', s
382387
// Round to hundredth of a second.
383388
const t = Math.round((Date.now() - startTime) / 10) / 100;
384389
console.log(
385-
colors.green(`${capsType} "${colors.bold.blue(testName)}" took `) +
386-
colors.bold.blue('' + t) +
387-
colors.green('s...'),
390+
styleText(['green'], `${capsType} "`) +
391+
styleText(['bold', 'blue'], testName) +
392+
styleText(['green'], '" took ') +
393+
styleText(['bold', 'blue'], t.toFixed(2)) +
394+
styleText(['green'], 's...'),
388395
);
389396
console.log('');
390397
}

0 commit comments

Comments
 (0)