2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
import { test, testPerm, assert, assertEquals } from "./test_util.ts";
|
2018-10-13 16:03:27 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function dirCwdNotNull(): void {
|
2019-02-12 10:08:56 -05:00
|
|
|
assert(Deno.cwd() != null);
|
2018-10-13 16:03:27 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: true }, function dirCwdChdirSuccess(): void {
|
2019-02-12 10:08:56 -05:00
|
|
|
const initialdir = Deno.cwd();
|
|
|
|
const path = Deno.makeTempDirSync();
|
|
|
|
Deno.chdir(path);
|
|
|
|
const current = Deno.cwd();
|
2019-03-06 16:54:58 -05:00
|
|
|
if (Deno.build.os === "mac") {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(current, "/private" + path);
|
2018-10-13 16:03:27 -04:00
|
|
|
} else {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(current, path);
|
2018-10-13 16:03:27 -04:00
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chdir(initialdir);
|
2018-10-13 16:03:27 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: true }, function dirCwdError(): void {
|
2018-10-13 16:03:27 -04:00
|
|
|
// excluding windows since it throws resource busy, while removeSync
|
2019-03-06 16:54:58 -05:00
|
|
|
if (["linux", "mac"].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 {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.cwd();
|
2018-10-13 16:03:27 -04:00
|
|
|
throw Error("current directory removed, should throw error");
|
|
|
|
} catch (err) {
|
2020-02-24 15:48:35 -05:00
|
|
|
if (err instanceof Deno.errors.NotFound) {
|
2018-10-13 16:03:27 -04:00
|
|
|
console.log(err.name === "NotFound");
|
|
|
|
} else {
|
|
|
|
throw Error("raised different exception");
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chdir(initialdir);
|
2018-10-13 16:03:27 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: true }, function dirChdirError(): void {
|
2019-02-12 10:08:56 -05:00
|
|
|
const path = Deno.makeTempDirSync() + "test";
|
2018-10-13 16:03:27 -04:00
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.chdir(path);
|
2018-10-13 16:03:27 -04:00
|
|
|
throw Error("directory not available, should throw error");
|
|
|
|
} catch (err) {
|
2020-02-24 15:48:35 -05:00
|
|
|
if (err instanceof Deno.errors.NotFound) {
|
2018-10-13 16:03:27 -04:00
|
|
|
console.log(err.name === "NotFound");
|
|
|
|
} else {
|
|
|
|
throw Error("raised different exception");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|