diff --git a/tests/autofix.test.ts b/tests/autofix.test.ts new file mode 100644 index 0000000..d98ebdd --- /dev/null +++ b/tests/autofix.test.ts @@ -0,0 +1,111 @@ +/** + * @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"; + +/** + * 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(); + + 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 suggest, not autofix, converting ADD to COPY", () => { + const result = fix("ADD src/ /app/src/\n", { + "docker/prefer-copy-over-add": "error", + }); + + 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 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.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", () => { + 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", "cmd npm start", ""].join("\n"), + { + "docker/consistent-instruction-casing": "error", + "docker/no-maintainer": "error", + "docker/prefer-exec-form": "error", + }, + ); + + expect(result.fixed).toBe(true); + expect(result.output).toBe( + [ + "FROM node:20", + 'LABEL maintainer="jane"', + '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..277435d --- /dev/null +++ b/tests/directives.test.ts @@ -0,0 +1,120 @@ +/** + * @fileoverview Integration tests for inline configuration comments. + */ + +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, +): 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..b786145 --- /dev/null +++ b/tests/plugin.test.ts @@ -0,0 +1,228 @@ +/** + * @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; + +/** + * 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 = {}, +): 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 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([]); + // 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", + ]); + }); + + 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, + ); + + // 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"); + } + }); + + 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"); + }); +});