fix(core): refactor the logging logic in e2e tests (#15548)

This commit is contained in:
Katerina Skroumpelou 2023-03-09 22:13:32 +02:00 committed by GitHub
parent 16ad3086be
commit a0e00c85fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 157 additions and 75 deletions

View File

@ -35,7 +35,7 @@ describe('make-angular-cli-faster', () => {
cwd: tmpProjPath(), cwd: tmpProjPath(),
env: process.env, env: process.env,
encoding: 'utf-8', encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'], stdio: 'pipe',
} }
) )
).not.toThrow(); ).not.toThrow();

View File

@ -27,7 +27,7 @@ describe('nx init (for NestCLI)', () => {
cwd: e2eCwd, cwd: e2eCwd,
encoding: 'utf-8', encoding: 'utf-8',
env: process.env, env: process.env,
stdio: ['pipe', 'pipe', 'pipe'], stdio: 'pipe',
} }
); );
@ -39,7 +39,7 @@ describe('nx init (for NestCLI)', () => {
cwd: projectRoot, cwd: projectRoot,
encoding: 'utf-8', encoding: 'utf-8',
env: process.env, env: process.env,
stdio: ['pipe', 'pipe', 'pipe'], stdio: 'pipe',
} }
); );

View File

@ -1,4 +1,4 @@
import { PackageManager } from '@nrwl/devkit'; import { output, PackageManager } from '@nrwl/devkit';
import { packageInstall, tmpProjPath } from './create-project-utils'; import { packageInstall, tmpProjPath } from './create-project-utils';
import { import {
detectPackageManager, detectPackageManager,
@ -70,9 +70,9 @@ export function runCommand(
): string { ): string {
const { failOnError, ...childProcessOptions } = options ?? {}; const { failOnError, ...childProcessOptions } = options ?? {};
try { try {
const r = execSync(command, { const r = execSync(`${command}${isVerbose() ? ' --verbose' : ''}`, {
cwd: tmpProjPath(), cwd: tmpProjPath(),
stdio: ['pipe', 'pipe', 'pipe'], stdio: 'pipe',
env: { env: {
...getStrippedEnvironmentVariables(), ...getStrippedEnvironmentVariables(),
...childProcessOptions?.env, ...childProcessOptions?.env,
@ -80,16 +80,23 @@ export function runCommand(
}, },
encoding: 'utf-8', encoding: 'utf-8',
...childProcessOptions, ...childProcessOptions,
}).toString(); });
if (process.env.NX_VERBOSE_LOGGING) {
console.log(r); if (isVerbose()) {
output.log({
title: `Command: ${command}`,
bodyLines: [r as string],
color: 'green',
});
} }
return r;
return r as string;
} catch (e) { } catch (e) {
// this is intentional // this is intentional
// npm ls fails if package is not found // npm ls fails if package is not found
logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`);
if (!failOnError && (e.stdout || e.stderr)) { if (!failOnError && (e.stdout || e.stderr)) {
return e.stdout?.toString() + e.stderr?.toString(); return e.stdout + e.stderr;
} }
throw e; throw e;
} }
@ -272,25 +279,33 @@ export function runNgAdd(
try { try {
const pmc = getPackageManagerCommand(); const pmc = getPackageManagerCommand();
packageInstall(packageName, undefined, version); packageInstall(packageName, undefined, version);
return execSync(pmc.run(`ng g ${packageName}:ng-add`, command ?? ''), { const fullCommand = pmc.run(
`ng g ${packageName}:ng-add`,
`${command}${isVerbose() ? ' --verbose' : ''}` ?? ''
);
const result = execSync(fullCommand, {
cwd: tmpProjPath(), cwd: tmpProjPath(),
stdio: 'pipe', stdio: 'pipe',
env: { ...(opts.env || getStrippedEnvironmentVariables()) }, env: { ...(opts.env || getStrippedEnvironmentVariables()) },
encoding: 'utf-8', encoding: 'utf-8',
}) });
.toString()
.replace( const r = stripConsoleColors(result);
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
'' if (isVerbose()) {
); output.log({
title: `Original command: ${fullCommand}`,
bodyLines: [result as string],
color: 'green',
});
}
return r;
} catch (e) { } catch (e) {
if (opts.silenceError) { if (opts.silenceError) {
return e.stdout.toString(); return e.stdout;
} else { } else {
logError( logError(`Ng Add failed: ${command}`, `${e.stdout}\n\n${e.stderr}`);
`Ng Add failed: ${command}`,
`${e.stdout?.toString()}\n\n${e.stderr?.toString()}`
);
throw e; throw e;
} }
} }
@ -305,19 +320,30 @@ export function runCLI(
): string { ): string {
try { try {
const pm = getPackageManagerCommand(); const pm = getPackageManagerCommand();
const logs = execSync(`${pm.runNx} ${command}`, { const logs = execSync(
`${pm.runNx} ${command} ${isVerbose() ? ' --verbose' : ''}`,
{
cwd: opts.cwd || tmpProjPath(), cwd: opts.cwd || tmpProjPath(),
env: { CI: 'true', ...getStrippedEnvironmentVariables(), ...opts.env }, env: {
CI: 'true',
...getStrippedEnvironmentVariables(),
...opts.env,
},
encoding: 'utf-8', encoding: 'utf-8',
stdio: 'pipe', stdio: 'pipe',
maxBuffer: 50 * 1024 * 1024, maxBuffer: 50 * 1024 * 1024,
}); }
const r = stripConsoleColors(logs); );
if (isVerbose()) { if (isVerbose()) {
console.log(logs); output.log({
title: `Original command: ${command}`,
bodyLines: [logs as string],
color: 'green',
});
} }
const r = stripConsoleColors(logs);
const needsMaxWorkers = /g.*(express|nest|node|web|react):app.*/; const needsMaxWorkers = /g.*(express|nest|node|web|react):app.*/;
if (needsMaxWorkers.test(command)) { if (needsMaxWorkers.test(command)) {
setMaxWorkers(); setMaxWorkers();
@ -326,12 +352,9 @@ export function runCLI(
return r; return r;
} catch (e) { } catch (e) {
if (opts.silenceError) { if (opts.silenceError) {
return stripConsoleColors(e.stdout?.toString() + e.stderr?.toString()); return stripConsoleColors(e.stdout + e.stderr);
} else { } else {
logError( logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`);
`Original command: ${command}`,
`${e.stdout?.toString()}\n\n${e.stderr?.toString()}`
);
throw e; throw e;
} }
} }
@ -346,28 +369,35 @@ export function runLernaCLI(
): string { ): string {
try { try {
const pm = getPackageManagerCommand(); const pm = getPackageManagerCommand();
const logs = execSync(`${pm.runLerna} ${command}`, { const fullCommand = `${pm.runLerna} ${command}${
isVerbose() ? ' --verbose' : ''
}`;
const logs = execSync(fullCommand, {
cwd: opts.cwd || tmpProjPath(), cwd: opts.cwd || tmpProjPath(),
env: { CI: 'true', ...(opts.env || getStrippedEnvironmentVariables()) }, env: {
CI: 'true',
...(opts.env || getStrippedEnvironmentVariables()),
},
encoding: 'utf-8', encoding: 'utf-8',
stdio: 'pipe', stdio: 'pipe',
maxBuffer: 50 * 1024 * 1024, maxBuffer: 50 * 1024 * 1024,
}); });
const r = stripConsoleColors(logs);
if (isVerbose()) { if (isVerbose()) {
console.log(logs); output.log({
title: `Original command: ${fullCommand}`,
bodyLines: [logs as string],
color: 'green',
});
} }
const r = stripConsoleColors(logs);
return r; return r;
} catch (e) { } catch (e) {
if (opts.silenceError) { if (opts.silenceError) {
return stripConsoleColors(e.stdout?.toString() + e.stderr?.toString()); return stripConsoleColors(e.stdout + e.stderr);
} else { } else {
logError( logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`);
`Original command: ${command}`,
`${e.stdout?.toString()}\n\n${e.stderr?.toString()}`
);
throw e; throw e;
} }
} }

View File

@ -27,6 +27,7 @@ import {
RunCmdOpts, RunCmdOpts,
runCommand, runCommand,
} from './command-utils'; } from './command-utils';
import { output } from '@nrwl/devkit';
let projName: string; let projName: string;
@ -90,7 +91,7 @@ export function newProject({
projName = name; projName = name;
copySync(`${tmpBackupProjPath()}`, `${tmpProjPath()}`); copySync(`${tmpBackupProjPath()}`, `${tmpProjPath()}`);
if (process.env.NX_VERBOSE_LOGGING == 'true') { if (isVerbose()) {
logInfo(`NX`, `E2E test is creating a project: ${tmpProjPath()}`); logInfo(`NX`, `E2E test is creating a project: ${tmpProjPath()}`);
} }
return projScope; return projScope;
@ -169,13 +170,27 @@ export function runCreateWorkspace(
command += ` ${extraArgs}`; command += ` ${extraArgs}`;
} }
const create = execSync(command, { try {
const create = execSync(`${command}${isVerbose() ? ' --verbose' : ''}`, {
cwd, cwd,
stdio: isVerbose() ? 'inherit' : 'pipe', stdio: 'pipe',
env: { CI: 'true', ...process.env }, env: { CI: 'true', ...process.env },
encoding: 'utf-8', encoding: 'utf-8',
}); });
return create ? create.toString() : '';
if (isVerbose()) {
output.log({
title: `Command: ${command}`,
bodyLines: [create as string],
color: 'green',
});
}
return create;
} catch (e) {
logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`);
throw e;
}
} }
export function runCreatePlugin( export function runCreatePlugin(
@ -212,13 +227,27 @@ export function runCreatePlugin(
command += ` ${extraArgs}`; command += ` ${extraArgs}`;
} }
const create = execSync(command, { try {
const create = execSync(`${command}${isVerbose() ? ' --verbose' : ''}`, {
cwd: e2eCwd, cwd: e2eCwd,
stdio: ['pipe', 'pipe', 'pipe'], stdio: 'pipe',
env: process.env, env: process.env,
encoding: 'utf-8', encoding: 'utf-8',
}); });
return create ? create.toString() : '';
if (isVerbose()) {
output.log({
title: `Command: ${command}`,
bodyLines: [create as string],
color: 'green',
});
}
return create;
} catch (e) {
logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`);
throw e;
}
} }
export function packageInstall( export function packageInstall(
@ -233,16 +262,37 @@ export function packageInstall(
.split(' ') .split(' ')
.map((pgk) => `${pgk}@${version}`) .map((pgk) => `${pgk}@${version}`)
.join(' '); .join(' ');
const command = `${
mode === 'dev' ? pm.addDev : pm.addProd
} ${pkgsWithVersions}${isVerbose() ? ' --verbose' : ''}`;
try {
const install = execSync( const install = execSync(
`${mode === 'dev' ? pm.addDev : pm.addProd} ${pkgsWithVersions}`, `${mode === 'dev' ? pm.addDev : pm.addProd} ${pkgsWithVersions}${
isVerbose() ? ' --verbose' : ''
}`,
{ {
cwd, cwd,
stdio: ['pipe', 'pipe', 'pipe'], stdio: 'pipe',
env: process.env, env: process.env,
encoding: 'utf-8', encoding: 'utf-8',
} }
); );
return install ? install.toString() : '';
if (isVerbose()) {
output.log({
title: `Command: ${command}`,
bodyLines: [install as string],
color: 'green',
});
}
return install;
} catch (e) {
logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`);
throw e;
}
} }
export function runNgNew( export function runNgNew(
@ -259,10 +309,10 @@ export function runNgNew(
return execSync(command, { return execSync(command, {
cwd: e2eCwd, cwd: e2eCwd,
stdio: ['pipe', 'pipe', 'pipe'], stdio: isVerbose() ? 'inherit' : 'pipe',
env: process.env, env: process.env,
encoding: 'utf-8', encoding: 'utf-8',
}).toString(); });
} }
export function newLernaWorkspace({ export function newLernaWorkspace({
@ -290,7 +340,7 @@ export function newLernaWorkspace({
); );
} }
if (process.env.NX_VERBOSE_LOGGING == 'true') { if (isVerbose()) {
logInfo(`NX`, `E2E test has created a lerna workspace: ${tmpProjPath()}`); logInfo(`NX`, `E2E test has created a lerna workspace: ${tmpProjPath()}`);
} }

View File

@ -70,7 +70,9 @@ export function getNpmMajorVersion(): string {
} }
export function getLatestLernaVersion(): string { export function getLatestLernaVersion(): string {
const lernaVersion = execSync(`npm view lerna version`).toString().trim(); const lernaVersion = execSync(`npm view lerna version`, {
encoding: 'utf-8',
}).trim();
return lernaVersion; return lernaVersion;
} }

View File

@ -40,7 +40,7 @@ export function logSuccess(title: string, body?: string) {
* @returns * @returns
*/ */
export function stripConsoleColors(log: string): string { export function stripConsoleColors(log: string): string {
return log.replace( return log?.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
'' ''
); );