|
| 1 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join, parse } from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { copyDirSync } from "./copyDir.js"; |
| 6 | + |
| 7 | +const made: string[] = []; |
| 8 | +afterEach(() => { |
| 9 | + for (const dir of made.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 10 | +}); |
| 11 | + |
| 12 | +function source(): string { |
| 13 | + const dir = mkdtempSync(join(tmpdir(), "copydir-src-")); |
| 14 | + made.push(dir); |
| 15 | + mkdirSync(join(dir, "compositions", "café"), { recursive: true }); |
| 16 | + writeFileSync(join(dir, "index.html"), "<html></html>"); |
| 17 | + writeFileSync(join(dir, "compositions", "café", "scène.html"), "<div></div>"); |
| 18 | + return dir; |
| 19 | +} |
| 20 | + |
| 21 | +describe("copyDirSync", () => { |
| 22 | + it("copies every file, non-ASCII names included, into a folder that already holds others", () => { |
| 23 | + const dest = mkdtempSync(join(tmpdir(), "copydir-dest-")); |
| 24 | + made.push(dest); |
| 25 | + writeFileSync(join(dest, "hyperframes.json"), "{}"); |
| 26 | + copyDirSync(source(), dest); |
| 27 | + expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>"); |
| 28 | + expect(readFileSync(join(dest, "compositions", "café", "scène.html"), "utf8")).toBe( |
| 29 | + "<div></div>", |
| 30 | + ); |
| 31 | + expect(readFileSync(join(dest, "hyperframes.json"), "utf8")).toBe("{}"); |
| 32 | + }); |
| 33 | + |
| 34 | + // windows-latest: fs.cpSync into this folder ends the process with 0xC0000409, so this test fails without the fix. |
| 35 | + it.skipIf(process.platform !== "win32")( |
| 36 | + "copies into an existing folder directly under a drive root", |
| 37 | + () => { |
| 38 | + const dest = join(parse(tmpdir()).root, `copydir-root-${process.pid}-${Date.now()}`); |
| 39 | + mkdirSync(dest); |
| 40 | + made.push(dest); |
| 41 | + copyDirSync(source(), dest); |
| 42 | + expect(readFileSync(join(dest, "index.html"), "utf8")).toBe("<html></html>"); |
| 43 | + }, |
| 44 | + ); |
| 45 | +}); |
0 commit comments