2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-24 18:57:08 -04:00
|
|
|
import { unitTest, assert, assertEquals, assertThrows } from "./test_util.ts";
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function linkSyncSuccess(): void {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldData = "Hardlink";
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(oldData));
|
|
|
|
// Create the hard link.
|
|
|
|
Deno.linkSync(oldName, newName);
|
|
|
|
// We should expect reading the same content.
|
|
|
|
const newData = new TextDecoder().decode(Deno.readFileSync(newName));
|
|
|
|
assertEquals(oldData, newData);
|
|
|
|
// Writing to newname also affects oldname.
|
|
|
|
const newData2 = "Modified";
|
|
|
|
Deno.writeFileSync(newName, new TextEncoder().encode(newData2));
|
|
|
|
assertEquals(
|
|
|
|
newData2,
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(oldName))
|
|
|
|
);
|
|
|
|
// Writing to oldname also affects newname.
|
|
|
|
const newData3 = "ModifiedAgain";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(newData3));
|
|
|
|
assertEquals(
|
|
|
|
newData3,
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName))
|
|
|
|
);
|
|
|
|
// Remove oldname. File still accessible through newname.
|
|
|
|
Deno.removeSync(oldName);
|
|
|
|
const newNameStat = Deno.statSync(newName);
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(newNameStat.isFile);
|
|
|
|
assert(!newNameStat.isSymlink); // Not a symlink.
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(
|
|
|
|
newData3,
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function linkSyncExists(): void {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode("oldName"));
|
|
|
|
// newname is already created.
|
|
|
|
Deno.writeFileSync(newName, new TextEncoder().encode("newName"));
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.linkSync(oldName, newName);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.AlreadyExists);
|
2019-04-08 09:11:32 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function linkSyncNotFound(): void {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.linkSync(oldName, newName);
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2019-04-08 09:11:32 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: false, write: true } },
|
|
|
|
function linkSyncReadPerm(): void {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.linkSync("oldbaddir", "newbaddir");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-08-13 09:39:01 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-08-13 09:39:01 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: false } },
|
|
|
|
function linkSyncWritePerm(): void {
|
2020-06-24 18:57:08 -04:00
|
|
|
assertThrows(() => {
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.linkSync("oldbaddir", "newbaddir");
|
2020-06-24 18:57:08 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2019-04-08 09:11:32 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-04-08 09:11:32 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
async function linkSuccess(): Promise<void> {
|
|
|
|
const testDir = Deno.makeTempDirSync();
|
|
|
|
const oldData = "Hardlink";
|
|
|
|
const oldName = testDir + "/oldname";
|
|
|
|
const newName = testDir + "/newname";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(oldData));
|
|
|
|
// Create the hard link.
|
|
|
|
await Deno.link(oldName, newName);
|
|
|
|
// We should expect reading the same content.
|
|
|
|
const newData = new TextDecoder().decode(Deno.readFileSync(newName));
|
|
|
|
assertEquals(oldData, newData);
|
|
|
|
// Writing to newname also affects oldname.
|
|
|
|
const newData2 = "Modified";
|
|
|
|
Deno.writeFileSync(newName, new TextEncoder().encode(newData2));
|
|
|
|
assertEquals(
|
|
|
|
newData2,
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(oldName))
|
|
|
|
);
|
|
|
|
// Writing to oldname also affects newname.
|
|
|
|
const newData3 = "ModifiedAgain";
|
|
|
|
Deno.writeFileSync(oldName, new TextEncoder().encode(newData3));
|
|
|
|
assertEquals(
|
|
|
|
newData3,
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName))
|
|
|
|
);
|
|
|
|
// Remove oldname. File still accessible through newname.
|
|
|
|
Deno.removeSync(oldName);
|
|
|
|
const newNameStat = Deno.statSync(newName);
|
2020-04-16 01:40:30 -04:00
|
|
|
assert(newNameStat.isFile);
|
|
|
|
assert(!newNameStat.isSymlink); // Not a symlink.
|
2020-03-04 11:31:14 -05:00
|
|
|
assertEquals(
|
|
|
|
newData3,
|
|
|
|
new TextDecoder().decode(Deno.readFileSync(newName))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|