From af0b3d7ab05727f21e2ba9efea06748c27771cfc Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Thu, 9 Mar 2023 13:28:29 +0200 Subject: [PATCH] fix(core): refactor the logging logic in e2e tests --- .../src/make-angular-cli-faster.test.ts | 2 +- e2e/nx-init/src/nx-init-nest.test.ts | 4 +- e2e/utils/command-utils.ts | 117 +++++++++++------- e2e/utils/create-project-utils.ts | 104 ++++++++++++---- e2e/utils/get-env-info.ts | 4 +- e2e/utils/log-utils.ts | 2 +- 6 files changed, 158 insertions(+), 75 deletions(-) diff --git a/e2e/make-angular-cli-faster/src/make-angular-cli-faster.test.ts b/e2e/make-angular-cli-faster/src/make-angular-cli-faster.test.ts index 72856820c5e3bc..75035d78bd6b0a 100644 --- a/e2e/make-angular-cli-faster/src/make-angular-cli-faster.test.ts +++ b/e2e/make-angular-cli-faster/src/make-angular-cli-faster.test.ts @@ -35,7 +35,7 @@ describe('make-angular-cli-faster', () => { cwd: tmpProjPath(), env: process.env, encoding: 'utf-8', - stdio: ['pipe', 'pipe', 'pipe'], + stdio: 'pipe', } ) ).not.toThrow(); diff --git a/e2e/nx-init/src/nx-init-nest.test.ts b/e2e/nx-init/src/nx-init-nest.test.ts index 9e9811092fd8ed..7aac2737f61736 100644 --- a/e2e/nx-init/src/nx-init-nest.test.ts +++ b/e2e/nx-init/src/nx-init-nest.test.ts @@ -27,7 +27,7 @@ describe('nx init (for NestCLI)', () => { cwd: e2eCwd, encoding: 'utf-8', env: process.env, - stdio: ['pipe', 'pipe', 'pipe'], + stdio: 'pipe', } ); @@ -39,7 +39,7 @@ describe('nx init (for NestCLI)', () => { cwd: projectRoot, encoding: 'utf-8', env: process.env, - stdio: ['pipe', 'pipe', 'pipe'], + stdio: 'pipe', } ); diff --git a/e2e/utils/command-utils.ts b/e2e/utils/command-utils.ts index 39e9b7c2711635..2cf660eddc69a1 100644 --- a/e2e/utils/command-utils.ts +++ b/e2e/utils/command-utils.ts @@ -1,4 +1,4 @@ -import { PackageManager } from '@nrwl/devkit'; +import { output, PackageManager } from '@nrwl/devkit'; import { packageInstall, tmpProjPath } from './create-project-utils'; import { detectPackageManager, @@ -16,6 +16,7 @@ import { Workspaces } from '../../packages/nx/src/config/workspaces'; import { isVerbose } from './get-env-info'; import { updateFile } from './file-utils'; import { logError, logInfo, logSuccess, stripConsoleColors } from './log-utils'; +import * as chalk from 'chalk'; export const kill = require('kill-port'); @@ -70,9 +71,9 @@ export function runCommand( ): string { const { failOnError, ...childProcessOptions } = options ?? {}; try { - const r = execSync(command, { + const r = execSync(`${command}${isVerbose() ? ' --verbose' : ''}`, { cwd: tmpProjPath(), - stdio: ['pipe', 'pipe', 'pipe'], + stdio: 'pipe', env: { ...getStrippedEnvironmentVariables(), ...childProcessOptions?.env, @@ -80,16 +81,23 @@ export function runCommand( }, encoding: 'utf-8', ...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) { // this is intentional // npm ls fails if package is not found + logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`); if (!failOnError && (e.stdout || e.stderr)) { - return e.stdout?.toString() + e.stderr?.toString(); + return e.stdout + e.stderr; } throw e; } @@ -272,25 +280,33 @@ export function runNgAdd( try { const pmc = getPackageManagerCommand(); 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(), stdio: 'pipe', env: { ...(opts.env || getStrippedEnvironmentVariables()) }, encoding: 'utf-8', - }) - .toString() - .replace( - /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, - '' - ); + })?.replace( + /[\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 result; } catch (e) { if (opts.silenceError) { - return e.stdout.toString(); + return e.stdout; } else { - logError( - `Ng Add failed: ${command}`, - `${e.stdout?.toString()}\n\n${e.stderr?.toString()}` - ); + logError(`Ng Add failed: ${command}`, `${e.stdout}\n\n${e.stderr}`); throw e; } } @@ -305,19 +321,30 @@ export function runCLI( ): string { try { const pm = getPackageManagerCommand(); - const logs = execSync(`${pm.runNx} ${command}`, { - cwd: opts.cwd || tmpProjPath(), - env: { CI: 'true', ...getStrippedEnvironmentVariables(), ...opts.env }, - encoding: 'utf-8', - stdio: 'pipe', - maxBuffer: 50 * 1024 * 1024, - }); - const r = stripConsoleColors(logs); + const logs = execSync( + `${pm.runNx} ${command} ${isVerbose() ? ' --verbose' : ''}`, + { + cwd: opts.cwd || tmpProjPath(), + env: { + CI: 'true', + ...getStrippedEnvironmentVariables(), + ...opts.env, + }, + encoding: 'utf-8', + stdio: 'pipe', + maxBuffer: 50 * 1024 * 1024, + } + ); 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.*/; if (needsMaxWorkers.test(command)) { setMaxWorkers(); @@ -326,12 +353,9 @@ export function runCLI( return r; } catch (e) { if (opts.silenceError) { - return stripConsoleColors(e.stdout?.toString() + e.stderr?.toString()); + return stripConsoleColors(e.stdout + e.stderr); } else { - logError( - `Original command: ${command}`, - `${e.stdout?.toString()}\n\n${e.stderr?.toString()}` - ); + logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`); throw e; } } @@ -346,28 +370,35 @@ export function runLernaCLI( ): string { try { const pm = getPackageManagerCommand(); - const logs = execSync(`${pm.runLerna} ${command}`, { + const fullCommand = `${pm.runLerna} ${command}${ + isVerbose() ? ' --verbose' : '' + }`; + const logs = execSync(fullCommand, { cwd: opts.cwd || tmpProjPath(), - env: { CI: 'true', ...(opts.env || getStrippedEnvironmentVariables()) }, + env: { + CI: 'true', + ...(opts.env || getStrippedEnvironmentVariables()), + }, encoding: 'utf-8', stdio: 'pipe', maxBuffer: 50 * 1024 * 1024, }); - const r = stripConsoleColors(logs); if (isVerbose()) { - console.log(logs); + output.log({ + title: `Original command: ${fullCommand}`, + bodyLines: [logs as string], + color: 'green', + }); } + const r = stripConsoleColors(logs); return r; } catch (e) { if (opts.silenceError) { - return stripConsoleColors(e.stdout?.toString() + e.stderr?.toString()); + return stripConsoleColors(e.stdout + e.stderr); } else { - logError( - `Original command: ${command}`, - `${e.stdout?.toString()}\n\n${e.stderr?.toString()}` - ); + logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`); throw e; } } diff --git a/e2e/utils/create-project-utils.ts b/e2e/utils/create-project-utils.ts index 50e5e942f0c715..59f3522d55429b 100644 --- a/e2e/utils/create-project-utils.ts +++ b/e2e/utils/create-project-utils.ts @@ -27,6 +27,7 @@ import { RunCmdOpts, runCommand, } from './command-utils'; +import { output } from '@nrwl/devkit'; let projName: string; @@ -90,7 +91,7 @@ export function newProject({ projName = name; copySync(`${tmpBackupProjPath()}`, `${tmpProjPath()}`); - if (process.env.NX_VERBOSE_LOGGING == 'true') { + if (isVerbose()) { logInfo(`NX`, `E2E test is creating a project: ${tmpProjPath()}`); } return projScope; @@ -169,13 +170,27 @@ export function runCreateWorkspace( command += ` ${extraArgs}`; } - const create = execSync(command, { - cwd, - stdio: isVerbose() ? 'inherit' : 'pipe', - env: { CI: 'true', ...process.env }, - encoding: 'utf-8', - }); - return create ? create.toString() : ''; + try { + const create = execSync(`${command}${isVerbose() ? ' --verbose' : ''}`, { + cwd, + stdio: 'pipe', + env: { CI: 'true', ...process.env }, + encoding: 'utf-8', + }); + + 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( @@ -212,13 +227,27 @@ export function runCreatePlugin( command += ` ${extraArgs}`; } - const create = execSync(command, { - cwd: e2eCwd, - stdio: ['pipe', 'pipe', 'pipe'], - env: process.env, - encoding: 'utf-8', - }); - return create ? create.toString() : ''; + try { + const create = execSync(`${command}${isVerbose() ? ' --verbose' : ''}`, { + cwd: e2eCwd, + stdio: 'pipe', + env: process.env, + encoding: 'utf-8', + }); + + 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( @@ -233,16 +262,37 @@ export function packageInstall( .split(' ') .map((pgk) => `${pgk}@${version}`) .join(' '); - const install = execSync( - `${mode === 'dev' ? pm.addDev : pm.addProd} ${pkgsWithVersions}`, - { - cwd, - stdio: ['pipe', 'pipe', 'pipe'], - env: process.env, - encoding: 'utf-8', + + const command = `${ + mode === 'dev' ? pm.addDev : pm.addProd + } ${pkgsWithVersions}${isVerbose() ? ' --verbose' : ''}`; + + try { + const install = execSync( + `${mode === 'dev' ? pm.addDev : pm.addProd} ${pkgsWithVersions}${ + isVerbose() ? ' --verbose' : '' + }`, + { + cwd, + stdio: 'pipe', + env: process.env, + encoding: 'utf-8', + } + ); + + if (isVerbose()) { + output.log({ + title: `Command: ${command}`, + bodyLines: [install as string], + color: 'green', + }); } - ); - return install ? install.toString() : ''; + + return install; + } catch (e) { + logError(`Original command: ${command}`, `${e.stdout}\n\n${e.stderr}`); + throw e; + } } export function runNgNew( @@ -259,10 +309,10 @@ export function runNgNew( return execSync(command, { cwd: e2eCwd, - stdio: ['pipe', 'pipe', 'pipe'], + stdio: isVerbose() ? 'inherit' : 'pipe', env: process.env, encoding: 'utf-8', - }).toString(); + }); } 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()}`); } diff --git a/e2e/utils/get-env-info.ts b/e2e/utils/get-env-info.ts index d0e83679510769..7c1cd7ab31edbe 100644 --- a/e2e/utils/get-env-info.ts +++ b/e2e/utils/get-env-info.ts @@ -70,7 +70,9 @@ export function getNpmMajorVersion(): 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; } diff --git a/e2e/utils/log-utils.ts b/e2e/utils/log-utils.ts index 1de5eb7abf0123..88632b9aea290a 100644 --- a/e2e/utils/log-utils.ts +++ b/e2e/utils/log-utils.ts @@ -40,7 +40,7 @@ export function logSuccess(title: string, body?: string) { * @returns */ 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, '' );