mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 00:29:09 -05:00
a21a5ad2fa
Resolves #1705 This PR adds the Deno APIs as a global namespace named `Deno`. For backwards compatibility, the ability to `import * from "deno"` is preserved. I have tried to convert every test and internal code the references the module to use the namespace instead, but because I didn't break compatibility I am not sure. On the REPL, `deno` no longer exists, replaced only with `Deno` to align with the regular runtime. The runtime type library includes both the namespace and module. This means it duplicates the whole type information. When we remove the functionality from the runtime, it will be a one line change to the library generator to remove the module definition from the type library. I marked a `TODO` in a couple places where to remove the `"deno"` module, but there are additional places I know I didn't mark.
335 lines
9.4 KiB
TypeScript
335 lines
9.4 KiB
TypeScript
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
import { testPerm, assert, assertEqual } from "./test_util.ts";
|
|
|
|
// SYNC
|
|
|
|
testPerm({ write: true }, function removeSyncDirSuccess() {
|
|
// REMOVE EMPTY DIRECTORY
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
Deno.mkdirSync(path);
|
|
const pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
Deno.removeSync(path); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// Directory is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, function removeSyncFileSuccess() {
|
|
// REMOVE FILE
|
|
const enc = new TextEncoder();
|
|
const data = enc.encode("Hello");
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
|
const fileInfo = Deno.statSync(filename);
|
|
assert(fileInfo.isFile()); // check exist first
|
|
Deno.removeSync(filename); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(filename);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// File is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, function removeSyncFail() {
|
|
// NON-EMPTY DIRECTORY
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
const subPath = path + "/subsubdir";
|
|
Deno.mkdirSync(path);
|
|
Deno.mkdirSync(subPath);
|
|
const pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
const subPathInfo = Deno.statSync(subPath);
|
|
assert(subPathInfo.isDirectory()); // check exist first
|
|
let err;
|
|
try {
|
|
// Should not be able to recursively remove
|
|
Deno.removeSync(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// TODO(ry) Is Other really the error we should get here? What would Go do?
|
|
assertEqual(err.kind, Deno.ErrorKind.Other);
|
|
assertEqual(err.name, "Other");
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
try {
|
|
// Non-existent
|
|
Deno.removeSync("/baddir");
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: false }, function removeSyncPerm() {
|
|
let err;
|
|
try {
|
|
Deno.removeSync("/baddir");
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
assertEqual(err.name, "PermissionDenied");
|
|
});
|
|
|
|
testPerm({ write: true }, function removeAllSyncDirSuccess() {
|
|
// REMOVE EMPTY DIRECTORY
|
|
let path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
Deno.mkdirSync(path);
|
|
let pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
Deno.removeSync(path, { recursive: true }); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// Directory is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
// REMOVE NON-EMPTY DIRECTORY
|
|
path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
const subPath = path + "/subsubdir";
|
|
Deno.mkdirSync(path);
|
|
Deno.mkdirSync(subPath);
|
|
pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
const subPathInfo = Deno.statSync(subPath);
|
|
assert(subPathInfo.isDirectory()); // check exist first
|
|
Deno.removeSync(path, { recursive: true }); // remove
|
|
// We then check parent directory again after remove
|
|
try {
|
|
Deno.statSync(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// Directory is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, function removeAllSyncFileSuccess() {
|
|
// REMOVE FILE
|
|
const enc = new TextEncoder();
|
|
const data = enc.encode("Hello");
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
|
const fileInfo = Deno.statSync(filename);
|
|
assert(fileInfo.isFile()); // check exist first
|
|
Deno.removeSync(filename, { recursive: true }); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(filename);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// File is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, function removeAllSyncFail() {
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
let err;
|
|
try {
|
|
// Non-existent
|
|
Deno.removeSync("/baddir", { recursive: true });
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: false }, function removeAllSyncPerm() {
|
|
let err;
|
|
try {
|
|
Deno.removeSync("/baddir", { recursive: true });
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
assertEqual(err.name, "PermissionDenied");
|
|
});
|
|
|
|
// ASYNC
|
|
|
|
testPerm({ write: true }, async function removeDirSuccess() {
|
|
// REMOVE EMPTY DIRECTORY
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
Deno.mkdirSync(path);
|
|
const pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
await Deno.remove(path); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// Directory is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, async function removeFileSuccess() {
|
|
// REMOVE FILE
|
|
const enc = new TextEncoder();
|
|
const data = enc.encode("Hello");
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
|
const fileInfo = Deno.statSync(filename);
|
|
assert(fileInfo.isFile()); // check exist first
|
|
await Deno.remove(filename); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(filename);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// File is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, async function removeFail() {
|
|
// NON-EMPTY DIRECTORY
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
const subPath = path + "/subsubdir";
|
|
Deno.mkdirSync(path);
|
|
Deno.mkdirSync(subPath);
|
|
const pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
const subPathInfo = Deno.statSync(subPath);
|
|
assert(subPathInfo.isDirectory()); // check exist first
|
|
let err;
|
|
try {
|
|
// Should not be able to recursively remove
|
|
await Deno.remove(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.Other);
|
|
assertEqual(err.name, "Other");
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
try {
|
|
// Non-existent
|
|
await Deno.remove("/baddir");
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: false }, async function removePerm() {
|
|
let err;
|
|
try {
|
|
await Deno.remove("/baddir");
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
assertEqual(err.name, "PermissionDenied");
|
|
});
|
|
|
|
testPerm({ write: true }, async function removeAllDirSuccess() {
|
|
// REMOVE EMPTY DIRECTORY
|
|
let path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
Deno.mkdirSync(path);
|
|
let pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
await Deno.remove(path, { recursive: true }); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// Directory is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
// REMOVE NON-EMPTY DIRECTORY
|
|
path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
const subPath = path + "/subsubdir";
|
|
Deno.mkdirSync(path);
|
|
Deno.mkdirSync(subPath);
|
|
pathInfo = Deno.statSync(path);
|
|
assert(pathInfo.isDirectory()); // check exist first
|
|
const subPathInfo = Deno.statSync(subPath);
|
|
assert(subPathInfo.isDirectory()); // check exist first
|
|
await Deno.remove(path, { recursive: true }); // remove
|
|
// We then check parent directory again after remove
|
|
try {
|
|
Deno.statSync(path);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// Directory is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, async function removeAllFileSuccess() {
|
|
// REMOVE FILE
|
|
const enc = new TextEncoder();
|
|
const data = enc.encode("Hello");
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
|
const fileInfo = Deno.statSync(filename);
|
|
assert(fileInfo.isFile()); // check exist first
|
|
await Deno.remove(filename, { recursive: true }); // remove
|
|
// We then check again after remove
|
|
let err;
|
|
try {
|
|
Deno.statSync(filename);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
// File is gone
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: true }, async function removeAllFail() {
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
let err;
|
|
try {
|
|
// Non-existent
|
|
await Deno.remove("/baddir", { recursive: true });
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
|
assertEqual(err.name, "NotFound");
|
|
});
|
|
|
|
testPerm({ write: false }, async function removeAllPerm() {
|
|
let err;
|
|
try {
|
|
await Deno.remove("/baddir", { recursive: true });
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
|
assertEqual(err.name, "PermissionDenied");
|
|
});
|