Skip to content

Commit

Permalink
Add commands to update the violation golden files
Browse files Browse the repository at this point in the history
Also explicitely use the node_module path for running eslint8 as there's a bug in yarn that makes it run the wrong binary yarnpkg/yarn#8590

Change-Id: I0ffb28f7831b8c016d1f6f00b9af9909d78647ed
  • Loading branch information
neuracr committed Jul 19, 2024
1 parent 654446f commit a79b037
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"scripts": {
"unit_tests": "yarn workspace eslint-plugin-safety-web test",
"integration_tests": "yarn workspace basic-typescript-eslint9 test && yarn workspace basic-typescript-eslint8 test && yarn workspace basic-javascript-eslint9 test",
"update_integration_tests": "yarn workspace basic-typescript-eslint9 update && yarn workspace basic-typescript-eslint8 update && yarn workspace basic-javascript-eslint9 update",
"test": "yarn workspaces run test"
}
}
}
9 changes: 6 additions & 3 deletions test-helpers/expect-violations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"type": "module",
"dependencies": {
"chai": "^5.1.1"
"chai": "^5.1.1",
"yargs": "^17.7.2"
},
"bin": "./bin/index.js",
"scripts": {
Expand All @@ -13,5 +14,7 @@
"lint": "echo TODO",
"test": "echo TODO"
},
"devDependencies": {}
}
"devDependencies": {
"@types/yargs": "^17.0.32"
}
}
43 changes: 35 additions & 8 deletions test-helpers/expect-violations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,49 @@ import { expect } from 'chai';
import fs from 'node:fs';
import type { ViolationReport } from './violations.js';
import * as path from 'path';
import yargs from 'yargs';

function main() {
const argv = process.argv;
// TODO process with Yargs as more flags are added.
if (argv.length !== 3) {
console.error('Usage: <eslint JSON output> | npx expect-violations <path/to/expectated_violations.json>');
return;
}
const expectatedViolationReportPath = argv[2];
const options = yargs(process.argv.slice(2))
.scriptName('expect-violations')
.command('update <expected_file_json>', 'Updates the expected violation file')
.command('test <expected_file_json>', 'Test the expected violation file')
.demandCommand()
.parseSync();

const command = options._[0] as 'update' | 'test';
const expectatedViolationReportPath = options.expected_file_json as string;

const expectedViolationReport = getExpectedViolations(expectatedViolationReportPath) as ViolationReport;
const actualESLintReport = JSON.parse(fs.readFileSync(0).toString()) as ViolationReport; // STDIN_FILENO = 0
const canonicalizedReport = canonicalizeViolationReport(actualESLintReport);
switch (command) {
case 'test':
test(canonicalizedReport, expectedViolationReport);
break;
case 'update':
update(canonicalizedReport, expectedViolationReport, expectatedViolationReportPath);
break;
}
}


function test(canonicalizedReport: ViolationReport, expectedViolationReport: ViolationReport) {
expect(canonicalizedReport).to.deep.equal(expectedViolationReport);
}

function update(canonicalizedReport: ViolationReport, expectedViolationReport: ViolationReport, filePath: string) {
let isEqual = true;
try {
expect(JSON.parse(JSON.stringify(canonicalizedReport, null, 2))).to.deep.equal(expectedViolationReport);
} catch {
isEqual = false;
}
if (isEqual) {
console.log('Expected violations already up to date.');
} else {
fs.writeFileSync(filePath, JSON.stringify(canonicalizedReport, null, 2));
console.log(`Updated the violation file: ${filePath}`);
}
}

