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,
|
|
|
|
assertThrowsAsync
|
|
|
|
} 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-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function ensureLinkIfItNotExist(): 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);
|
|
|
|
}
|
|
|
|
);
|
2019-04-22 11:35:14 -04:00
|
|
|
|
|
|
|
await Deno.remove(destDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function ensureLinkSyncIfItNotExist(): 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-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function ensureLinkIfItExist(): 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);
|
|
|
|
|
|
|
|
assertEquals(srcStat.isFile(), true);
|
|
|
|
assertEquals(linkStat.isFile(), true);
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
await Deno.readFile(testFile)
|
|
|
|
);
|
|
|
|
const linkFileContent1 = new TextDecoder().decode(
|
|
|
|
await Deno.readFile(testFile)
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent1, "123");
|
|
|
|
assertEquals(testFileContent1, linkFileContent1);
|
|
|
|
|
|
|
|
// let's change link file.
|
|
|
|
await Deno.writeFile(testFile, new TextEncoder().encode("abc"));
|
|
|
|
|
|
|
|
const testFileContent2 = new TextDecoder().decode(
|
|
|
|
await Deno.readFile(testFile)
|
|
|
|
);
|
|
|
|
const linkFileContent2 = new TextDecoder().decode(
|
|
|
|
await Deno.readFile(testFile)
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent2, "abc");
|
|
|
|
assertEquals(testFileContent2, linkFileContent2);
|
|
|
|
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(function ensureLinkSyncIfItExist(): 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);
|
|
|
|
|
|
|
|
assertEquals(srcStat.isFile(), true);
|
|
|
|
assertEquals(linkStat.isFile(), true);
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
Deno.readFileSync(testFile)
|
|
|
|
);
|
|
|
|
const linkFileContent1 = new TextDecoder().decode(
|
|
|
|
Deno.readFileSync(testFile)
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent1, "123");
|
|
|
|
assertEquals(testFileContent1, linkFileContent1);
|
|
|
|
|
|
|
|
// let's change link file.
|
|
|
|
Deno.writeFileSync(testFile, new TextEncoder().encode("abc"));
|
|
|
|
|
|
|
|
const testFileContent2 = new TextDecoder().decode(
|
|
|
|
Deno.readFileSync(testFile)
|
|
|
|
);
|
|
|
|
const linkFileContent2 = new TextDecoder().decode(
|
|
|
|
Deno.readFileSync(testFile)
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(testFileContent2, "abc");
|
|
|
|
assertEquals(testFileContent2, linkFileContent2);
|
|
|
|
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function ensureLinkDirectoryIfItExist(): 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-02-21 10:36:13 -05: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-02-11 11:24:27 -05:00
|
|
|
Deno.test(function ensureLinkSyncDirectoryIfItExist(): 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-02-21 10:36:13 -05: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 });
|
|
|
|
});
|