diff --git a/package.json b/package.json index 7502b74e..a1cb297e 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "lint": "run-s lint:*", "lint:eslint": "([ \"$CI\" = true ] && eslint --ext js --ext ts --quiet . || eslint --ext js --ext ts .)", "lint:prettier": "prettier --check .", + "lint:expectations": "node tools/sort-test-expectations.js --lint", "postinstall": "npm run postinstall --workspaces --if-present", "prepare": "npm run prepare --workspaces --if-present", "test": "wireit", diff --git a/tools/sort-test-expectations.js b/tools/sort-test-expectations.js index 6e1332dc..ecdec0d5 100644 --- a/tools/sort-test-expectations.js +++ b/tools/sort-test-expectations.js @@ -25,6 +25,8 @@ const source = 'test/TestExpectations.json'; const testExpectations = JSON.parse(fs.readFileSync(source, 'utf-8')); +const commitedExpectations = structuredClone(testExpectations); + function getSpecificity(item) { return ( item.parameters.length + @@ -50,10 +52,21 @@ testExpectations.forEach(item => { item.platforms.sort(); }); -fs.writeFileSync( - source, - prettier.format(JSON.stringify(testExpectations), { - ...require(path.join(__dirname, '..', '.prettierrc.cjs')), - parser: 'json', - }) -); +if (process.argv.includes('--lint')) { + if ( + JSON.stringify(commitedExpectations) !== JSON.stringify(testExpectations) + ) { + console.error( + `${source} is not formatted properly. Run 'npm run format:expectations'.` + ); + process.exit(1); + } +} else { + fs.writeFileSync( + source, + prettier.format(JSON.stringify(testExpectations), { + ...require(path.join(__dirname, '..', '.prettierrc.cjs')), + parser: 'json', + }) + ); +}