File size: 2,921 Bytes
21dd449 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it, vi } from "vitest";
import { createSymlink } from "./symlink";
import { readFileSync, writeFileSync } from "node:fs";
import { lstat, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
let failSymlink = false;
vi.mock("node:fs/promises", async (importOriginal) => ({
...(await importOriginal<typeof import("node:fs/promises")>()),
symlink: async (...args: any[]) => {
if (failSymlink) {
failSymlink = false;
throw new Error("Symlink not supported");
}
// @ts-expect-error - ignore
return (await importOriginal<typeof import("node:fs/promises")>()).symlink(...args);
},
}));
describe("utils/symlink", () => {
it("should create a symlink", async () => {
writeFileSync(join(tmpdir(), "test.txt"), "hello world");
await createSymlink({
sourcePath: join(tmpdir(), "test.txt"),
finalPath: join(tmpdir(), "test-symlink.txt"),
});
const stats = await lstat(join(tmpdir(), "test-symlink.txt"));
expect(stats.isSymbolicLink()).toBe(process.platform !== "win32");
// Test file content
const content = readFileSync(join(tmpdir(), "test-symlink.txt"), "utf8");
expect(content).toBe("hello world");
// Cleanup
await rm(join(tmpdir(), "test-symlink.txt"));
await rm(join(tmpdir(), "test.txt"));
});
it("should work when symlinking twice", async () => {
writeFileSync(join(tmpdir(), "test.txt"), "hello world");
writeFileSync(join(tmpdir(), "test2.txt"), "hello world2");
await createSymlink({
sourcePath: join(tmpdir(), "test.txt"),
finalPath: join(tmpdir(), "test-symlink.txt"),
});
await createSymlink({
sourcePath: join(tmpdir(), "test2.txt"),
finalPath: join(tmpdir(), "test-symlink.txt"),
});
const stats = await lstat(join(tmpdir(), "test-symlink.txt"));
expect(stats.isSymbolicLink()).toBe(process.platform !== "win32");
// Test file content
const content = readFileSync(join(tmpdir(), "test-symlink.txt"), "utf8");
expect(content).toBe("hello world2");
// Cleanup
await rm(join(tmpdir(), "test-symlink.txt"));
await rm(join(tmpdir(), "test.txt"));
await rm(join(tmpdir(), "test2.txt"));
});
it("should work when symlink doesn't work (windows)", async () => {
writeFileSync(join(tmpdir(), "test.txt"), "hello world");
failSymlink = true;
await createSymlink({
sourcePath: join(tmpdir(), "test.txt"),
finalPath: join(tmpdir(), "test-symlink.txt"),
});
const stats = await lstat(join(tmpdir(), "test-symlink.txt"));
expect(stats.isSymbolicLink()).toBe(false);
// Test file content
const content = readFileSync(join(tmpdir(), "test-symlink.txt"), "utf8");
expect(content).toBe("hello world");
// Cleanup
await rm(join(tmpdir(), "test-symlink.txt"));
await rm(join(tmpdir(), "test.txt"));
});
});
|