Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
116 changes: 73 additions & 43 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 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' : ''}`, {
mandarini marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
RunCmdOpts,
runCommand,
} from './command-utils';
import { output } from '@nrwl/devkit';

let projName: string;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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({
Expand Down Expand Up @@ -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()}`);
}

Expand Down
4 changes: 3 additions & 1 deletion e2e/utils/get-env-info.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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