2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-27 06:22:32 -04:00
|
|
|
import { assert, assertEquals, assertThrows, unitTest } from "./test_util.ts";
|
2018-10-13 16:03:27 -04:00
|
|
|
|
2020-05-04 14:23:06 -04:00
|
|
|
unitTest({ perms: { read: true } }, function dirCwdNotNull(): void {
|
2019-02-12 10:08:56 -05:00
|
|
|
assert(Deno.cwd() != null);
|
2018-10-13 16:03:27 -04:00
|
|
|
});
|
|
|
|
|
2020-05-02 18:33:43 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function dirCwdChdirSuccess(): void {
|
|
|
|
const initialdir = Deno.cwd();
|
|
|
|
const path = Deno.makeTempDirSync();
|
|
|
|
Deno.chdir(path);
|
|
|
|
const current = Deno.cwd();
|
|
|
|
if (Deno.build.os === "darwin") {
|
|
|
|
assertEquals(current, "/private" + path);
|
|
|
|
} else {
|
|
|
|
assertEquals(current, path);
|
|
|
|
}
|
|
|
|
Deno.chdir(initialdir);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-02 18:33:43 -04:00
|
|
|
);
|
2018-10-13 16:03:27 -04:00
|
|
|
|
2020-05-02 18:33:43 -04:00
|
|
|
unitTest({ perms: { read: true, write: true } }, function dirCwdError(): void {
|
2018-10-13 16:03:27 -04:00
|
|
|
// excluding windows since it throws resource busy, while removeSync
|
2020-04-28 12:35:23 -04:00
|
|
|
if (["linux", "darwin"].includes(Deno.build.os)) {
|
2019-02-12 10:08:56 -05:00
|
|
|
const initialdir = Deno.cwd();
|
|
|
|
const path = Deno.makeTempDirSync();
|
|
|
|
Deno.chdir(path);
|
|
|
|
Deno.removeSync(path);
|
2018-10-13 16:03:27 -04:00
|
|
|
try {
|
2020-05-29 11:27:43 -04:00
|
|
|
assertThrows(() => {
|
|
|
|
Deno.cwd();
|
|
|
|
}, Deno.errors.NotFound);
|
|
|
|
} finally {
|
|
|
|
Deno.chdir(initialdir);
|
2018-10-13 16:03:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-29 11:27:43 -04:00
|
|
|
unitTest({ perms: { read: false } }, function dirCwdPermError(): void {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.cwd();
|
|
|
|
},
|
|
|
|
Deno.errors.PermissionDenied,
|
2021-03-17 17:45:12 -04:00
|
|
|
"Requires read access to <CWD>, run again with the --allow-read flag",
|
2020-05-29 11:27:43 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-05-02 18:33:43 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { read: true, write: true } },
|
|
|
|
function dirChdirError(): void {
|
|
|
|
const path = Deno.makeTempDirSync() + "test";
|
2020-05-29 11:27:43 -04:00
|
|
|
assertThrows(() => {
|
2020-05-02 18:33:43 -04:00
|
|
|
Deno.chdir(path);
|
2020-05-29 11:27:43 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-02 18:33:43 -04:00
|
|
|
);
|