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
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: { read: true } }, function dirCwdNotNull() {
|
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(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function dirCwdChdirSuccess() {
|
2020-05-02 18:33:43 -04:00
|
|
|
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
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: { read: true, write: true } }, function dirCwdError() {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-22 19:50:50 -04:00
|
|
|
unitTest({ permissions: { read: false } }, function dirCwdPermError() {
|
2020-05-29 11:27:43 -04:00
|
|
|
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(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ permissions: { read: true, write: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function dirChdirError() {
|
2020-05-02 18:33:43 -04:00
|
|
|
const path = Deno.makeTempDirSync() + "test";
|
2021-10-11 09:21:18 -04:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.chdir(path);
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`chdir '${path}'`,
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-05-02 18:33:43 -04:00
|
|
|
);
|