Merge pull request #694 from kaoru0822-kitauji/fix/dockerfile-language-boundary
fix(reference): bound Dockerfile language detection
This commit is contained in:
@@ -322,11 +322,10 @@ const isPathUnderAny = (path: string, names: ReadonlySet<string>): 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";
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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: "<dynamic-import>",
|
||||
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");
|
||||
});
|
||||
Reference in New Issue
Block a user