From 1c9109d5c75477de701002918f2ada2d46e23d12 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 10 Nov 2026 17:33:19 -0500 Subject: [PATCH 1/7] test: add end-to-end tests for recommended config Co-Authored-By: Claude Fable 5.2 --- tests/autofix.test.ts | 100 ++++++++++++++++++++ tests/directives.test.ts | 114 +++++++++++++++++++++++ tests/plugin.test.ts | 191 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 405 insertions(+) create mode 100644 tests/autofix.test.ts create mode 100644 tests/directives.test.ts create mode 100644 tests/plugin.test.ts diff --git a/tests/autofix.test.ts b/tests/autofix.test.ts new file mode 100644 index 0000000..0939c68 --- /dev/null +++ b/tests/autofix.test.ts @@ -0,0 +1,100 @@ +/** + * @fileoverview Integration tests for autofixing through the Linter API. + */ + +import { describe, expect, it } from "vitest"; +import { Linter } from "eslint"; +import docker from "../src/index.js"; + +function fix(code: string, rules: Record): Linter.FixReport { + const linter = new Linter(); + + return linter.verifyAndFix( + code, + [ + { + files: ["**/Dockerfile"], + plugins: { docker }, + language: "docker/dockerfile", + rules: rules as never, + }, + ] as never, + "Dockerfile", + ); +} + +describe("autofix", () => { + it("should uppercase instruction keywords", () => { + const result = fix("from node:20\nrun echo hi\n", { + "docker/consistent-instruction-casing": "error", + }); + + expect(result.fixed).toBe(true); + expect(result.output).toBe("FROM node:20\nRUN echo hi\n"); + expect(result.messages).toEqual([]); + }); + + it("should convert MAINTAINER to a LABEL", () => { + const result = fix("FROM node:20\nMAINTAINER jane@example.com\n", { + "docker/no-maintainer": "error", + }); + + expect(result.output).toBe( + 'FROM node:20\nLABEL maintainer="jane@example.com"\n', + ); + }); + + it("should convert ADD to COPY", () => { + const result = fix("ADD src/ /app/src/\n", { + "docker/prefer-copy-over-add": "error", + }); + + expect(result.output).toBe("COPY src/ /app/src/\n"); + }); + + it("should convert a lone RUN cd to WORKDIR", () => { + const result = fix("RUN cd /app\n", { + "docker/no-cd-in-run": "error", + }); + + expect(result.output).toBe("WORKDIR /app\n"); + }); + + it("should convert shell form CMD to exec form", () => { + const result = fix("CMD npm start\n", { + "docker/prefer-exec-form": "error", + }); + + expect(result.output).toBe('CMD ["npm", "start"]\n'); + }); + + it("should apply fixes from multiple rules at once", () => { + const result = fix( + [ + "from node:20", + "maintainer jane", + "add src/ /app/", + "cmd npm start", + "", + ].join("\n"), + { + "docker/consistent-instruction-casing": "error", + "docker/no-maintainer": "error", + "docker/prefer-copy-over-add": "error", + "docker/prefer-exec-form": "error", + }, + ); + + expect(result.fixed).toBe(true); + expect(result.output).toBe( + [ + "FROM node:20", + 'LABEL maintainer="jane"', + "COPY src/ /app/", + 'CMD ["npm", "start"]', + "", + ].join("\n"), + ); + expect(result.messages).toEqual([]); + }); +}); diff --git a/tests/directives.test.ts b/tests/directives.test.ts new file mode 100644 index 0000000..ce8aef9 --- /dev/null +++ b/tests/directives.test.ts @@ -0,0 +1,114 @@ +/** + * @fileoverview Integration tests for inline configuration comments. + */ + +import { describe, expect, it } from "vitest"; +import { Linter } from "eslint"; +import docker from "../src/index.js"; + +function lint( + code: string, + rules: Record, +): Linter.LintMessage[] { + const linter = new Linter(); + + return linter.verify( + code, + [ + { + files: ["**/Dockerfile"], + plugins: { docker }, + language: "docker/dockerfile", + rules: rules as never, + }, + ] as never, + "Dockerfile", + ); +} + +describe("disable directives", () => { + it("should honor eslint-disable-next-line", () => { + const messages = lint( + "# eslint-disable-next-line docker/no-maintainer\nMAINTAINER jane\n", + { "docker/no-maintainer": "error" }, + ); + + expect(messages).toEqual([]); + }); + + it("should not treat # after an instruction as a directive", () => { + // Dockerfile comments must start at the beginning of a line, so a + // trailing # is part of the arguments and eslint-disable-line + // cannot be used inline. Use eslint-disable-next-line instead. + const messages = lint( + "MAINTAINER jane # eslint-disable-line docker/no-maintainer\n", + { "docker/no-maintainer": "error" }, + ); + + expect(messages).toHaveLength(1); + expect(messages[0]?.ruleId).toBe("docker/no-maintainer"); + }); + + it("should honor eslint-disable/eslint-enable blocks", () => { + const messages = lint( + [ + "# eslint-disable docker/require-image-tag", + "FROM node", + "# eslint-enable docker/require-image-tag", + "FROM ubuntu", + "", + ].join("\n"), + { "docker/require-image-tag": "error" }, + ); + + expect(messages).toHaveLength(1); + expect(messages[0]?.line).toBe(4); + }); + + it("should only disable the named rule", () => { + const messages = lint( + "# eslint-disable-next-line docker/no-sudo\nMAINTAINER jane\n", + { "docker/no-maintainer": "error" }, + ); + const ruleMessages = messages.filter( + message => message.ruleId !== null, + ); + + expect(ruleMessages).toHaveLength(1); + expect(ruleMessages[0]?.ruleId).toBe("docker/no-maintainer"); + }); + + it("should report unused disable directives when asked", () => { + const linter = new Linter(); + const messages = linter.verify( + "# eslint-disable-next-line docker/no-maintainer\nFROM node:20\n", + [ + { + files: ["**/Dockerfile"], + plugins: { docker }, + language: "docker/dockerfile", + linterOptions: { + reportUnusedDisableDirectives: "error", + }, + rules: { "docker/no-maintainer": "error" }, + }, + ] as never, + "Dockerfile", + ); + + expect(messages).toHaveLength(1); + expect(messages[0]?.message).toMatch(/Unused eslint-disable/u); + }); +}); + +describe("inline rule configuration", () => { + it("should apply severity from eslint comments", () => { + const messages = lint( + '# eslint docker/no-maintainer: "warn"\nMAINTAINER jane\n', + { "docker/no-maintainer": "error" }, + ); + + expect(messages).toHaveLength(1); + expect(messages[0]?.severity).toBe(1); + }); +}); diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts new file mode 100644 index 0000000..4dcbe07 --- /dev/null +++ b/tests/plugin.test.ts @@ -0,0 +1,191 @@ +/** + * @fileoverview Integration tests running the plugin through the ESLint + * Linter API with the recommended configuration. + */ + +import { describe, expect, it } from "vitest"; +import { Linter } from "eslint"; +import docker from "../src/index.js"; + +const baseConfig = { + files: ["**/Dockerfile"], + plugins: { docker }, + language: "docker/dockerfile", +} as const; + +function lint( + code: string, + rules: Record = {}, +): Linter.LintMessage[] { + const linter = new Linter(); + + return linter.verify( + code, + [ + { + ...baseConfig, + rules: rules as never, + }, + ] as never, + "Dockerfile", + ); +} + +describe("plugin integration", () => { + it("should lint a clean Dockerfile without messages", () => { + const messages = lint( + [ + "FROM node:20-alpine AS base", + "WORKDIR /app", + "COPY package.json package-lock.json ./", + "RUN npm ci", + "COPY . .", + "USER node", + 'CMD ["node", "server.js"]', + "", + ].join("\n"), + { + "docker/require-image-tag": "error", + "docker/require-absolute-workdir": "error", + "docker/prefer-copy-over-add": "error", + "docker/prefer-exec-form": "error", + "docker/no-cd-in-run": "error", + }, + ); + + expect(messages).toEqual([]); + }); + + it("should report rule violations with correct positions", () => { + const messages = lint("FROM node\n", { + "docker/require-image-tag": "error", + }); + + expect(messages).toHaveLength(1); + expect(messages[0]).toMatchObject({ + ruleId: "docker/require-image-tag", + line: 1, + column: 6, + endLine: 1, + endColumn: 10, + severity: 2, + }); + }); + + it("should not produce fatal errors for malformed input", () => { + const messages = lint("]]] this is not really a dockerfile [[[\n"); + + expect(messages.filter(message => message.fatal)).toEqual([]); + }); + + it("should run multiple rules together", () => { + const messages = lint( + [ + "FROM ubuntu:latest", + "MAINTAINER jane@example.com", + "RUN sudo apt-get update && apt-get upgrade -y", + "RUN cd /opt && make", + "ADD src/ /app/src/", + "WORKDIR app", + "", + ].join("\n"), + { + "docker/require-image-tag": "error", + "docker/no-maintainer": "error", + "docker/no-sudo": "error", + "docker/no-apt-get-upgrade": "error", + "docker/no-cd-in-run": "error", + "docker/prefer-copy-over-add": "error", + "docker/require-absolute-workdir": "error", + }, + ); + const ruleIds = new Set(messages.map(message => message.ruleId)); + + expect(ruleIds).toEqual( + new Set([ + "docker/require-image-tag", + "docker/no-maintainer", + "docker/no-sudo", + "docker/no-apt-get-upgrade", + "docker/no-cd-in-run", + "docker/prefer-copy-over-add", + "docker/require-absolute-workdir", + ]), + ); + }); +}); + +describe("recommended configuration", () => { + it("should be usable directly with the Linter", () => { + const linter = new Linter(); + const messages = linter.verify( + "FROM node\n", + [docker.configs.recommended] as never, + "Dockerfile", + ); + + expect( + messages.some( + message => message.ruleId === "docker/require-image-tag", + ), + ).toBe(true); + }); + + it("should apply to Dockerfile variants", () => { + const linter = new Linter(); + + for (const filename of [ + "Dockerfile", + "Dockerfile.dev", + "app.dockerfile", + "Containerfile", + "services/api/Dockerfile", + ]) { + const messages = linter.verify( + "FROM node\n", + [docker.configs.recommended] as never, + filename, + ); + + expect(messages.length, filename).toBeGreaterThan(0); + } + }); + + it("should not apply to other files", () => { + const linter = new Linter(); + + // Files that do not match the config's patterns fall back to the + // default JavaScript language, so use valid JavaScript here. + const messages = linter.verify( + 'const image = "FROM node";\n', + [docker.configs.recommended] as never, + "script.js", + ); + + expect(messages).toEqual([]); + }); + + it("should flag a Dockerfile with typical hadolint findings", () => { + const linter = new Linter(); + const code = [ + "from ubuntu", + "MAINTAINER jane@example.com", + "RUN cd /tmp && sudo make", + "CMD npm start", + "", + ].join("\n"); + const messages = linter.verify( + code, + [docker.configs.recommended] as never, + "Dockerfile", + ); + const ruleIds = new Set(messages.map(message => message.ruleId)); + + expect(ruleIds).toContain("docker/consistent-instruction-casing"); + expect(ruleIds).toContain("docker/require-image-tag"); + expect(ruleIds).toContain("docker/no-maintainer"); + expect(ruleIds).toContain("docker/no-cd-in-run"); + expect(ruleIds).toContain("docker/no-sudo"); + expect(ruleIds).toContain("docker/prefer-exec-form"); + }); +}); From 651f1e864e23d1cd3ddbcfdfa3452a91257783b7 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 11 Nov 2026 09:24:48 -0500 Subject: [PATCH 2/7] test: assert a rule diagnostic for each Dockerfile variant A nonempty message list also passes when a filename matches no config, because ESLint reports 'No matching configuration found'. Co-Authored-By: Claude Opus 5.5 --- tests/plugin.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts index 4dcbe07..b593679 100644 --- a/tests/plugin.test.ts +++ b/tests/plugin.test.ts @@ -147,7 +147,12 @@ describe("recommended configuration", () => { filename, ); - expect(messages.length, filename).toBeGreaterThan(0); + // Unmatched files also produce a message ("No matching + // configuration found"), so check for a rule diagnostic. + expect( + messages.map(message => message.ruleId), + filename, + ).toContain("docker/require-image-tag"); } }); From 7c7b089565046a7a68ac01a0a5cf1a2c10a00596 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 18 Nov 2026 10:43:16 -0500 Subject: [PATCH 3/7] test: run recommended rules against malformed input Co-Authored-By: Claude Opus 5.5 --- tests/plugin.test.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts index b593679..4fea75f 100644 --- a/tests/plugin.test.ts +++ b/tests/plugin.test.ts @@ -72,10 +72,33 @@ describe("plugin integration", () => { }); }); - it("should not produce fatal errors for malformed input", () => { + it("should parse malformed input without any messages", () => { const messages = lint("]]] this is not really a dockerfile [[[\n"); + expect(messages).toEqual([]); + }); + + it("should run the recommended rules on malformed input", () => { + const messages = lint( + [ + "]]] this is not really a dockerfile [[[", + "FROM", + "RUN", + "CMD [", + 'ENTRYPOINT ["unterminated', + "WORKDIR", + "ADD", + "MAINTAINER", + "", + ].join("\n"), + docker.configs.recommended.rules, + ); + expect(messages.filter(message => message.fatal)).toEqual([]); + // Only the deprecated MAINTAINER keyword is a real problem here. + expect(messages.map(message => message.ruleId)).toEqual([ + "docker/no-maintainer", + ]); }); it("should run multiple rules together", () => { From b6345c472e725520ee0d9d6910eb2b55f001b4d6 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 18 Nov 2026 10:46:51 -0500 Subject: [PATCH 4/7] test: expect a suggestion rather than a fix for ADD to COPY Co-Authored-By: Claude Opus 5.5 --- tests/autofix.test.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/autofix.test.ts b/tests/autofix.test.ts index 0939c68..3fa790f 100644 --- a/tests/autofix.test.ts +++ b/tests/autofix.test.ts @@ -44,12 +44,18 @@ describe("autofix", () => { ); }); - it("should convert ADD to COPY", () => { + it("should suggest, not autofix, converting ADD to COPY", () => { const result = fix("ADD src/ /app/src/\n", { "docker/prefer-copy-over-add": "error", }); - expect(result.output).toBe("COPY src/ /app/src/\n"); + expect(result.fixed).toBe(false); + expect(result.output).toBe("ADD src/ /app/src/\n"); + expect(result.messages).toHaveLength(1); + expect(result.messages[0]?.suggestions?.[0]?.fix).toEqual({ + range: [0, 3], + text: "COPY", + }); }); it("should convert a lone RUN cd to WORKDIR", () => { @@ -70,17 +76,10 @@ describe("autofix", () => { it("should apply fixes from multiple rules at once", () => { const result = fix( - [ - "from node:20", - "maintainer jane", - "add src/ /app/", - "cmd npm start", - "", - ].join("\n"), + ["from node:20", "maintainer jane", "cmd npm start", ""].join("\n"), { "docker/consistent-instruction-casing": "error", "docker/no-maintainer": "error", - "docker/prefer-copy-over-add": "error", "docker/prefer-exec-form": "error", }, ); @@ -90,7 +89,6 @@ describe("autofix", () => { [ "FROM node:20", 'LABEL maintainer="jane"', - "COPY src/ /app/", 'CMD ["npm", "start"]', "", ].join("\n"), From e6893b88a16d5d91557dc7700c2b242d139b93b3 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Fri, 20 Nov 2026 10:36:34 -0500 Subject: [PATCH 5/7] test: expect incomplete JSON arrays to be treated as shell form Co-Authored-By: Claude Opus 5.5 --- tests/plugin.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts index 4fea75f..0335b22 100644 --- a/tests/plugin.test.ts +++ b/tests/plugin.test.ts @@ -95,8 +95,11 @@ describe("plugin integration", () => { ); expect(messages.filter(message => message.fatal)).toEqual([]); - // Only the deprecated MAINTAINER keyword is a real problem here. + // Docker runs an incomplete JSON array as a shell command, so the + // CMD and ENTRYPOINT lines are shell form. expect(messages.map(message => message.ruleId)).toEqual([ + "docker/prefer-exec-form", + "docker/prefer-exec-form", "docker/no-maintainer", ]); }); From 9afd7027f1ea571c36ea70c090141b1f95c5b32e Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Fri, 20 Nov 2026 10:46:29 -0500 Subject: [PATCH 6/7] test: expect a suggestion rather than a fix for RUN cd Co-Authored-By: Claude Opus 5.5 --- tests/autofix.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/autofix.test.ts b/tests/autofix.test.ts index 3fa790f..27e6234 100644 --- a/tests/autofix.test.ts +++ b/tests/autofix.test.ts @@ -58,12 +58,18 @@ describe("autofix", () => { }); }); - it("should convert a lone RUN cd to WORKDIR", () => { + it("should suggest, not autofix, converting a lone RUN cd to WORKDIR", () => { const result = fix("RUN cd /app\n", { "docker/no-cd-in-run": "error", }); - expect(result.output).toBe("WORKDIR /app\n"); + expect(result.fixed).toBe(false); + expect(result.output).toBe("RUN cd /app\n"); + expect(result.messages).toHaveLength(1); + expect(result.messages[0]?.suggestions?.[0]?.fix).toEqual({ + range: [0, 11], + text: "WORKDIR /app", + }); }); it("should convert shell form CMD to exec form", () => { From 841eedf5ac61b6fb20120acf83222d93437509e8 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 24 Nov 2026 17:23:28 -0500 Subject: [PATCH 7/7] docs: add JSDoc tags to integration test helpers Co-Authored-By: Claude Opus 5.5 --- tests/autofix.test.ts | 7 +++++++ tests/directives.test.ts | 6 ++++++ tests/plugin.test.ts | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/tests/autofix.test.ts b/tests/autofix.test.ts index 27e6234..d98ebdd 100644 --- a/tests/autofix.test.ts +++ b/tests/autofix.test.ts @@ -6,6 +6,13 @@ import { describe, expect, it } from "vitest"; import { Linter } from "eslint"; import docker from "../src/index.js"; +/** + * Lints Dockerfile code and applies all available fixes. + * @param code The Dockerfile code to fix. + * @param rules The rule configuration to use. + * @returns The fix report, including the fixed code and the remaining + * messages. + */ function fix(code: string, rules: Record): Linter.FixReport { const linter = new Linter(); diff --git a/tests/directives.test.ts b/tests/directives.test.ts index ce8aef9..277435d 100644 --- a/tests/directives.test.ts +++ b/tests/directives.test.ts @@ -6,6 +6,12 @@ import { describe, expect, it } from "vitest"; import { Linter } from "eslint"; import docker from "../src/index.js"; +/** + * Lints Dockerfile code with the plugin loaded. + * @param code The Dockerfile code to lint. + * @param rules The rule configuration to use. + * @returns The lint messages. + */ function lint( code: string, rules: Record, diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts index 0335b22..b786145 100644 --- a/tests/plugin.test.ts +++ b/tests/plugin.test.ts @@ -13,6 +13,12 @@ const baseConfig = { language: "docker/dockerfile", } as const; +/** + * Lints Dockerfile code with the plugin loaded. + * @param code The Dockerfile code to lint. + * @param rules The rule configuration to use. Defaults to no rules. + * @returns The lint messages. + */ function lint( code: string, rules: Record = {},