2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-21 15:23:44 -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-21 15:23:44 -04:00
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-04-21 15:23:44 -04:00
|
|
|
import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureSymlinkIfItNotExist", async function (): Promise<void> {
|
2019-04-21 15:23:44 -04:00
|
|
|
const testDir = path.join(testdataDir, "link_file_1");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
|
|
|
await ensureSymlink(testFile, path.join(testDir, "test1.txt"));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
async (): Promise<void> => {
|
2019-11-13 13:42:34 -05:00
|
|
|
await Deno.stat(testFile).then((): void => {
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-04-24 07:41:23 -04:00
|
|
|
}
|
|
|
|
);
|
2019-04-21 15:23:44 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureSymlinkSyncIfItNotExist", function (): void {
|
2019-04-21 15:23:44 -04:00
|
|
|
const testDir = path.join(testdataDir, "link_file_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
ensureSymlinkSync(testFile, path.join(testDir, "test1.txt"));
|
|
|
|
});
|
2019-04-21 15:23:44 -04:00
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testFile);
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-04-21 15:23:44 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureSymlinkIfItExist", async function (): Promise<void> {
|
2019-04-21 15:23:44 -04: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 14:14:33 -05:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-21 15:23:44 -04:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
2020-05-18 18:46:02 -04:00
|
|
|
await ensureSymlink(testFile, linkFile);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
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.isSymlink, true);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureSymlinkSyncIfItExist", function (): void {
|
2019-04-21 15:23:44 -04: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 14:14:33 -05:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-21 15:23:44 -04:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
2020-05-18 18:46:02 -04:00
|
|
|
ensureSymlinkSync(testFile, linkFile);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
const srcStat = Deno.lstatSync(testFile);
|
|
|
|
|
|
|
|
const linkStat = Deno.lstatSync(linkFile);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(srcStat.isFile, true);
|
|
|
|
assertEquals(linkStat.isSymlink, true);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureSymlinkDirectoryIfItExist", async function (): Promise<void> {
|
2019-04-21 15:23:44 -04: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 14:14:33 -05:00
|
|
|
await Deno.mkdir(testDir, { recursive: true });
|
2019-04-21 15:23:44 -04:00
|
|
|
await Deno.writeFile(testFile, new Uint8Array());
|
|
|
|
|
2020-05-18 18:46:02 -04:00
|
|
|
await ensureSymlink(testDir, linkDir);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
const testDirStat = await Deno.lstat(testDir);
|
|
|
|
const linkDirStat = await Deno.lstat(linkDir);
|
|
|
|
const testFileStat = await Deno.lstat(testFile);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(testFileStat.isFile, true);
|
|
|
|
assertEquals(testDirStat.isDirectory, true);
|
|
|
|
assertEquals(linkDirStat.isSymlink, true);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
await Deno.remove(linkDir, { recursive: true });
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("ensureSymlinkSyncDirectoryIfItExist", function (): void {
|
2019-04-21 15:23:44 -04: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 14:14:33 -05:00
|
|
|
Deno.mkdirSync(testDir, { recursive: true });
|
2019-04-21 15:23:44 -04:00
|
|
|
Deno.writeFileSync(testFile, new Uint8Array());
|
|
|
|
|
2020-05-18 18:46:02 -04:00
|
|
|
ensureSymlinkSync(testDir, linkDir);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
const testDirStat = Deno.lstatSync(testDir);
|
|
|
|
const linkDirStat = Deno.lstatSync(linkDir);
|
|
|
|
const testFileStat = Deno.lstatSync(testFile);
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(testFileStat.isFile, true);
|
|
|
|
assertEquals(testDirStat.isDirectory, true);
|
|
|
|
assertEquals(linkDirStat.isSymlink, true);
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
Deno.removeSync(linkDir, { recursive: true });
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|