2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assert, assertEquals, assertThrows } from "./test_util.ts";
|
2018-10-13 16:03:27 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test({ 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
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
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-11-23 11:45:18 -05:00
|
|
|
Deno.test({ 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-11-23 11:45:18 -05:00
|
|
|
Deno.test({ 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
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
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
|
|
|
);
|