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 { test } from "../testing/mod.ts";
|
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync
|
|
|
|
} 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");
|
2019-09-09 13:58:36 -04:00
|
|
|
const isWindows = Deno.build.os === "win";
|
2019-04-21 15:23:44 -04:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureSymlinkIfItNotExist(): 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");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await ensureSymlink(testFile, path.join(testDir, "test1.txt"));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
assertThrowsAsync(
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureSymlinkSyncIfItNotExist(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureSymlinkIfItExist(): 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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): Promise<void> => ensureSymlink(testFile, linkFile),
|
2019-04-21 15:23:44 -04:00
|
|
|
Error,
|
|
|
|
"Not implemented"
|
|
|
|
);
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
await ensureSymlink(testFile, linkFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
const srcStat = await Deno.lstat(testFile);
|
|
|
|
const linkStat = await Deno.lstat(linkFile);
|
|
|
|
|
|
|
|
assertEquals(srcStat.isFile(), true);
|
|
|
|
assertEquals(linkStat.isSymlink(), true);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureSymlinkSyncIfItExist(): 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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => ensureSymlinkSync(testFile, linkFile),
|
2019-04-21 15:23:44 -04:00
|
|
|
Error,
|
|
|
|
"Not implemented"
|
|
|
|
);
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ensureSymlinkSync(testFile, linkFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
const srcStat = Deno.lstatSync(testFile);
|
|
|
|
|
|
|
|
const linkStat = Deno.lstatSync(linkFile);
|
|
|
|
|
|
|
|
assertEquals(srcStat.isFile(), true);
|
|
|
|
assertEquals(linkStat.isSymlink(), true);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function ensureSymlinkDirectoryIfItExist(): 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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
await assertThrowsAsync(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): Promise<void> => ensureSymlink(testDir, linkDir),
|
2019-04-21 15:23:44 -04:00
|
|
|
Error,
|
|
|
|
"Not implemented"
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
|
|
|
|
assertEquals(testFileStat.isFile(), true);
|
|
|
|
assertEquals(testDirStat.isDirectory(), true);
|
|
|
|
assertEquals(linkDirStat.isSymlink(), true);
|
|
|
|
|
|
|
|
await Deno.remove(linkDir, { recursive: true });
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function ensureSymlinkSyncDirectoryIfItExist(): 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());
|
|
|
|
|
|
|
|
if (isWindows) {
|
|
|
|
assertThrows(
|
2019-04-24 07:41:23 -04:00
|
|
|
(): void => ensureSymlinkSync(testDir, linkDir),
|
2019-04-21 15:23:44 -04:00
|
|
|
Error,
|
|
|
|
"Not implemented"
|
|
|
|
);
|
|
|
|
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);
|
|
|
|
|
|
|
|
assertEquals(testFileStat.isFile(), true);
|
|
|
|
assertEquals(testDirStat.isDirectory(), true);
|
|
|
|
assertEquals(linkDirStat.isSymlink(), true);
|
|
|
|
|
|
|
|
Deno.removeSync(linkDir, { recursive: true });
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|