From 474e419239caf07ab58e312711e6c01367374e61 Mon Sep 17 00:00:00 2001 From: Yavor Ivanov Date: Fri, 19 Apr 2024 10:24:08 +0300 Subject: [PATCH] refactor: Restructure rules tests to use a common execution --- test/lib/linter/_linterHelper.ts | 9 ++++ test/lib/linter/rules/BestPractices.ts | 5 +++ test/lib/linter/rules/CSPCompliance.ts | 9 +--- test/lib/linter/rules/NoGlobals.ts | 9 +--- test/lib/linter/rules/NoPseudoModules.ts | 9 +--- .../rules/snapshots/BestPractices.ts.md | 42 ++++++++++++++++++ .../rules/snapshots/BestPractices.ts.snap | Bin 0 -> 501 bytes 7 files changed, 62 insertions(+), 21 deletions(-) create mode 100644 test/lib/linter/rules/BestPractices.ts create mode 100644 test/lib/linter/rules/snapshots/BestPractices.ts.md create mode 100644 test/lib/linter/rules/snapshots/BestPractices.ts.snap diff --git a/test/lib/linter/_linterHelper.ts b/test/lib/linter/_linterHelper.ts index 86ac94434..9dbe3c052 100644 --- a/test/lib/linter/_linterHelper.ts +++ b/test/lib/linter/_linterHelper.ts @@ -2,6 +2,7 @@ import anyTest, {ExecutionContext, TestFn} from "ava"; import sinonGlobal, {SinonStub} from "sinon"; import util from "util"; import {readdirSync} from "node:fs"; +import path from "node:path"; import esmock from "esmock"; import SourceFileLinter from "../../../src/linter/ui5Types/SourceFileLinter.js"; import {SourceFile, TypeChecker} from "typescript"; @@ -125,3 +126,11 @@ export function preprocessLintResultsForSnapshot(res: LintResult[]) { }); return res; } + +export function runLintRulesTests(filePath: string) { + const __dirname = path.dirname(filePath); + const fileName = path.basename(filePath, ".ts"); + const fixturesPath = path.join(__dirname, "..", "..", "..", "fixtures", "linter", "rules", fileName); + + createTestsForFixtures(fixturesPath); +} diff --git a/test/lib/linter/rules/BestPractices.ts b/test/lib/linter/rules/BestPractices.ts new file mode 100644 index 000000000..103ec0308 --- /dev/null +++ b/test/lib/linter/rules/BestPractices.ts @@ -0,0 +1,5 @@ +import {fileURLToPath} from "node:url"; +import {runLintRulesTests} from "../_linterHelper.js"; + +const filePath = fileURLToPath(import.meta.url); +runLintRulesTests(filePath); diff --git a/test/lib/linter/rules/CSPCompliance.ts b/test/lib/linter/rules/CSPCompliance.ts index 57b18a4f6..103ec0308 100644 --- a/test/lib/linter/rules/CSPCompliance.ts +++ b/test/lib/linter/rules/CSPCompliance.ts @@ -1,10 +1,5 @@ -import path from "node:path"; import {fileURLToPath} from "node:url"; -import {createTestsForFixtures} from "../_linterHelper.js"; +import {runLintRulesTests} from "../_linterHelper.js"; const filePath = fileURLToPath(import.meta.url); -const __dirname = path.dirname(filePath); -const fileName = path.basename(filePath, ".ts"); -const fixturesPath = path.join(__dirname, "..", "..", "..", "fixtures", "linter", "rules", fileName); - -createTestsForFixtures(fixturesPath); +runLintRulesTests(filePath); diff --git a/test/lib/linter/rules/NoGlobals.ts b/test/lib/linter/rules/NoGlobals.ts index 57b18a4f6..103ec0308 100644 --- a/test/lib/linter/rules/NoGlobals.ts +++ b/test/lib/linter/rules/NoGlobals.ts @@ -1,10 +1,5 @@ -import path from "node:path"; import {fileURLToPath} from "node:url"; -import {createTestsForFixtures} from "../_linterHelper.js"; +import {runLintRulesTests} from "../_linterHelper.js"; const filePath = fileURLToPath(import.meta.url); -const __dirname = path.dirname(filePath); -const fileName = path.basename(filePath, ".ts"); -const fixturesPath = path.join(__dirname, "..", "..", "..", "fixtures", "linter", "rules", fileName); - -createTestsForFixtures(fixturesPath); +runLintRulesTests(filePath); diff --git a/test/lib/linter/rules/NoPseudoModules.ts b/test/lib/linter/rules/NoPseudoModules.ts index 57b18a4f6..103ec0308 100644 --- a/test/lib/linter/rules/NoPseudoModules.ts +++ b/test/lib/linter/rules/NoPseudoModules.ts @@ -1,10 +1,5 @@ -import path from "node:path"; import {fileURLToPath} from "node:url"; -import {createTestsForFixtures} from "../_linterHelper.js"; +import {runLintRulesTests} from "../_linterHelper.js"; const filePath = fileURLToPath(import.meta.url); -const __dirname = path.dirname(filePath); -const fileName = path.basename(filePath, ".ts"); -const fixturesPath = path.join(__dirname, "..", "..", "..", "fixtures", "linter", "rules", fileName); - -createTestsForFixtures(fixturesPath); +runLintRulesTests(filePath); diff --git a/test/lib/linter/rules/snapshots/BestPractices.ts.md b/test/lib/linter/rules/snapshots/BestPractices.ts.md new file mode 100644 index 000000000..d1804248b --- /dev/null +++ b/test/lib/linter/rules/snapshots/BestPractices.ts.md @@ -0,0 +1,42 @@ +# Snapshot report for `test/lib/linter/rules/BestPractices.ts` + +The actual snapshot is saved in `BestPractices.ts.snap`. + +Generated by [AVA](https://avajs.dev). + +## General: Component.js + +> Snapshot 1 + + [ + { + coverageInfo: [], + errorCount: 0, + fatalErrorCount: 0, + filePath: 'Component.js', + messages: [], + warningCount: 0, + }, + ] + +## General: manifest.json + +> Snapshot 1 + + [ + { + coverageInfo: [], + errorCount: 1, + fatalErrorCount: 1, + filePath: 'manifest.json', + messages: [ + { + fatal: true, + message: 'Unexpected end of JSON input', + ruleId: 'ui5-linter-parsing-error', + severity: 2, + }, + ], + warningCount: 0, + }, + ] diff --git a/test/lib/linter/rules/snapshots/BestPractices.ts.snap b/test/lib/linter/rules/snapshots/BestPractices.ts.snap new file mode 100644 index 0000000000000000000000000000000000000000..3b03e634a979c7d44991bb2ecb9e41bdcb76b9ef GIT binary patch literal 501 zcmVRzVr;zFfe#UYLGx;L0wQ)p`wZ)AqK{4 z{G43t+*x<$lnxA342(=&SXw>@dzgs2dBx_9s0-QU08RdSS?(TO|% z#9sN7^Czs?%JfmRXQr9ato^-Xr<|6Vn;7rjUd6HW@)f;np^N!PJrX+r9st;;&L#zq zt(9j))hY}_&>Dz9H;8ttL)}vVZvcD*@EO2&03iW)sl2E&JUZr)C$U)e`+d*`r~|r5 zbV$H0YJq}HS|6DiTRG+fofuGvZ4y;)jjc6y&*a({TWvH3!bEzR?*Cz`m#CDVOFvn> zxs0etG+9d2o3e9q%&u|G`l+;9>2dv~UX5L=#;)FLFZD`kvxT&&)GA>&4`lSJHSzwR zTKfXv2Y@XCcCKq}__x;PQLdqFWo*UUMvYPt>g4~3Bk_TN3&L*=E-}8Ol5y~rfNuo+ zB;dXPCZfSMSYg|4gbfNdMJ2*UuUr0LRo{83`E|x4&vC>$j!Y6geKC9%DV^oMakgXg rln>&nyq&9ugH&nHc92QylpYTXZ%qe|XKa-}D;