2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2019-04-21 19:23:44 +00:00
|
|
|
// TODO(axetroy): Add test for Windows once symlink is implemented for Windows.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
|
|
|
|
import * as path from "@std/path";
|
2019-04-21 19:23:44 +00:00
|
|
|
import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
|
|
|
|
|
2020-09-08 09:43:43 +00:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = path.resolve(moduleDir, "testdata");
|
2019-04-21 19:23:44 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlink() rejects if file does not exist", async function () {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_1");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-04-24 11:41:23 +00:00
|
|
|
await ensureSymlink(testFile, path.join(testDir, "test1.txt"));
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
|
|
|
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2022-08-24 01:21:57 +00:00
|
|
|
await Deno.stat(testFile).then(() => {
|
2023-11-23 06:59:59 +00:00
|
|
|
throw new Error("test file should exist.");
|
2019-11-13 18:42:34 +00:00
|
|
|
});
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-04-21 19:23:44 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlinkSync() throws if file does not exist", function () {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
ensureSymlinkSync(testFile, path.join(testDir, "test1.txt"));
|
|
|
|
});
|
2019-04-21 19:23:44 +00:00
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
Deno.statSync(testFile);
|
2023-11-23 06:59:59 +00:00
|
|
|
throw new Error("test file should exist.");
|
2019-11-13 18:42:34 +00:00
|
|
|
});
|
2019-04-21 19:23:44 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlink() ensures linkName links to target", async function () {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_3");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
const linkFile = path.join(testDir, "link.txt");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-21 19:23:44 +00:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
2022-09-14 10:44:51 +00:00
|
|
|
await ensureSymlink(testFile, linkFile);
|
2020-05-18 22:46:02 +00:00
|
|
|
await ensureSymlink(testFile, linkFile);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
const srcStat = await Deno.lstat(testFile);
|
|
|
|
const linkStat = await Deno.lstat(linkFile);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(srcStat.isFile, true);
|
|
|
|
assertEquals(linkStat.isSymlink, true);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlinkSync() ensures linkName links to target", function () {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_4");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
const linkFile = path.join(testDir, "link.txt");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-21 19:23:44 +00:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
2022-09-14 10:44:51 +00:00
|
|
|
ensureSymlinkSync(testFile, linkFile);
|
2020-05-18 22:46:02 +00:00
|
|
|
ensureSymlinkSync(testFile, linkFile);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
const srcStat = Deno.lstatSync(testFile);
|
|
|
|
const linkStat = Deno.lstatSync(linkFile);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(srcStat.isFile, true);
|
|
|
|
assertEquals(linkStat.isSymlink, true);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2024-02-28 02:50:33 +00:00
|
|
|
Deno.test("ensureSymlink() rejects if the linkName path already exist", async function () {
|
|
|
|
const testDir = path.join(testdataDir, "link_file_5");
|
|
|
|
const linkFile = path.join(testDir, "test.txt");
|
|
|
|
const linkDir = path.join(testDir, "test_dir");
|
|
|
|
const linkSymlink = path.join(testDir, "test_symlink");
|
|
|
|
const targetFile = path.join(testDir, "target.txt");
|
|
|
|
|
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
|
|
|
await Deno.writeTextFile(linkFile, "linkFile");
|
|
|
|
await Deno.mkdir(linkDir);
|
|
|
|
await Deno.symlink("non-existent", linkSymlink, { type: "file" });
|
|
|
|
await Deno.writeTextFile(targetFile, "targetFile");
|
|
|
|
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await ensureSymlink(targetFile, linkFile);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await ensureSymlink(targetFile, linkDir);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await ensureSymlink(targetFile, linkSymlink);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(await Deno.readTextFile(linkFile), "linkFile");
|
|
|
|
assertEquals((await Deno.stat(linkDir)).isDirectory, true);
|
|
|
|
assertEquals(await Deno.readLink(linkSymlink), "non-existent");
|
|
|
|
assertEquals(await Deno.readTextFile(targetFile), "targetFile");
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("ensureSymlinkSync() throws if the linkName path already exist", function () {
|
|
|
|
const testDir = path.join(testdataDir, "link_file_6");
|
|
|
|
const linkFile = path.join(testDir, "test.txt");
|
|
|
|
const linkDir = path.join(testDir, "test_dir");
|
|
|
|
const linkSymlink = path.join(testDir, "test_symlink");
|
|
|
|
const targetFile = path.join(testDir, "target.txt");
|
|
|
|
|
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
|
|
|
Deno.writeTextFileSync(linkFile, "linkFile");
|
|
|
|
Deno.mkdirSync(linkDir);
|
|
|
|
Deno.symlinkSync("non-existent", linkSymlink, { type: "file" });
|
|
|
|
Deno.writeTextFileSync(targetFile, "targetFile");
|
|
|
|
|
|
|
|
assertThrows(() => {
|
|
|
|
ensureSymlinkSync(targetFile, linkFile);
|
|
|
|
});
|
|
|
|
assertThrows(() => {
|
|
|
|
ensureSymlinkSync(targetFile, linkDir);
|
|
|
|
});
|
|
|
|
assertThrows(() => {
|
|
|
|
ensureSymlinkSync(targetFile, linkSymlink);
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(Deno.readTextFileSync(linkFile), "linkFile");
|
|
|
|
assertEquals(Deno.statSync(linkDir).isDirectory, true);
|
|
|
|
assertEquals(Deno.readLinkSync(linkSymlink), "non-existent");
|
|
|
|
assertEquals(Deno.readTextFileSync(targetFile), "targetFile");
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlink() ensures dir linkName links to dir target", async function () {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_origin_3");
|
|
|
|
const linkDir = path.join(testdataDir, "link_file_link_3");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-21 19:23:44 +00:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
2022-09-14 10:44:51 +00:00
|
|
|
await ensureSymlink(testDir, linkDir);
|
2020-05-18 22:46:02 +00:00
|
|
|
await ensureSymlink(testDir, linkDir);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
const testDirStat = await Deno.lstat(testDir);
|
|
|
|
const linkDirStat = await Deno.lstat(linkDir);
|
|
|
|
const testFileStat = await Deno.lstat(testFile);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(testFileStat.isFile, true);
|
|
|
|
assertEquals(testDirStat.isDirectory, true);
|
|
|
|
assertEquals(linkDirStat.isSymlink, true);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
await Deno.remove(linkDir, { recursive: true });
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlinkSync() ensures dir linkName links to dir target", function () {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_origin_3");
|
|
|
|
const linkDir = path.join(testdataDir, "link_file_link_3");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-21 19:23:44 +00:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
2022-09-14 10:44:51 +00:00
|
|
|
ensureSymlinkSync(testDir, linkDir);
|
2020-05-18 22:46:02 +00:00
|
|
|
ensureSymlinkSync(testDir, linkDir);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
const testDirStat = Deno.lstatSync(testDir);
|
|
|
|
const linkDirStat = Deno.lstatSync(linkDir);
|
|
|
|
const testFileStat = Deno.lstatSync(testFile);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(testFileStat.isFile, true);
|
|
|
|
assertEquals(testDirStat.isDirectory, true);
|
|
|
|
assertEquals(linkDirStat.isSymlink, true);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
Deno.removeSync(linkDir, { recursive: true });
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
2023-02-28 21:24:46 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlink() creates symlink with relative target", async function () {
|
2023-02-28 21:24:46 +00:00
|
|
|
const testDir = path.join(testdataDir, "symlink-relative");
|
|
|
|
const testLinkName = path.join(testDir, "link.txt");
|
|
|
|
const testFile = path.join(testDir, "target.txt");
|
|
|
|
|
|
|
|
await Deno.mkdir(testDir);
|
|
|
|
|
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
await ensureSymlink("target.txt", testLinkName);
|
|
|
|
|
|
|
|
const testDirStat = await Deno.lstat(testDir);
|
|
|
|
const linkDirStat = await Deno.lstat(testLinkName);
|
|
|
|
const testFileStat = await Deno.lstat(testFile);
|
|
|
|
|
|
|
|
assertEquals(testFileStat.isFile, true);
|
|
|
|
assertEquals(testDirStat.isDirectory, true);
|
|
|
|
assertEquals(linkDirStat.isSymlink, true);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureSymlinkSync() creates symlink with relative target", function () {
|
2023-02-28 21:24:46 +00:00
|
|
|
const testDir = path.join(testdataDir, "symlink-relative-sync");
|
|
|
|
const testLinkName = path.join(testDir, "link.txt");
|
|
|
|
const testFile = path.join(testDir, "target.txt");
|
|
|
|
|
|
|
|
Deno.mkdirSync(testDir);
|
|
|
|
|
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
ensureSymlinkSync("target.txt", testLinkName);
|
|
|
|
|
|
|
|
const testDirStat = Deno.lstatSync(testDir);
|
|
|
|
const linkDirStat = Deno.lstatSync(testLinkName);
|
|
|
|
const testFileStat = Deno.lstatSync(testFile);
|
|
|
|
|
|
|
|
assertEquals(testFileStat.isFile, true);
|
|
|
|
assertEquals(testDirStat.isDirectory, true);
|
|
|
|
assertEquals(linkDirStat.isSymlink, true);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|