From 53fa55937f241fc9ef3091eb22314cc370c749b9 Mon Sep 17 00:00:00 2001 From: Kaoru0822 Date: Tue, 6 Oct 2026 00:03:29 -0700 Subject: [PATCH] fix(reference): bound Dockerfile language detection --- src/domain/referenceSourceClassification.ts | 7 +- .../referenceSourceDockerfileLanguage.test.ts | 51 ++++++++ .../referenceSourceDockerfileLanguage.test.ts | 117 ++++++++++++++++++ 3 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 src/domain/referenceSourceDockerfileLanguage.test.ts create mode 100644 tests/boundary/filesystem/referenceSourceDockerfileLanguage.test.ts diff --git a/src/domain/referenceSourceClassification.ts b/src/domain/referenceSourceClassification.ts index 212bafb46..6468a598d 100644 --- a/src/domain/referenceSourceClassification.ts +++ b/src/domain/referenceSourceClassification.ts @@ -321,11 +321,10 @@ const isPathUnderAny = (path: string, names: ReadonlySet): boolean => { /** Detect a source language from a POSIX path. */ export const detectReferenceSourceLanguage = (path: string): string | null => { const filename = posix.basename(path); - if (filename === "Dockerfile" || filename.startsWith("Dockerfile.")) - return "Dockerfile"; - if (filename.toLowerCase().startsWith("dockerfile")) return "Dockerfile"; - const lower = filename.toLowerCase(); + if (lower === "dockerfile" || lower.startsWith("dockerfile.")) + return "Dockerfile"; + if (lower.endsWith(".d.ts")) return "TypeScript"; if (lower.endsWith(".d.mts")) return "TypeScript"; if (lower.endsWith(".d.cts")) return "TypeScript"; diff --git a/src/domain/referenceSourceDockerfileLanguage.test.ts b/src/domain/referenceSourceDockerfileLanguage.test.ts new file mode 100644 index 000000000..b9c718a05 --- /dev/null +++ b/src/domain/referenceSourceDockerfileLanguage.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, it } from "vitest"; + +import { detectReferenceSourceLanguage } from "./referenceSourceClassification.js"; + +describe("reference source Dockerfile language detection", () => { + it.each([ + ["DockerfileGuide.ts", "TypeScript"], + ["nested/dockerfileHelper.py", "Python"], + ["docs/dockerfiles.md", "Markdown"], + ["DOCKERFILE_HELPER.JS", "JavaScript"], + ["Dockerfile-Guide.tsx", "TSX"], + ["DockerfileGuide.d.ts", "TypeScript"], + ["DockerfileGuide.d.mts", "TypeScript"], + ["DockerfileGuide.d.cts", "TypeScript"], + ["dockerfiles", null], + ["dockerfileHelper.unknown", null], + ])( + "uses the extension for unrelated prefix filename %s", + (path, language) => { + expect(detectReferenceSourceLanguage(path)).toBe(language); + }, + ); + + it.each([ + "Dockerfile", + "dockerfile", + "DOCKERFILE", + "nested/DoCkErFiLe", + "Dockerfile.dev", + "Dockerfile.dev.ts", + "nested/dockerfile.dev.ts", + "DOCKERFILE.DEV.TS", + "Dockerfile.", + ])("preserves exact and dotted Dockerfile recognition for %s", (path) => { + expect(detectReferenceSourceLanguage(path)).toBe("Dockerfile"); + }); + + it.each([ + ["ordinary.ts", "TypeScript"], + ["ordinary.TS", "TypeScript"], + ["ordinary.d.mts", "TypeScript"], + ["Dockerfile/main.js", "JavaScript"], + ["README.md", "Markdown"], + ["package.json", "JSON"], + ["Containerfile", null], + [".dockerfile", null], + ["unknown", null], + ])("preserves other filename handling for %s", (path, language) => { + expect(detectReferenceSourceLanguage(path)).toBe(language); + }); +}); diff --git a/tests/boundary/filesystem/referenceSourceDockerfileLanguage.test.ts b/tests/boundary/filesystem/referenceSourceDockerfileLanguage.test.ts new file mode 100644 index 000000000..5742af64f --- /dev/null +++ b/tests/boundary/filesystem/referenceSourceDockerfileLanguage.test.ts @@ -0,0 +1,117 @@ +import { createHash } from "node:crypto"; +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; + +import { expect, it } from "vitest"; + +import { importReferenceSource } from "../../../src/application/ReferenceSourceImport.js"; +import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; + +it("imports Dockerfile-prefixed TypeScript with ordinary source evidence and uncertainty", async () => { + const root = await createTestTempDirectory("rea-dockerfile-language-"); + const source = [ + 'import "./dep.js";', + 'import "./missing.js";', + 'const target: string = "./dep.js";', + "void import(target);", + ].join("\n"); + const sourcePaths = ["DockerfileGuide.ts", "ordinary.ts"]; + const dockerfilePaths = ["Dockerfile", "dockerfile.dev.ts"]; + await Promise.all([ + ...[...sourcePaths, ...dockerfilePaths].map((path) => + writeFile(join(root, path), source), + ), + writeFile(join(root, "dep.js"), "export {};\n"), + ]); + + const result = await importReferenceSource({ + root, + caller: "dockerfile-language-test", + policy: { secretPatterns: [] }, + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + const graph = result.value; + expect(graph.parse_failures).toEqual([]); + expect(graph.relationships).toHaveLength(sourcePaths.length * 3); + for (const path of sourcePaths) { + expect( + graph.relationships.filter(({ from_path }) => from_path === path), + ).toEqual([ + { + from_path: path, + to: "", + kind: "imports", + resolution: "unknown", + parse_state: "partial", + }, + { + from_path: path, + to: "dep.js", + kind: "imports", + resolution: "internal", + parse_state: "parsed", + }, + { + from_path: path, + to: "missing.js", + kind: "imports", + resolution: "unresolved", + parse_state: "parsed", + }, + ]); + } + for (const path of [...sourcePaths, ...dockerfilePaths]) { + expect(graph.entries).toContainEqual({ + path, + kind: "file", + sha256: createHash("sha256").update(source).digest("hex"), + size: Buffer.byteLength(source), + language: sourcePaths.includes(path) ? "TypeScript" : "Dockerfile", + classifications: path === "Dockerfile" ? ["manifest"] : ["source"], + content_state: "hashed", + limitations: [], + }); + } + expect(graph.languages).toEqual(["Dockerfile", "JavaScript", "TypeScript"]); + expect(graph.manifests).toEqual(["Dockerfile"]); + expect(graph.inventory_state).toBe("partial"); +}); + +it("retains parser failures and hashes for malformed Dockerfile-prefixed TypeScript", async () => { + const root = await createTestTempDirectory("rea-dockerfile-parse-failure-"); + const paths = ["DockerfileGuide.ts", "ordinary.ts"]; + const source = 'import "./dep.js";\nconst = ;\n'; + await Promise.all(paths.map((path) => writeFile(join(root, path), source))); + const result = await importReferenceSource({ + root, + caller: "dockerfile-language-test", + policy: { secretPatterns: [] }, + }); + expect(result.ok).toBe(true); + if (!result.ok) return; + expect(result.value.relationships).toEqual([]); + expect(result.value.parse_failures).toEqual( + paths.map((path) => ({ + path, + parser: "babel", + reason: expect.stringContaining("Unexpected token"), + })), + ); + expect( + new Set(result.value.parse_failures.map(({ reason }) => reason)).size, + ).toBe(1); + for (const path of paths) { + expect(result.value.entries).toContainEqual({ + path, + kind: "file", + sha256: createHash("sha256").update(source).digest("hex"), + size: Buffer.byteLength(source), + language: "TypeScript", + classifications: ["source"], + content_state: "hashed", + limitations: [], + }); + } + expect(result.value.inventory_state).toBe("partial"); +});