function canonicalizeViolationReport(report: ViolationReport): ViolationReport {
Expand Down
5 changes: 3 additions & 2 deletions tests/basic_javascript_eslint9/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"clean": ":",
"build": ":",
"lint": "echo skip lint script for integration test case",
"test": "eslint --format json | yarn run expect-violations ./expected_violations.json"
"test": "eslint --format json | yarn run expect-violations test ./expected_violations.json",
"update": "eslint --format json | yarn run expect-violations update ./expected_violations.json"
},
"devDependencies": {
"eslint": "^9.6.0",
"eslint-plugin-safety-web": "0.1.0",
"expect-violations": "^0.0.1",
"typescript-eslint": "^7.16.0"
}
}
}
2 changes: 1 addition & 1 deletion tests/basic_typescript_eslint8/expected_violations.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
}
]
}
]
]
5 changes: 3 additions & 2 deletions tests/basic_typescript_eslint8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"clean": "tsc --build --clean",
"build": "tsc",
"lint": "echo skip lint script for integration test case",
"test": "eslint src/ --format json | npx expect-violations ./expected_violations.json"
"test": "node_modules/.bin/eslint src/ --format json | yarn run expect-violations test ./expected_violations.json",
"update": "node_modules/.bin/eslint src/ --format json | yarn run expect-violations update ./expected_violations.json"
},
"devDependencies": {
"eslint": "^8.0.0",
Expand All @@ -16,4 +17,4 @@
"typescript": "^5.5.3",
"typescript-eslint": "^7.16.0"
}
}
}
3 changes: 2 additions & 1 deletion tests/basic_typescript_eslint9/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"clean": "tsc --build --clean",
"build": "tsc",
"lint": "echo skip lint script for integration test case",
"test": "eslint --format json | npx expect-violations ./expected_violations.json"
"test": "eslint --format json | yarn run expect-violations test ./expected_violations.json",
"update": "eslint --format json | yarn run expect-violations update ./expected_violations.json"
},
"devDependencies": {
"eslint": "^9.6.0",
Expand Down
41 changes: 40 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@
dependencies:
undici-types "~5.26.4"

"@types/yargs-parser@*":
version "21.0.3"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.3.tgz#815e30b786d2e8f0dcd85fd5bcf5e1a04d008f15"
integrity sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==

"@types/yargs@^17.0.32":
version "17.0.32"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-17.0.32.tgz#030774723a2f7faafebf645f4e5a48371dca6229"
integrity sha512-xQ67Yc/laOG5uMfX/093MRlGGCIBzZMarVa+gfNKJxWAIgykYpVGkBdbqEzGDDfCrVUj6Hiff4mTZ5BA6TmAog==
dependencies:
"@types/yargs-parser" "*"

"@typescript-eslint/[email protected]":
version "7.15.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.15.0.tgz#8eaf396ac2992d2b8f874b68eb3fcd6b179cb7f3"
Expand Down Expand Up @@ -641,6 +653,15 @@ cliui@^7.0.2:
strip-ansi "^6.0.0"
wrap-ansi "^7.0.0"

cliui@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-8.0.1.tgz#0c04b075db02cbfe60dc8e6cf2f5486b1a3608aa"
integrity sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==
dependencies:
string-width "^4.2.0"
strip-ansi "^6.0.1"
wrap-ansi "^7.0.0"

color-convert@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3"
Expand Down Expand Up @@ -1502,7 +1523,7 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

string-width@^4.1.0, string-width@^4.2.0:
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -1693,6 +1714,11 @@ yargs-parser@^20.2.2, yargs-parser@^20.2.9:
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==

yargs-parser@^21.1.1:
version "21.1.1"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==

yargs-unparser@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/yargs-unparser/-/yargs-unparser-2.0.0.tgz#f131f9226911ae5d9ad38c432fe809366c2325eb"
Expand All @@ -1716,6 +1742,19 @@ yargs@^16.2.0:
y18n "^5.0.5"
yargs-parser "^20.2.2"

yargs@^17.7.2:
version "17.7.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.7.2.tgz#991df39aca675a192b816e1e0363f9d75d2aa269"
integrity sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==
dependencies:
cliui "^8.0.1"
escalade "^3.1.1"
get-caller-file "^2.0.5"
require-directory "^2.1.1"
string-width "^4.2.3"
y18n "^5.0.5"
yargs-parser "^21.1.1"

[email protected]:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
Expand Down

0 comments on commit a79b037

Please sign in to comment.