|
| 1 | +import { mkdtemp, readFile, rm, stat, unlink } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +import { FileSystemReportFiles } from "../src/plugin.js"; |
| 8 | + |
| 9 | +describe("FileSystemReportFiles", () => { |
| 10 | + const temporaryDirectories: string[] = []; |
| 11 | + |
| 12 | + afterEach(async () => { |
| 13 | + for (const directoryPath of temporaryDirectories) { |
| 14 | + await rm(directoryPath, { recursive: true, force: true }); |
| 15 | + } |
| 16 | + temporaryDirectories.length = 0; |
| 17 | + }); |
| 18 | + |
| 19 | + const createWriter = async () => { |
| 20 | + const outputDirectory = await mkdtemp(join(tmpdir(), "allure-core-plugin-test-")); |
| 21 | + |
| 22 | + temporaryDirectories.push(outputDirectory); |
| 23 | + |
| 24 | + return { |
| 25 | + outputDirectory, |
| 26 | + writer: new FileSystemReportFiles(outputDirectory), |
| 27 | + }; |
| 28 | + }; |
| 29 | + |
| 30 | + it("uses hardlinks for equal payloads written into different paths", async () => { |
| 31 | + const { writer } = await createWriter(); |
| 32 | + |
| 33 | + const sourcePath = await writer.addFile("report1/main.js", Buffer.from("shared-content", "utf8")); |
| 34 | + const targetPath = await writer.addFile("report2/main.js", Buffer.from("shared-content", "utf8")); |
| 35 | + const sourceStat = await stat(sourcePath); |
| 36 | + const targetStat = await stat(targetPath); |
| 37 | + |
| 38 | + expect(await readFile(sourcePath, "utf8")).toBe("shared-content"); |
| 39 | + expect(await readFile(targetPath, "utf8")).toBe("shared-content"); |
| 40 | + |
| 41 | + if (process.platform !== "win32") { |
| 42 | + expect(sourceStat.ino).toBe(targetStat.ino); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + it("rewrites only a target file when shared link receives a different payload", async () => { |
| 47 | + const { writer } = await createWriter(); |
| 48 | + |
| 49 | + const sourcePath = await writer.addFile("report1/main.css", Buffer.from("same-content", "utf8")); |
| 50 | + const targetPath = await writer.addFile("report2/main.css", Buffer.from("same-content", "utf8")); |
| 51 | + |
| 52 | + await writer.addFile("report2/main.css", Buffer.from("updated-content", "utf8")); |
| 53 | + |
| 54 | + expect(await readFile(sourcePath, "utf8")).toBe("same-content"); |
| 55 | + expect(await readFile(targetPath, "utf8")).toBe("updated-content"); |
| 56 | + |
| 57 | + if (process.platform !== "win32") { |
| 58 | + const sourceStat = await stat(sourcePath); |
| 59 | + const targetStat = await stat(targetPath); |
| 60 | + |
| 61 | + expect(sourceStat.ino).not.toBe(targetStat.ino); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it("falls back to regular write when canonical hardlink source disappears", async () => { |
| 66 | + const { writer } = await createWriter(); |
| 67 | + |
| 68 | + const canonicalPath = await writer.addFile("report1/asset.js", Buffer.from("content-to-share", "utf8")); |
| 69 | + |
| 70 | + await unlink(canonicalPath); |
| 71 | + |
| 72 | + const fallbackPath = await writer.addFile("report2/asset.js", Buffer.from("content-to-share", "utf8")); |
| 73 | + |
| 74 | + expect(await readFile(fallbackPath, "utf8")).toBe("content-to-share"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("does not rewrite file when path receives identical content again", async () => { |
| 78 | + const { writer } = await createWriter(); |
| 79 | + |
| 80 | + const targetPath = await writer.addFile("awesome/main.js", Buffer.from("stable-shared-asset", "utf8")); |
| 81 | + const firstStat = await stat(targetPath); |
| 82 | + |
| 83 | + await writer.addFile("awesome/main.js", Buffer.from("stable-shared-asset", "utf8")); |
| 84 | + |
| 85 | + if (process.platform !== "win32") { |
| 86 | + const secondStat = await stat(targetPath); |
| 87 | + |
| 88 | + expect(firstStat.ino).toBe(secondStat.ino); |
| 89 | + } |
| 90 | + }); |
| 91 | +}); |
0 commit comments