|
| 1 | +// Copyright 2018-2025 the Deno authors. MIT license. |
| 2 | + |
| 3 | +import { assert, assertRejects, assertThrows } from "@std/assert"; |
| 4 | +import { symlink, symlinkSync } from "./unstable_symlink.ts"; |
| 5 | +import { AlreadyExists } from "./unstable_errors.js"; |
| 6 | +import { lstat, mkdir, mkdtemp, open, rm, stat } from "node:fs/promises"; |
| 7 | +import { |
| 8 | + closeSync, |
| 9 | + lstatSync, |
| 10 | + mkdirSync, |
| 11 | + mkdtempSync, |
| 12 | + openSync, |
| 13 | + rmSync, |
| 14 | + statSync, |
| 15 | +} from "node:fs"; |
| 16 | +import { tmpdir } from "node:os"; |
| 17 | +import { dirname, join, resolve } from "node:path"; |
| 18 | +import { fileURLToPath } from "node:url"; |
| 19 | + |
| 20 | +const moduleDir = dirname(fileURLToPath(import.meta.url)); |
| 21 | +const testdataDir = resolve(moduleDir, "testdata"); |
| 22 | + |
| 23 | +Deno.test("symlink() creates a link to a regular file", async () => { |
| 24 | + const tempDirPath = await mkdtemp(resolve(tmpdir(), "symlink_")); |
| 25 | + const testFile = join(tempDirPath, "testFile.txt"); |
| 26 | + const symlinkPath = join(tempDirPath, "testFile.txt.link"); |
| 27 | + |
| 28 | + const tempFh = await open(testFile, "w"); |
| 29 | + await symlink(testFile, symlinkPath); |
| 30 | + |
| 31 | + const symlinkLstat = await lstat(symlinkPath); |
| 32 | + const fileStat = await stat(testFile); |
| 33 | + |
| 34 | + assert(symlinkLstat.isSymbolicLink); |
| 35 | + assert(fileStat.isFile); |
| 36 | + |
| 37 | + await tempFh.close(); |
| 38 | + await rm(tempDirPath, { recursive: true, force: true }); |
| 39 | +}); |
| 40 | + |
| 41 | +Deno.test("symlink() creates a link to a directory", async () => { |
| 42 | + const tempDirPath = await mkdtemp(resolve(tmpdir(), "symlink_")); |
| 43 | + const testDir = join(tempDirPath, "testDir"); |
| 44 | + const symlinkPath = join(tempDirPath, "testDir.link"); |
| 45 | + |
| 46 | + await mkdir(testDir); |
| 47 | + await symlink(testDir, symlinkPath); |
| 48 | + |
| 49 | + const symlinkLstat = await lstat(symlinkPath); |
| 50 | + const dirStat = await stat(testDir); |
| 51 | + |
| 52 | + assert(symlinkLstat.isSymbolicLink); |
| 53 | + assert(dirStat.isDirectory); |
| 54 | + |
| 55 | + await rm(tempDirPath, { recursive: true, force: true }); |
| 56 | +}); |
| 57 | + |
| 58 | +Deno.test( |
| 59 | + "symlink() rejects with AlreadyExists for creating the same link path to the same file path", |
| 60 | + async () => { |
| 61 | + const existingFile = join(testdataDir, "0.ts"); |
| 62 | + const existingSymlink = join(testdataDir, "0-link"); |
| 63 | + |
| 64 | + await assertRejects(async () => { |
| 65 | + await symlink(existingFile, existingSymlink); |
| 66 | + }, AlreadyExists); |
| 67 | + }, |
| 68 | +); |
| 69 | + |
| 70 | +Deno.test( |
| 71 | + "symlinkSync() creates a link to a regular file", |
| 72 | + () => { |
| 73 | + const tempDirPath = mkdtempSync(resolve(tmpdir(), "symlinkSync_")); |
| 74 | + const filePath = join(tempDirPath, "testFile.txt"); |
| 75 | + const symlinkPath = join(tempDirPath, "testFile.txt.link"); |
| 76 | + |
| 77 | + const tempFd = openSync(filePath, "w"); |
| 78 | + symlinkSync(filePath, symlinkPath); |
| 79 | + |
| 80 | + const symlinkLstat = lstatSync(symlinkPath); |
| 81 | + const fileStat = statSync(filePath); |
| 82 | + |
| 83 | + assert(symlinkLstat.isSymbolicLink); |
| 84 | + assert(fileStat.isFile); |
| 85 | + |
| 86 | + closeSync(tempFd); |
| 87 | + rmSync(tempDirPath, { recursive: true, force: true }); |
| 88 | + }, |
| 89 | +); |
| 90 | + |
| 91 | +Deno.test("symlinkSync() creates a link to a directory", () => { |
| 92 | + const tempDirPath = mkdtempSync(resolve(tmpdir(), "symlinkSync_")); |
| 93 | + const testDir = join(tempDirPath, "testDir"); |
| 94 | + const symlinkPath = join(tempDirPath, "testDir.link"); |
| 95 | + |
| 96 | + mkdirSync(testDir); |
| 97 | + symlinkSync(testDir, symlinkPath); |
| 98 | + |
| 99 | + const symlinkLstat = lstatSync(symlinkPath); |
| 100 | + const dirStat = statSync(testDir); |
| 101 | + |
| 102 | + assert(symlinkLstat.isSymbolicLink); |
| 103 | + assert(dirStat.isDirectory); |
| 104 | + |
| 105 | + rmSync(tempDirPath, { recursive: true, force: true }); |
| 106 | +}); |
| 107 | + |
| 108 | +Deno.test( |
| 109 | + "symlinkSync() throws with AlreadyExists for creating the same link path to the same file path", |
| 110 | + () => { |
| 111 | + const existingFile = join(testdataDir, "0.ts"); |
| 112 | + const existingSymlink = join(testdataDir, "0-link"); |
| 113 | + |
| 114 | + assertThrows(() => { |
| 115 | + symlinkSync(existingFile, existingSymlink); |
| 116 | + }, AlreadyExists); |
| 117 | + }, |
| 118 | +); |
0 commit comments