diff --git a/package.json b/package.json index 36d6303..fce2f05 100644 --- a/package.json +++ b/package.json @@ -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" -} + } } \ No newline at end of file diff --git a/test-helpers/expect-violations/package.json b/test-helpers/expect-violations/package.json index 1ade343..5521d65 100644 --- a/test-helpers/expect-violations/package.json +++ b/test-helpers/expect-violations/package.json @@ -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": { @@ -13,5 +14,7 @@ "lint": "echo TODO", "test": "echo TODO" }, - "devDependencies": {} -} + "devDependencies": { + "@types/yargs": "^17.0.32" + } +} \ No newline at end of file diff --git a/test-helpers/expect-violations/src/index.ts b/test-helpers/expect-violations/src/index.ts index 41c945e..c2c221a 100644 --- a/test-helpers/expect-violations/src/index.ts +++ b/test-helpers/expect-violations/src/index.ts @@ -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: | npx expect-violations '); - return; - } - const expectatedViolationReportPath = argv[2]; + const options = yargs(process.argv.slice(2)) + .scriptName('expect-violations') + .command('update ', 'Updates the expected violation file') + .command('test ', '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 { diff --git a/tests/basic_javascript_eslint9/package.json b/tests/basic_javascript_eslint9/package.json index 7d83e45..fd24443 100644 --- a/tests/basic_javascript_eslint9/package.json +++ b/tests/basic_javascript_eslint9/package.json @@ -7,7 +7,8 @@ "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", @@ -15,4 +16,4 @@ "expect-violations": "^0.0.1", "typescript-eslint": "^7.16.0" } -} +} \ No newline at end of file diff --git a/tests/basic_typescript_eslint8/expected_violations.json b/tests/basic_typescript_eslint8/expected_violations.json index b334202..82bab61 100644 --- a/tests/basic_typescript_eslint8/expected_violations.json +++ b/tests/basic_typescript_eslint8/expected_violations.json @@ -13,4 +13,4 @@ } ] } -] +] \ No newline at end of file diff --git a/tests/basic_typescript_eslint8/package.json b/tests/basic_typescript_eslint8/package.json index c1827ad..5a82039 100644 --- a/tests/basic_typescript_eslint8/package.json +++ b/tests/basic_typescript_eslint8/package.json @@ -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", @@ -16,4 +17,4 @@ "typescript": "^5.5.3", "typescript-eslint": "^7.16.0" } -} +} \ No newline at end of file diff --git a/tests/basic_typescript_eslint9/package.json b/tests/basic_typescript_eslint9/package.json index cab9a21..f19ee2b 100644 --- a/tests/basic_typescript_eslint9/package.json +++ b/tests/basic_typescript_eslint9/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index 4b70576..f26ada1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/eslint-plugin@7.15.0": version "7.15.0" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.15.0.tgz#8eaf396ac2992d2b8f874b68eb3fcd6b179cb7f3" @@ -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" @@ -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== @@ -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" @@ -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" + yn@3.1.1: version "3.1.1" resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"