deno/js/read_link_test.ts

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-01-21 19:03:30 +00:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEqual } from "./test_util.ts";
2018-09-25 04:20:49 +00:00
import * as deno from "deno";
testPerm({ write: true, read: true }, function readlinkSyncSuccess() {
const testDir = deno.makeTempDirSync();
2018-09-25 04:20:49 +00:00
const target = testDir + "/target";
const symlink = testDir + "/symln";
deno.mkdirSync(target);
// TODO Add test for Windows once symlink is implemented for Windows.
// See https://github.com/denoland/deno/issues/815.
2018-10-04 11:20:14 +00:00
if (deno.platform.os !== "win") {
2018-09-25 04:20:49 +00:00
deno.symlinkSync(target, symlink);
const targetPath = deno.readlinkSync(symlink);
assertEqual(targetPath, target);
}
});
testPerm({ read: false }, async function readlinkSyncPerm() {
let caughtError = false;
try {
deno.readlinkSync("/symlink");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true }, function readlinkSyncNotFound() {
2018-09-25 04:20:49 +00:00
let caughtError = false;
let data;
try {
data = deno.readlinkSync("bad_filename");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.NotFound);
}
assert(caughtError);
assertEqual(data, undefined);
});
testPerm({ write: true, read: true }, async function readlinkSuccess() {
const testDir = deno.makeTempDirSync();
2018-09-25 04:20:49 +00:00
const target = testDir + "/target";
const symlink = testDir + "/symln";
deno.mkdirSync(target);
// TODO Add test for Windows once symlink is implemented for Windows.
// See https://github.com/denoland/deno/issues/815.
2018-10-04 11:20:14 +00:00
if (deno.platform.os !== "win") {
2018-09-25 04:20:49 +00:00
deno.symlinkSync(target, symlink);
const targetPath = await deno.readlink(symlink);
assertEqual(targetPath, target);
}
});
testPerm({ read: false }, async function readlinkPerm() {
let caughtError = false;
try {
await deno.readlink("/symlink");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});