2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-22 11:35:14 -04:00
|
|
|
// TODO(axetroy): Add test for Windows once symlink is implemented for Windows.
|
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertThrowsAsync,
|
2019-04-22 11:35:14 -04:00
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-04-22 11:35:14 -04:00
|
|
|
import { ensureLink, ensureLinkSync } from "./ensure_link.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureLinkIfItNotExist", async function (): Promise<void> {
|
2019-04-22 11:35:14 -04: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");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
2019-04-27 13:14:56 -04:00
|
|
|
async (): Promise<void> => {
|
2019-04-24 07:41:23 -04:00
|
|
|
await ensureLink(testFile, linkFile);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
);
|
2019-04-22 11:35:14 -04:00
|
|
|
|
|
|
|
await Deno.remove(destDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureLinkSyncIfItNotExist", function (): void {
|
2019-04-22 11:35:14 -04:00
|
|
|
const testDir = path.join(testdataDir, "ensure_link_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
const linkFile = path.join(testDir, "link.txt");
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
ensureLinkSync(testFile, linkFile);
|
|
|
|
});
|
2019-04-22 11:35:14 -04:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureLinkIfItExist", async function (): Promise<void> {
|
2019-04-22 11:35:14 -04: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 14:14:33 -05:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-22 11:35:14 -04: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 01:40:30 -04:00
|
|
|
assertEquals(srcStat.isFile, true);
|
|
|
|
assertEquals(linkStat.isFile, true);
|
2019-04-22 11:35:14 -04:00
|
|
|
|
|
|
|
// har link success. try to change one of them. they should be change both.
|
|
|
|
|
|
|
|
// let's change origin file.
|
|
|
|
await Deno.writeFile(testFile, new TextEncoder().encode("123"));
|
|
|
|
|
|
|
|
const testFileContent1 = new TextDecoder().decode(
|
2020-07-14 15:24:17 -04:00
|
|
|
await Deno.readFile(testFile),
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
const linkFileContent1 = new TextDecoder().decode(
|
2020-07-14 15:24:17 -04:00
|
|
|
await Deno.readFile(testFile),
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent1, "123");
|
|
|
|
assertEquals(testFileContent1, linkFileContent1);
|
|
|
|
|
|
|
|
// let's change link file.
|
|
|
|
await Deno.writeFile(testFile, new TextEncoder().encode("abc"));
|
|
|
|
|
|
|
|
const testFileContent2 = new TextDecoder().decode(
|
2020-07-14 15:24:17 -04:00
|
|
|
await Deno.readFile(testFile),
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
const linkFileContent2 = new TextDecoder().decode(
|
2020-07-14 15:24:17 -04:00
|
|
|
await Deno.readFile(testFile),
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent2, "abc");
|
|
|
|
assertEquals(testFileContent2, linkFileContent2);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureLinkSyncIfItExist", function (): void {
|
2019-04-22 11:35:14 -04: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 14:14:33 -05:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-22 11:35:14 -04:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
ensureLinkSync(testFile, linkFile);
|
|
|
|
|
|
|
|
const srcStat = Deno.lstatSync(testFile);
|
|
|
|
|
|
|
|
const linkStat = Deno.lstatSync(linkFile);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(srcStat.isFile, true);
|
|
|
|
assertEquals(linkStat.isFile, true);
|
2019-04-22 11:35:14 -04: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 15:24:17 -04:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
const linkFileContent1 = new TextDecoder().decode(
|
2020-07-14 15:24:17 -04:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 11:35:14 -04: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 15:24:17 -04:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
const linkFileContent2 = new TextDecoder().decode(
|
2020-07-14 15:24:17 -04:00
|
|
|
Deno.readFileSync(testFile),
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent2, "abc");
|
|
|
|
assertEquals(testFileContent2, linkFileContent2);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureLinkDirectoryIfItExist", async function (): Promise<void> {
|
2019-04-22 11:35:14 -04: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 14:14:33 -05:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-22 11:35:14 -04:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-04-22 11:35:14 -04:00
|
|
|
await ensureLink(testDir, linkDir);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-27 13:14:56 -04:00
|
|
|
// "Operation not permitted (os error 1)" // throw an local matching test
|
|
|
|
// "Access is denied. (os error 5)" // throw in CI
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureLinkSyncDirectoryIfItExist", function (): void {
|
2019-04-22 11:35:14 -04: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 14:14:33 -05:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-22 11:35:14 -04:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => {
|
2019-04-22 11:35:14 -04:00
|
|
|
ensureLinkSync(testDir, linkDir);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-27 13:14:56 -04:00
|
|
|
// "Operation not permitted (os error 1)" // throw an local matching test
|
|
|
|
// "Access is denied. (os error 5)" // throw in CI
|
2019-04-22 11:35:14 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|