fix(core): refactor the logging logic in e2e tests (#15548)
This commit is contained in:
parent
16ad3086be
commit
a0e00c85fc
@ -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();
|
||||
|
||||
@ -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',
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { PackageManager } from '@nrwl/devkit';
|
||||
import { output, PackageManager } from '@nrwl/devkit';
|
||||
import { packageInstall, tmpProjPath } from './create-project-utils';
|
||||
import {
|
||||
detectPackageManager,
|
||||
@ -70,9 +70,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 +80,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 +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;
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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()}`);
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
''
|
||||
);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user