chore(linter): fix non-deterministic snapshots for convert tslint to … (#14905)

This commit is contained in:
Jason Jean 2023-02-09 19:04:19 -05:00 committed by GitHub
parent 3330da3d03
commit 6b1d0c6934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 89 additions and 215 deletions

View File

@ -132,20 +132,19 @@ async function runWatch(command: string) {
stdio: 'pipe',
});
let output = '';
p.stdout?.on('data', (data) => {
output += data;
const s = data.toString().trim();
isVerbose() && console.log(s);
if (s.includes('watch process waiting')) {
resolve(async (timeout = 6000) => {
await wait(timeout);
p.kill();
return output;
return output
.split('\n')
.filter((line) => line.length > 0 && !line.includes('NX'));
});
} else {
if (s.length == 0 || s.includes('NX')) {
return;
}
output.push(s);
}
});
});

View File

@ -676,7 +676,7 @@ export function runNgAdd(
packageInstall(packageName, undefined, version);
return execSync(pmc.run(`ng g ${packageName}:ng-add`, command ?? ''), {
cwd: tmpProjPath(),
stdio: isVerbose() ? 'inherit' : 'pipe',
stdio: 'pipe',
env: { ...(opts.env || getStrippedEnvironmentVariables()) },
encoding: 'utf-8',
})
@ -709,7 +709,7 @@ export function runCLI(
const pm = getPackageManagerCommand();
const logs = execSync(`${pm.runNx} ${command}`, {
cwd: opts.cwd || tmpProjPath(),
env: { CI: 'true', ...(opts.env || getStrippedEnvironmentVariables()) },
env: { CI: 'true', ...getStrippedEnvironmentVariables(), ...opts.env },
encoding: 'utf-8',
stdio: 'pipe',
maxBuffer: 50 * 1024 * 1024,
@ -1153,13 +1153,17 @@ export async function expectJestTestsToPass(
}
export function getStrippedEnvironmentVariables() {
const strippedVariables = new Set(['NX_TASK_TARGET_PROJECT']);
return Object.fromEntries(
Object.entries(process.env).filter(
([key, value]) =>
!strippedVariables.has(key) ||
!key.startsWith('NX_') ||
key.startsWith('NX_E2E_')
)
Object.entries(process.env).filter(([key, value]) => {
if (key.startsWith('NX_E2E_')) {
return true;
}
if (key.startsWith('NX_')) {
return false;
}
return true;
})
);
}

View File

@ -67,18 +67,26 @@ describe('Webpack Plugin', () => {
`console.log(process.env['NX_TEST_VAR']);\n`
);
process.env.NX_TEST_VAR = 'Hello build time';
runCLI(`build ${myPkg} --platform=node`);
runCLI(`build ${myPkg} --platform=node`, {
env: {
NX_TEST_VAR: 'Hello build time',
},
});
process.env.NX_TEST_VAR = 'Hello run time';
expect(runCommand(`node dist/libs/${myPkg}/main.js`)).toMatch(
/Hello run time/
);
expect(
runCommand(`node dist/libs/${myPkg}/main.js`, {
env: {
NX_TEST_VAR: 'Hello run time',
},
})
).toMatch(/Hello run time/);
process.env.NX_TEST_VAR = 'Hello build time';
runCLI(`build ${myPkg} --platform=web`);
runCLI(`build ${myPkg} --platform=web`, {
env: {
NX_TEST_VAR: 'Hello build time',
},
});
expect(readFile(`dist/libs/${myPkg}/main.js`)).toMatch(/Hello build time/);
delete process.env.NX_TEST_VAR;
}, 300_000);
});

View File

@ -201,29 +201,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"_buffer",
"_counters",
"_timers",
"_groupDepth",
"Console",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",
@ -526,29 +504,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"_buffer",
"_counters",
"_timers",
"_groupDepth",
"Console",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",
@ -835,29 +791,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"_buffer",
"_counters",
"_timers",
"_groupDepth",
"Console",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",
@ -1209,29 +1143,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"_buffer",
"_counters",
"_timers",
"_groupDepth",
"Console",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",

View File

@ -226,7 +226,13 @@ describe('convert-tslint-to-eslint', () => {
/**
* The root level .eslintrc.json should now have been generated
*/
expect(readJson(host, '.eslintrc.json')).toMatchSnapshot();
const eslintJson = readJson(host, '.eslintrc.json');
expect(eslintJson.overrides[3].rules['no-console'][1].allow).toContain(
'log'
);
// Remove no-console config because it is not deterministic across node versions
delete eslintJson.overrides[3].rules['no-console'][1].allow;
expect(eslintJson).toMatchSnapshot();
/**
* The project level .eslintrc.json should now have been generated
@ -267,7 +273,13 @@ describe('convert-tslint-to-eslint', () => {
/**
* The root level .eslintrc.json should now have been generated
*/
expect(readJson(host, '.eslintrc.json')).toMatchSnapshot();
const eslintJson = readJson(host, '.eslintrc.json');
expect(eslintJson.overrides[3].rules['no-console'][1].allow).toContain(
'log'
);
// Remove no-console config because it is not deterministic across node versions
delete eslintJson.overrides[3].rules['no-console'][1].allow;
expect(eslintJson).toMatchSnapshot();
/**
* The project level .eslintrc.json should now have been generated
@ -298,6 +310,11 @@ describe('convert-tslint-to-eslint', () => {
* The root level .eslintrc.json should now have been generated
*/
const eslintContent = readJson(host, '.eslintrc.json');
expect(eslintContent.overrides[3].rules['no-console'][1].allow).toContain(
'log'
);
// Remove no-console config because it is not deterministic across node versions
delete eslintContent.overrides[3].rules['no-console'][1].allow;
expect(eslintContent).toMatchSnapshot();
/**
@ -330,7 +347,8 @@ describe('convert-tslint-to-eslint', () => {
* The root level .eslintrc.json should not be re-created
* if it already existed
*/
expect(readJson(host, '.eslintrc.json')).toMatchSnapshot();
const eslintJson2 = readJson(host, '.eslintrc.json');
expect(eslintJson2).toMatchSnapshot();
/**
* The root TSLint file should have been deleted

View File

@ -214,29 +214,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"_buffer",
"_counters",
"_timers",
"_groupDepth",
"Console",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",

View File

@ -143,7 +143,13 @@ describe('convert-tslint-to-eslint', () => {
/**
* The root level .eslintrc.json should now have been generated
*/
expect(readJson(host, '.eslintrc.json')).toMatchSnapshot();
const eslintJson = readJson(host, '.eslintrc.json');
expect(eslintJson.overrides[3].rules['no-console'][1].allow).toContain(
'log'
);
// Remove no-console config because it is not deterministic across node versions
delete eslintJson.overrides[3].rules['no-console'][1].allow;
expect(eslintJson).toMatchSnapshot();
/**
* The project level .eslintrc.json should now have been generated

View File

@ -191,29 +191,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"Console",
"_buffer",
"_counters",
"_groupDepth",
"_timers",
"assert",
"clear",
"count",
"countReset",
"dir",
"dirxml",
"error",
"group",
"groupCollapsed",
"groupEnd",
"log",
"table",
"timeLog",
"warn",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",

View File

@ -71,8 +71,11 @@ describe('convertToESLintConfig()', () => {
exampleRootTslintJson.raw,
[]
);
expect(
converted.convertedESLintConfig.rules['no-console'][1].allow
).toContain('log');
// Ensure no-console snapshot is deterministic
converted.convertedESLintConfig.rules['no-console'][1].allow.sort();
delete converted.convertedESLintConfig.rules['no-console'][1].allow;
expect(converted).toMatchSnapshot();
});

View File

@ -213,29 +213,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"_buffer",
"_counters",
"_timers",
"_groupDepth",
"Console",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",
@ -520,29 +498,7 @@ Object {
"no-caller": "error",
"no-console": Array [
"error",
Object {
"allow": Array [
"log",
"warn",
"dir",
"timeLog",
"assert",
"clear",
"count",
"countReset",
"group",
"groupEnd",
"table",
"dirxml",
"error",
"groupCollapsed",
"_buffer",
"_counters",
"_timers",
"_groupDepth",
"Console",
],
},
Object {},
],
"no-debugger": "error",
"no-empty": "off",

View File

@ -174,7 +174,13 @@ describe('convert-tslint-to-eslint', () => {
/**
* The root level .eslintrc.json should now have been generated
*/
expect(readJson(host, '.eslintrc.json')).toMatchSnapshot();
const eslintJson = readJson(host, '.eslintrc.json');
expect(eslintJson.overrides[3].rules['no-console'][1].allow).toContain(
'log'
);
// Remove no-console config because it is not deterministic across node versions
delete eslintJson.overrides[3].rules['no-console'][1].allow;
expect(eslintJson).toMatchSnapshot();
/**
* The project level .eslintrc.json should now have been generated
@ -215,7 +221,13 @@ describe('convert-tslint-to-eslint', () => {
/**
* The root level .eslintrc.json should now have been generated
*/
expect(readJson(host, '.eslintrc.json')).toMatchSnapshot();
const eslintJson = readJson(host, '.eslintrc.json');
expect(eslintJson.overrides[3].rules['no-console'][1].allow).toContain(
'log'
);
// Remove no-console config because it is not deterministic across node versions
delete eslintJson.overrides[3].rules['no-console'][1].allow;
expect(eslintJson).toMatchSnapshot();
/**
* The project level .eslintrc.json should now have been generated