Skip to content

Commit

Permalink
fix(core): refactor the logging logic in e2e tests (#15548)
Browse files Browse the repository at this point in the history
(cherry picked from commit a0e00c8)
  • Loading branch information
mandarini authored and FrozenPandaz committed Mar 10, 2023
1 parent 27a3cb0 commit 9c84e1d
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 75 deletions.
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions e2e/nx-init/src/nx-init-nest.test.ts
Expand Up @@ -27,7 +27,7 @@ describe('nx init (for NestCLI)', () => {
cwd: e2eCwd,
encoding: 'utf-8',
env: process.env,
stdio: ['pipe', 'pipe', 'pipe'],
stdio: 'pipe',
}
);

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

Expand Down
116 changes: 73 additions & 43 deletions 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,
Expand Down Expand Up @@ -70,26 +70,33 @@ 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,
FORCE_COLOR: 'false',
},
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;
}
Expand Down Expand Up @@ -272,25 +279,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,
''
);
});

const r = stripConsoleColors(result);

if (isVerbose()) {
output.log({
title: `Original command: ${fullCommand}`,
bodyLines: [result as string],
color: 'green',
});
}

return r;
} 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;
}
}
Expand All @@ -305,19 +320,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();
Expand All @@ -326,12 +352,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;
}
}
Expand All @@ -346,28 +369,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;
}
}
Expand Down
104 changes: 77 additions & 27 deletions e2e/utils/create-project-utils.ts
Expand Up @@ -27,6 +27,7 @@ import {
RunCmdOpts,
runCommand,
} from './command-utils';
import { output } from '@nrwl/devkit';

let projName: string;

Expand Down Expand Up @@ -95,7 +96,7 @@ export function newProject({
copySync(`${tmpBackupProjPath()}`, `${tmpProjPath()}`);
}

if (process.env.NX_VERBOSE_LOGGING == 'true') {
if (isVerbose()) {
logInfo(`NX`, `E2E test is creating a project: ${tmpProjPath()}`);
}
return projScope;
Expand Down Expand Up @@ -174,13 +175,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(
Expand Down Expand Up @@ -217,13 +232,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(
Expand All @@ -238,16 +267,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(
Expand All @@ -263,10 +313,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({
Expand Down Expand Up @@ -294,7 +344,7 @@ export function newLernaWorkspace({
);
}

if (process.env.NX_VERBOSE_LOGGING == 'true') {
if (isVerbose()) {
logInfo(`NX`, `E2E test has created a lerna workspace: ${tmpProjPath()}`);
}

Expand Down
4 changes: 3 additions & 1 deletion e2e/utils/get-env-info.ts
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/utils/log-utils.ts
Expand Up @@ -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,
''
);
Expand Down

0 comments on commit 9c84e1d

Please sign in to comment.