From 1986a3774fd47a5ecfb3eff397bb259ccd3d9097 Mon Sep 17 00:00:00 2001 From: Mark Brouch Date: Fri, 20 Dec 2019 10:20:16 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AF=20Test=20overrides?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/overrides.spec.ts | 112 ++++++++++++++++++++++++++++++++ src/overrides.ts | 4 +- 2 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/__tests__/overrides.spec.ts diff --git a/src/__tests__/overrides.spec.ts b/src/__tests__/overrides.spec.ts new file mode 100644 index 0000000..6e12126 --- /dev/null +++ b/src/__tests__/overrides.spec.ts @@ -0,0 +1,112 @@ +import { CLIEngine } from "eslint"; + +import { + stripEmptyFiles, + stripNullRuleIds, + mapReportToOverrides +} from "../overrides"; + +function generateLintResult(ruleIds: string[] = []): CLIEngine.LintResult { + return { + filePath: "foo", + messages: ruleIds.map(ruleId => ({ + column: 0, + line: 0, + ruleId, + message: "", + nodeType: "", + severity: 0, + source: "" + })), + errorCount: 0, + warningCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0 + }; +} + +function generateReport( + results: CLIEngine.LintResult[] = [] +): CLIEngine.LintReport { + return { + results, + errorCount: 0, + warningCount: 0, + fixableErrorCount: 0, + fixableWarningCount: 0 + }; +} + +describe("stripEmptyFiles", () => { + test("removes results with no messages", () => { + const results = [ + generateLintResult(), + generateLintResult(["foo", "bar"]), + generateLintResult() + ]; + + expect(stripEmptyFiles(results)).toEqual([results[1]]); + }); +}); + +describe("stripNullRuleIds", () => { + test("removes null rule IDs", () => { + const ruleIds = ["foo", null, "bar", null, "baz"]; + + expect(stripNullRuleIds(ruleIds)).toEqual(["foo", "bar", "baz"]); + }); +}); + +describe("mapReportToOverrides", () => { + test("creates a list of overrides from a report", () => { + const report = generateReport([generateLintResult(["foo", "bar"])]); + + expect(mapReportToOverrides(report, "warn")).toEqual([ + { + files: ["foo"], + rules: { + foo: "warn", + bar: "warn" + } + } + ]); + }); + + test("sets the level of the rules", () => { + const report = generateReport([generateLintResult(["foo", "bar"])]); + + expect(mapReportToOverrides(report, "warn")).toEqual([ + { + files: ["foo"], + rules: { + foo: "warn", + bar: "warn" + } + } + ]); + + expect(mapReportToOverrides(report, "off")).toEqual([ + { + files: ["foo"], + rules: { + foo: "off", + bar: "off" + } + } + ]); + }); + + test("combines duplicate rule violations", () => { + const report = generateReport([generateLintResult(["foo", "foo", "bar"])]); + + expect(mapReportToOverrides(report, "warn")).toEqual([ + { + files: ["foo"], + rules: { + foo: "warn", + bar: "warn" + } + } + ]); + }); +}); diff --git a/src/overrides.ts b/src/overrides.ts index 1f44e75..680c348 100644 --- a/src/overrides.ts +++ b/src/overrides.ts @@ -1,13 +1,13 @@ import path from "path"; import { CLIEngine, Linter } from "eslint"; -function stripEmptyFiles( +export function stripEmptyFiles( results: CLIEngine.LintResult[] ): CLIEngine.LintResult[] { return results.filter(result => !!result.messages.length); } -function stripNullRuleIds(ruleIds: (string | null)[]): string[] { +export function stripNullRuleIds(ruleIds: (string | null)[]): string[] { return ruleIds.filter((ruleId): ruleId is string => ruleId !== null); }