2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 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.
|
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
2020-03-28 17:03:49 +00:00
|
|
|
assertThrowsAsync,
|
2019-04-21 19:23:44 +00:00
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 18:39:33 +00:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-04-21 19:23:44 +00:00
|
|
|
import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
2019-09-09 17:58:36 +00:00
|
|
|
const isWindows = Deno.build.os === "win";
|
2019-04-21 19:23:44 +00:00
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureSymlinkIfItNotExist", async function (): Promise<void> {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_1");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2020-03-18 23:25:55 +00:00
|
|
|
await assertThrowsAsync(
|
2019-04-24 11:41:23 +00:00
|
|
|
async (): Promise<void> => {
|
|
|
|
await ensureSymlink(testFile, path.join(testDir, "test1.txt"));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-18 23:25:55 +00:00
|
|
|
await assertThrowsAsync(
|
2019-04-24 11:41:23 +00:00
|
|
|
async (): Promise<void> => {
|
2019-11-13 18:42:34 +00:00
|
|
|
await Deno.stat(testFile).then((): void => {
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-04-24 11:41:23 +00:00
|
|
|
}
|
|
|
|
);
|
2019-04-21 19:23:44 +00:00
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureSymlinkSyncIfItNotExist", function (): void {
|
2019-04-21 19:23:44 +00:00
|
|
|
const testDir = path.join(testdataDir, "link_file_2");
|
|
|
|
const testFile = path.join(testDir, "test.txt");
|
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertThrows((): void => {
|
|
|
|
ensureSymlinkSync(testFile, path.join(testDir, "test1.txt"));
|
|
|
|
});
|
2019-04-21 19:23:44 +00:00
|
|
|
|
2019-11-13 18:42:34 +00:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testFile);
|
|
|
|
throw new Error("test file should exists.");
|
|
|
|
});
|
2019-04-21 19:23:44 +00:00
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureSymlinkIfItExist", async function (): Promise<void> {
|
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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 11:41:23 +00:00
|
|
|
(): Promise<void> => ensureSymlink(testFile, linkFile),
|
2019-04-21 19:23:44 +00:00
|
|
|
Error,
|
2020-03-20 16:10:02 +00:00
|
|
|
"not implemented"
|
2019-04-21 19:23:44 +00:00
|
|
|
);
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
await ensureSymlink(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.isSymlink, true);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureSymlinkSyncIfItExist", function (): void {
|
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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
assertThrows(
|
2019-04-24 11:41:23 +00:00
|
|
|
(): void => ensureSymlinkSync(testFile, linkFile),
|
2019-04-21 19:23:44 +00:00
|
|
|
Error,
|
2020-03-20 16:10:02 +00:00
|
|
|
"not implemented"
|
2019-04-21 19:23:44 +00:00
|
|
|
);
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ensureSymlinkSync(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.isSymlink, true);
|
2019-04-21 19:23:44 +00:00
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureSymlinkDirectoryIfItExist", async function (): Promise<void> {
|
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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 11:41:23 +00:00
|
|
|
(): Promise<void> => ensureSymlink(testDir, linkDir),
|
2019-04-21 19:23:44 +00:00
|
|
|
Error,
|
2020-03-20 16:10:02 +00:00
|
|
|
"not implemented"
|
2019-04-21 19:23:44 +00:00
|
|
|
);
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
await ensureSymlink(testDir, linkDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
|
|
|
});
|
|
|
|
|
2020-04-28 10:33:09 +00:00
|
|
|
Deno.test("ensureSymlinkSyncDirectoryIfItExist", function (): void {
|
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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
assertThrows(
|
2019-04-24 11:41:23 +00:00
|
|
|
(): void => ensureSymlinkSync(testDir, linkDir),
|
2019-04-21 19:23:44 +00:00
|
|
|
Error,
|
2020-03-20 16:10:02 +00:00
|
|
|
"not implemented"
|
2019-04-21 19:23:44 +00:00
|
|
|
);
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ensureSymlinkSync(testDir, linkDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
|
|
|
});
|