|
| 1 | +// Copyright 2018-2025 the Deno authors. MIT license. |
| 2 | + |
| 3 | +import { assertEquals, assertRejects, assertThrows } from "@std/assert"; |
| 4 | +import { readLink, readLinkSync } from "./unstable_read_link.ts"; |
| 5 | +import { NotFound } from "./unstable_errors.js"; |
| 6 | +import { |
| 7 | + linkSync, |
| 8 | + mkdtempSync, |
| 9 | + rmSync, |
| 10 | + symlinkSync, |
| 11 | + writeFileSync, |
| 12 | +} from "node:fs"; |
| 13 | +import { link, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; |
| 14 | +import { tmpdir } from "node:os"; |
| 15 | +import { join, resolve } from "node:path"; |
| 16 | + |
| 17 | +Deno.test("readLink() can read through symlink", async () => { |
| 18 | + const tempDirPath = await mkdtemp(resolve(tmpdir(), "readLink_")); |
| 19 | + const testFile = join(tempDirPath, "testFile.txt"); |
| 20 | + const symlinkFile = join(tempDirPath, "testFile.txt.link"); |
| 21 | + |
| 22 | + await writeFile(testFile, "Hello, Standard Library"); |
| 23 | + await symlink(testFile, symlinkFile); |
| 24 | + |
| 25 | + const realFile = await readLink(symlinkFile); |
| 26 | + assertEquals(testFile, realFile); |
| 27 | + |
| 28 | + await rm(tempDirPath, { recursive: true, force: true }); |
| 29 | +}); |
| 30 | + |
| 31 | +Deno.test("readLink() rejects with Error when reading from a hard link", async () => { |
| 32 | + const tempDirPath = await mkdtemp(resolve(tmpdir(), "readLink_")); |
| 33 | + const testFile = join(tempDirPath, "testFile.txt"); |
| 34 | + const linkFile = join(tempDirPath, "testFile.txt.hlink"); |
| 35 | + |
| 36 | + await writeFile(testFile, "Hello, Standard Library"); |
| 37 | + await link(testFile, linkFile); |
| 38 | + |
| 39 | + await assertRejects(async () => { |
| 40 | + await readLink(linkFile); |
| 41 | + }, Error); |
| 42 | + |
| 43 | + await rm(tempDirPath, { recursive: true, force: true }); |
| 44 | +}); |
| 45 | + |
| 46 | +Deno.test("readLink() rejects with NotFound when reading through a non-existent file", async () => { |
| 47 | + await assertRejects(async () => { |
| 48 | + await readLink("non-existent-file.txt.link"); |
| 49 | + }, NotFound); |
| 50 | +}); |
| 51 | + |
| 52 | +Deno.test("readLinkSync() can read through symlink", () => { |
| 53 | + const tempDirPath = mkdtempSync(resolve(tmpdir(), "readLink_")); |
| 54 | + const testFile = join(tempDirPath, "testFile.txt"); |
| 55 | + const symlinkFile = join(tempDirPath, "testFile.txt.link"); |
| 56 | + |
| 57 | + writeFileSync(testFile, "Hello, Standard Library"); |
| 58 | + symlinkSync(testFile, symlinkFile); |
| 59 | + |
| 60 | + const realFile = readLinkSync(symlinkFile); |
| 61 | + assertEquals(testFile, realFile); |
| 62 | + |
| 63 | + rmSync(tempDirPath, { recursive: true, force: true }); |
| 64 | +}); |
| 65 | + |
| 66 | +Deno.test("readLinkSync() throws Error when reading from a hard link", () => { |
| 67 | + const tempDirPath = mkdtempSync(resolve(tmpdir(), "readLinkSync_")); |
| 68 | + const testFile = join(tempDirPath, "testFile.txt"); |
| 69 | + const linkFile = join(tempDirPath, "testFile.txt.hlink"); |
| 70 | + |
| 71 | + writeFileSync(testFile, "Hello, Standard Library!"); |
| 72 | + linkSync(testFile, linkFile); |
| 73 | + |
| 74 | + assertThrows(() => { |
| 75 | + readLinkSync(linkFile); |
| 76 | + }, Error); |
| 77 | + |
| 78 | + rmSync(tempDirPath, { recursive: true, force: true }); |
| 79 | +}); |
| 80 | + |
| 81 | +Deno.test("readLinkSync() throws NotFound when reading through a non-existent file", () => { |
| 82 | + assertThrows(() => { |
| 83 | + readLinkSync("non-existent-file.txt.hlink"); |
| 84 | + }, NotFound); |
| 85 | +}); |
0 commit comments