Skip to content

Commit

Permalink
Introduce build validation for bulk suppressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
iclanton committed Mar 20, 2024
1 parent 24f972b commit bbcf811
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 11 deletions.
88 changes: 88 additions & 0 deletions build-tests/eslint-bulk-suppressions-test/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { FileSystem, Executable, Text, Import } = require('@rushstack/node-core-library');
const {
ESLINT_PACKAGE_NAME_ENV_VAR_NAME
} = require('@rushstack/eslint-patch/lib/eslint-bulk-suppressions/constants');

const eslintBulkStartPath = Import.resolveModule({
modulePath: '@rushstack/eslint-bulk/lib/start',
baseFolderPath: __dirname
});

function tryLoadSuppressions(suppressionsJsonPath) {
try {
return Text.convertToLf(FileSystem.readFile(suppressionsJsonPath)).trim();
} catch (e) {
if (FileSystem.isNotExistError(e)) {
return '';
} else {
throw e;
}
}
}

const RUN_FOLDER_PATHS = ['client', 'server'];
const ESLINT_PACKAGE_NAMES = ['eslint', 'eslint-newest', 'eslint-oldest'];

const updateFilePaths = new Set();

for (const runFolderPath of RUN_FOLDER_PATHS) {
const folderPath = `${__dirname}/${runFolderPath}`;
const suppressionsJsonPath = `${folderPath}/.eslint-bulk-suppressions.json`;

const folderItems = FileSystem.readFolderItems(folderPath);
for (const folderItem of folderItems) {
if (folderItem.isFile() && folderItem.name.match(/^\.eslint\-bulk\-suppressions\-[\d.]+\.json$/)) {
const fullPath = `${folderPath}/${folderItem.name}`;
updateFilePaths.add(fullPath);
}
}

for (const eslintPackageName of ESLINT_PACKAGE_NAMES) {
const { version: eslintVersion } = require(`${eslintPackageName}/package.json`);

const startLoggingMessage = `-- Running eslint-bulk-suppressions for eslint@${eslintVersion} in ${runFolderPath} --`;
const endLoggingMessage = '-'.repeat(startLoggingMessage.length);
console.log(startLoggingMessage);
const referenceSuppressionsJsonPath = `${folderPath}/.eslint-bulk-suppressions-${eslintVersion}.json`;
const existingSuppressions = tryLoadSuppressions(referenceSuppressionsJsonPath);

const executableResult = Executable.spawnSync(
process.argv0,
[eslintBulkStartPath, 'suppress', '--all', 'src'],
{
currentWorkingDirectory: folderPath,
environment: {
...process.env,
[ESLINT_PACKAGE_NAME_ENV_VAR_NAME]: eslintPackageName
}
}
);

console.log('STDOUT:');
console.log(executableResult.stdout.toString());

console.log('STDERR:');
console.log(executableResult.stderr.toString());

const newSuppressions = tryLoadSuppressions(suppressionsJsonPath);
if (newSuppressions === existingSuppressions) {
updateFilePaths.delete(referenceSuppressionsJsonPath);
} else {
updateFilePaths.add(referenceSuppressionsJsonPath);
FileSystem.writeFile(referenceSuppressionsJsonPath, newSuppressions);
}

FileSystem.deleteFile(suppressionsJsonPath);

console.log(endLoggingMessage);
console.log();
}
}

if (updateFilePaths.size > 0) {
for (const updateFilePath of updateFilePaths) {
console.log(`The suppressions file "${updateFilePath}" was updated and must be committed to git.`);
}

process.exit(1);
}
9 changes: 7 additions & 2 deletions build-tests/eslint-bulk-suppressions-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
"version": "1.0.0",
"private": true,
"scripts": {
"_phase:build": "eslint ."
"_phase:build": "node build.js"
},
"devDependencies": {
"@rushstack/eslint-bulk": "workspace:*",
"@rushstack/eslint-patch": "workspace:*",
"@rushstack/heft": "workspace:*",
"local-node-rig": "workspace:*",
"@rushstack/node-core-library": "workspace:*",
"@typescript-eslint/parser": "~6.19.0",
"eslint": "~8.7.0",
"eslint-newest": "npm:eslint@~8.23.1",
"eslint-oldest": "npm:[email protected]",
"local-node-rig": "workspace:*",
"typescript": "~5.4.2"
}
}
4 changes: 4 additions & 0 deletions common/config/rush/nonbrowser-approved-packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
"name": "@rushstack/debug-certificate-manager",
"allowedCategories": [ "libraries" ]
},
{
"name": "@rushstack/eslint-bulk",
"allowedCategories": [ "tests" ]
},
{
"name": "@rushstack/eslint-config",
"allowedCategories": [ "libraries", "tests", "vscode-extensions" ]
Expand Down
147 changes: 140 additions & 7 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/rush/repo-state.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "552257b9ed51e893210d6bba3f0c10fcddf6a9aa",
"pnpmShrinkwrapHash": "1da0835844b2dddb8f340063ceb13ccf02a65dea",
"preferredVersionsHash": "c3ce06bc821dea3e1fe5dbc73da35b305908795a"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See LICENSE in the project root for license information.

import path from 'path';
import { BULK_SUPPRESSIONS_CLI_ESLINT_PACKAGE_NAME } from '../../constants';

// When this list is updated, update the `eslint-bulk-suppressions-newest-test`
// and/or the `eslint-bulk-suppressions-newest-test` projects' eslint dependencies.
Expand All @@ -11,7 +12,9 @@ export function getEslintPath(packagePath: string): string {
// Try to find a local ESLint installation, the one that should be listed as a dev dependency in package.json
// and installed in node_modules
try {
const localEslintApiPath: string = require.resolve('eslint', { paths: [packagePath] });
const localEslintApiPath: string = require.resolve(BULK_SUPPRESSIONS_CLI_ESLINT_PACKAGE_NAME, {
paths: [packagePath]
});
const localEslintPath: string = path.dirname(path.dirname(localEslintApiPath));
const { version: localEslintVersion } = require(`${localEslintPath}/package.json`);

Expand Down
6 changes: 6 additions & 0 deletions eslint/eslint-patch/src/eslint-bulk-suppressions/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ export const ESLINT_BULK_DETECT_ENV_VAR_NAME: '_RUSHSTACK_ESLINT_BULK_DETECT' =
export const ESLINT_BULK_FORCE_REGENERATE_PATCH_ENV_VAR_NAME: 'RUSHSTACK_ESLINT_BULK_FORCE_REGENERATE_PATCH' =
'RUSHSTACK_ESLINT_BULK_FORCE_REGENERATE_PATCH';
export const VSCODE_PID_ENV_VAR_NAME: 'VSCODE_PID' = 'VSCODE_PID';

export const ESLINT_PACKAGE_NAME_ENV_VAR_NAME: '_RUSHSTACK_ESLINT_PACKAGE_NAME' =
'_RUSHSTACK_ESLINT_PACKAGE_NAME';

export const BULK_SUPPRESSIONS_CLI_ESLINT_PACKAGE_NAME: string =
process.env[ESLINT_PACKAGE_NAME_ENV_VAR_NAME] ?? 'eslint';

0 comments on commit bbcf811

Please sign in to comment.