Skip to content

Commit

Permalink
fix(core): refactor the logging logic in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Mar 8, 2023
1 parent 5d19b53 commit c295022
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 84 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
137 changes: 85 additions & 52 deletions e2e/utils/command-utils.ts
Original file line number Diff line number Diff line change
@@ -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 All @@ -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');

Expand Down Expand Up @@ -70,26 +71,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',
},
} as any,
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 @@ -189,7 +197,7 @@ export function runCommandAsync(
CI: 'true',
...(opts.env || getStrippedEnvironmentVariables()),
FORCE_COLOR: 'false',
},
} as any,
encoding: 'utf-8',
},
(err, stdout, stderr) => {
Expand Down Expand Up @@ -218,14 +226,14 @@ export function runCommandUntil(
CI: 'true',
...getStrippedEnvironmentVariables(),
FORCE_COLOR: 'false',
},
} as any,
});
return new Promise((res, rej) => {
let output = '';
let complete = false;

function checkCriteria(c) {
output += c.toString();
output += c?.toString();
if (criteria(stripConsoleColors(output)) && !complete) {
complete = true;
res(p);
Expand Down Expand Up @@ -272,25 +280,35 @@ export function runNgAdd(
try {
const pmc = getPackageManagerCommand();
packageInstall(packageName, undefined, version);
return execSync(pmc.run(`ng g ${packageName}:ng-add`, command ?? ''), {
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 result = execSync(
pmc.run(
`ng g ${packageName}:ng-add`,
`${command}${isVerbose() ? '--verbose' : ''}` ?? ''
),
{
cwd: tmpProjPath(),
stdio: 'pipe',
env: { ...(opts.env || getStrippedEnvironmentVariables()) } as any,
encoding: 'utf-8',
}
)?.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
''
);
if (isVerbose()) {
output.log({
title: `Original command: ng g ${packageName}:ng-add ${command}`,
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;
}
}
Expand All @@ -305,19 +323,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,
} as any,
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 +355,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 +372,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()),
} as any,
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;
return r ?? logs;
} 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
Loading

0 comments on commit c295022

Please sign in to comment.