Something went wrong. Try again.
A custom fork of Quartz to generate my Digital Garden site
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238import test, { describe, beforeEach, afterEach, mock } from "node:test"import assert from "node:assert"import fs from "fs"import path from "path"import os from "os"import { symlinkOrCopySync, symlinkOrCopy } from "./helpers.js"
function makeTmpDir() { return fs.mkdtempSync(path.join(os.tmpdir(), "quartz-symlink-test-"))}
function makeTarget(tmpDir) { const target = path.join(tmpDir, "target") fs.mkdirSync(target) fs.writeFileSync(path.join(target, "marker.txt"), "hello") return target}
describe("symlinkOrCopySync", () => { let tmpDir
beforeEach(() => { tmpDir = makeTmpDir() })
afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }) })
test("creates a symlink on success", () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
symlinkOrCopySync(target, linkPath)
const stat = fs.lstatSync(linkPath) assert.ok(stat.isSymbolicLink()) assert.ok(fs.existsSync(path.join(linkPath, "marker.txt"))) })
test("silently succeeds when link already exists (EEXIST)", () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
symlinkOrCopySync(target, linkPath) assert.doesNotThrow(() => symlinkOrCopySync(target, linkPath)) })
test("re-throws non-EPERM errors", () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "nonexistent-parent", "link")
assert.throws( () => symlinkOrCopySync(target, linkPath), (err) => { return err.code === "ENOENT" }, ) })
test("falls back to junction on Windows EPERM", () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform") Object.defineProperty(process, "platform", { value: "win32", configurable: true })
let callCount = 0 const originalSymlinkSync = fs.symlinkSync mock.method(fs, "symlinkSync", (t, lp, type) => { callCount++ if (callCount === 1 && type === "dir") { const err = new Error("EPERM: operation not permitted, symlink") err.code = "EPERM" err.errno = -4048 err.syscall = "symlink" throw err } return originalSymlinkSync(t, lp, type) })
try { symlinkOrCopySync(target, linkPath) assert.ok(fs.existsSync(path.join(linkPath, "marker.txt"))) assert.strictEqual(callCount, 2) } finally { fs.symlinkSync.mock.restore() if (originalPlatform) { Object.defineProperty(process, "platform", originalPlatform) } } })
test("falls back to copy when both symlink and junction fail on Windows", () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform") Object.defineProperty(process, "platform", { value: "win32", configurable: true })
const originalSymlinkSync = fs.symlinkSync mock.method(fs, "symlinkSync", (_t, _lp, _type) => { const err = new Error("EPERM: operation not permitted, symlink") err.code = "EPERM" err.errno = -4048 err.syscall = "symlink" throw err })
try { symlinkOrCopySync(target, linkPath)
const stat = fs.lstatSync(linkPath) assert.ok(stat.isDirectory(), "fallback should produce a real directory, not a symlink") assert.strictEqual(fs.readFileSync(path.join(linkPath, "marker.txt"), "utf-8"), "hello") } finally { fs.symlinkSync.mock.restore() if (originalPlatform) { Object.defineProperty(process, "platform", originalPlatform) } } })
test("does not fall back on EPERM when not on Windows", () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform") Object.defineProperty(process, "platform", { value: "linux", configurable: true })
const originalSymlinkSync = fs.symlinkSync mock.method(fs, "symlinkSync", (_t, _lp, _type) => { const err = new Error("EPERM: operation not permitted, symlink") err.code = "EPERM" throw err })
try { assert.throws( () => symlinkOrCopySync(target, linkPath), (err) => err.code === "EPERM", ) } finally { fs.symlinkSync.mock.restore() if (originalPlatform) { Object.defineProperty(process, "platform", originalPlatform) } } })})
describe("symlinkOrCopy", () => { let tmpDir
beforeEach(() => { tmpDir = makeTmpDir() })
afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }) })
test("creates a symlink on success", async () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
await symlinkOrCopy(target, linkPath)
const stat = fs.lstatSync(linkPath) assert.ok(stat.isSymbolicLink()) assert.ok(fs.existsSync(path.join(linkPath, "marker.txt"))) })
test("silently succeeds when link already exists (EEXIST)", async () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
await symlinkOrCopy(target, linkPath) await assert.doesNotReject(() => symlinkOrCopy(target, linkPath)) })
test("falls back to copy when both symlink and junction fail on Windows", async () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform") Object.defineProperty(process, "platform", { value: "win32", configurable: true })
const originalSymlink = fs.promises.symlink mock.method(fs.promises, "symlink", async (_t, _lp, _type) => { const err = new Error("EPERM: operation not permitted, symlink") err.code = "EPERM" err.errno = -4048 err.syscall = "symlink" throw err })
try { await symlinkOrCopy(target, linkPath)
const stat = fs.lstatSync(linkPath) assert.ok(stat.isDirectory(), "fallback should produce a real directory, not a symlink") assert.strictEqual(fs.readFileSync(path.join(linkPath, "marker.txt"), "utf-8"), "hello") } finally { fs.promises.symlink.mock.restore() if (originalPlatform) { Object.defineProperty(process, "platform", originalPlatform) } } })
test("does not fall back on EPERM when not on Windows", async () => { const target = makeTarget(tmpDir) const linkPath = path.join(tmpDir, "link")
const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform") Object.defineProperty(process, "platform", { value: "linux", configurable: true })
mock.method(fs.promises, "symlink", async (_t, _lp, _type) => { const err = new Error("EPERM: operation not permitted, symlink") err.code = "EPERM" throw err })
try { await assert.rejects( () => symlinkOrCopy(target, linkPath), (err) => err.code === "EPERM", ) } finally { fs.promises.symlink.mock.restore() if (originalPlatform) { Object.defineProperty(process, "platform", originalPlatform) } } })})