2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 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.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "../assert/mod.ts";
|
2019-10-16 18:39:33 +00:00
|
|
|
import * as path from "../path/mod.ts";
|
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
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("ensureLinkIfItNotExist", 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 });
|
|
|
|
});
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
Deno.test("ensureLinkSyncIfItNotExist", 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 });
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("ensureLinkIfItExist", 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 });
|
|
|
|
});
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
Deno.test("ensureLinkSyncIfItExist", 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 });
|
|
|
|
});
|
|
|
|
|
2021-04-05 11:49:05 +00:00
|
|
|
Deno.test("ensureLinkDirectoryIfItExist", 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
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
Deno.test("ensureLinkSyncDirectoryIfItExist", 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 });
|
|
|
|
});
|