2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2019-04-22 15:35:14 +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-22 15:35:14 +00:00
|
|
|
import { ensureLink, ensureLinkSync } from "./ensure_link.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-22 15:35:14 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureLink() rejects if src and dest do not exist", async function () {
|
2019-04-22 15:35:14 +00:00
|
|
|
const srcDir = path.join(testdataDir, "ensure_link_1");
|
|
|
|
const destDir = path.join(testdataDir, "ensure_link_1_2");
|
|
|
|
const testFile = path.join(srcDir, "test.txt");
|
|
|
|
const linkFile = path.join(destDir, "link.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 ensureLink(testFile, linkFile);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-04-22 15:35:14 +00:00
|
|
|
|
|
|
|
await Deno.remove(destDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureLinkSync() throws if src and dest do not exist", function () {
|
2019-04-22 15:35:14 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_link_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
const linkFile = path.join(testDir, "link.txt");
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
ensureLinkSync(testFile, linkFile);
|
|
|
|
});
|
2019-04-22 15:35:14 +00:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureLink() ensures dest links to the src", async function () {
|
2019-04-22 15:35:14 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_link_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-22 15:35:14 +00:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
await ensureLink(testFile, linkFile);
|
|
|
|
|
|
|
|
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.isFile, true);
|
2019-04-22 15:35:14 +00:00
|
|
|
|
|
|
|
// har link success. try to change one of them. they should be change both.
|
|
|
|
|
|
|
|
// let's change origin file.
|
2023-11-03 04:41:18 +00:00
|
|
|
await Deno.writeTextFile(testFile, "123");
|
2019-04-22 15:35:14 +00:00
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
const testFileContent1 = await Deno.readTextFile(testFile);
|
|
|
|
const linkFileContent1 = await Deno.readTextFile(testFile);
|
2019-04-22 15:35:14 +00:00
|
|
|
|
|
|
|
assertEquals(testFileContent1, "123");
|
|
|
|
assertEquals(testFileContent1, linkFileContent1);
|
|
|
|
|
|
|
|
// let's change link file.
|
|
|
|
await Deno.writeFile(testFile, new TextEncoder().encode("abc"));
|
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
const testFileContent2 = await Deno.readTextFile(testFile);
|
|
|
|
const linkFileContent2 = await Deno.readTextFile(testFile);
|
2019-04-22 15:35:14 +00:00
|
|
|
|
|
|
|
assertEquals(testFileContent2, "abc");
|
|
|
|
assertEquals(testFileContent2, linkFileContent2);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureLinkSync() ensures dest links to the src", function () {
|
2019-04-22 15:35:14 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_link_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-22 15:35:14 +00:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
ensureLinkSync(testFile, linkFile);
|
|
|
|
|
|
|
|
const srcStat = Deno.lstatSync(testFile);
|
|
|
|
|
|
|
|
const linkStat = Deno.lstatSync(linkFile);
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(srcStat.isFile, true);
|
|
|
|
assertEquals(linkStat.isFile, true);
|
2019-04-22 15:35:14 +00:00
|
|
|
|
|
|
|
// har link success. try to change one of them. they should be change both.
|
|
|
|
|
|
|
|
// let's change origin file.
|
|
|
|
Deno.writeFileSync(testFile, new TextEncoder().encode("123"));
|
|
|
|
|
|
|
|
const testFileContent1 = new TextDecoder().decode(
|
2020-07-14 19:24:17 +00:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 15:35:14 +00:00
|
|
|
);
|
|
|
|
const linkFileContent1 = new TextDecoder().decode(
|
2020-07-14 19:24:17 +00:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 15:35:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent1, "123");
|
|
|
|
assertEquals(testFileContent1, linkFileContent1);
|
|
|
|
|
|
|
|
// let's change link file.
|
|
|
|
Deno.writeFileSync(testFile, new TextEncoder().encode("abc"));
|
|
|
|
|
|
|
|
const testFileContent2 = new TextDecoder().decode(
|
2020-07-14 19:24:17 +00:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 15:35:14 +00:00
|
|
|
);
|
|
|
|
const linkFileContent2 = new TextDecoder().decode(
|
2020-07-14 19:24:17 +00:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 15:35:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent2, "abc");
|
|
|
|
assertEquals(testFileContent2, linkFileContent2);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureLink() rejects if link does not exist", async function () {
|
2019-04-22 15:35:14 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_link_origin_3");
|
|
|
|
const linkDir = path.join(testdataDir, "ensure_link_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-22 15:35:14 +00:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-04-22 15:35:14 +00:00
|
|
|
await ensureLink(testDir, linkDir);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-27 17:14:56 +00:00
|
|
|
// "Operation not permitted (os error 1)" // throw an local matching test
|
|
|
|
// "Access is denied. (os error 5)" // throw in CI
|
2019-04-22 15:35:14 +00:00
|
|
|
);
|
|
|
|
|
2024-03-12 00:36:10 +00:00
|
|
|
await Deno.remove(testDir, { recursive: true });
|
2019-04-22 15:35:14 +00:00
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("ensureLinkSync() throws if link does not exist", function () {
|
2019-04-22 15:35:14 +00:00
|
|
|
const testDir = path.join(testdataDir, "ensure_link_origin_3");
|
|
|
|
const linkDir = path.join(testdataDir, "ensure_link_link_3");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-01-07 19:14:33 +00:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-22 15:35:14 +00:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2019-04-22 15:35:14 +00:00
|
|
|
ensureLinkSync(testDir, linkDir);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-27 17:14:56 +00:00
|
|
|
// "Operation not permitted (os error 1)" // throw an local matching test
|
|
|
|
// "Access is denied. (os error 5)" // throw in CI
|
2019-04-22 15:35:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|