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 { testPerm, assert, assertEquals } from "./test_util.ts";
|
2018-09-10 23:40:03 -04:00
|
|
|
|
|
|
|
// SYNC
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm({ write: true, read: true }, function removeSyncDirSuccess(): void {
|
2018-09-10 23:40:03 -04:00
|
|
|
// REMOVE EMPTY DIRECTORY
|
2020-02-03 08:20:15 -05:00
|
|
|
const path = Deno.makeTempDirSync() + "/subdir";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(path);
|
|
|
|
const pathInfo = Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(pathInfo.isDirectory()); // check exist first
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync(path); // remove
|
2018-09-10 23:40:03 -04:00
|
|
|
// We then check again after remove
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// Directory is gone
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm({ write: true, read: true }, function removeSyncFileSuccess(): void {
|
2018-09-10 23:40:03 -04:00
|
|
|
// REMOVE FILE
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const data = enc.encode("Hello");
|
2019-02-12 10:08:56 -05:00
|
|
|
const filename = Deno.makeTempDirSync() + "/test.txt";
|
|
|
|
Deno.writeFileSync(filename, data, { perm: 0o666 });
|
|
|
|
const fileInfo = Deno.statSync(filename);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(fileInfo.isFile()); // check exist first
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync(filename); // remove
|
2018-09-10 23:40:03 -04:00
|
|
|
// We then check again after remove
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.statSync(filename);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// File is gone
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm({ write: true, read: true }, function removeSyncFail(): void {
|
2018-09-10 23:40:03 -04:00
|
|
|
// NON-EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
2018-09-10 23:40:03 -04:00
|
|
|
const subPath = path + "/subsubdir";
|
2020-02-03 08:20:15 -05:00
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(subPath);
|
|
|
|
const pathInfo = Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(pathInfo.isDirectory()); // check exist first
|
2019-02-12 10:08:56 -05:00
|
|
|
const subPathInfo = Deno.statSync(subPath);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(subPathInfo.isDirectory()); // check exist first
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
// Should not be able to recursively remove
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// TODO(ry) Is Other really the error we should get here? What would Go do?
|
2020-02-21 10:36:13 -05:00
|
|
|
assert(err instanceof Error);
|
2018-09-10 23:40:03 -04:00
|
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
|
|
try {
|
|
|
|
// Non-existent
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync("/baddir");
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
function removeSyncDanglingSymlinkSuccess(): void {
|
|
|
|
const danglingSymlinkPath = Deno.makeTempDirSync() + "/dangling_symlink";
|
|
|
|
// TODO(#3832): Remove "Not Implemented" error checking when symlink creation is implemented for Windows
|
|
|
|
let errOnWindows;
|
|
|
|
try {
|
|
|
|
Deno.symlinkSync("unexistent_file", danglingSymlinkPath);
|
|
|
|
} catch (err) {
|
|
|
|
errOnWindows = err;
|
|
|
|
}
|
|
|
|
if (Deno.build.os === "win") {
|
|
|
|
assertEquals(errOnWindows.message, "Not implemented");
|
|
|
|
} else {
|
|
|
|
const pathInfo = Deno.lstatSync(danglingSymlinkPath);
|
|
|
|
assert(pathInfo.isSymlink());
|
|
|
|
Deno.removeSync(danglingSymlinkPath);
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
Deno.lstatSync(danglingSymlinkPath);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2020-02-03 08:20:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
function removeSyncValidSymlinkSuccess(): void {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const data = encoder.encode("Test");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filePath = tempDir + "/test.txt";
|
|
|
|
const validSymlinkPath = tempDir + "/valid_symlink";
|
|
|
|
Deno.writeFileSync(filePath, data, { perm: 0o666 });
|
|
|
|
// TODO(#3832): Remove "Not Implemented" error checking when symlink creation is implemented for Windows
|
|
|
|
let errOnWindows;
|
|
|
|
try {
|
|
|
|
Deno.symlinkSync(filePath, validSymlinkPath);
|
|
|
|
} catch (err) {
|
|
|
|
errOnWindows = err;
|
|
|
|
}
|
|
|
|
if (Deno.build.os === "win") {
|
|
|
|
assertEquals(errOnWindows.message, "Not implemented");
|
|
|
|
} else {
|
|
|
|
const symlinkPathInfo = Deno.statSync(validSymlinkPath);
|
|
|
|
assert(symlinkPathInfo.isFile());
|
|
|
|
Deno.removeSync(validSymlinkPath);
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
Deno.statSync(validSymlinkPath);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
Deno.removeSync(filePath);
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2020-02-03 08:20:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: false }, function removeSyncPerm(): void {
|
2018-09-10 23:40:03 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync("/baddir");
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm({ write: true, read: true }, function removeAllSyncDirSuccess(): void {
|
2018-09-10 23:40:03 -04:00
|
|
|
// REMOVE EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
let path = Deno.makeTempDirSync() + "/dir/subdir";
|
2020-02-03 08:20:15 -05:00
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
let pathInfo = Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(pathInfo.isDirectory()); // check exist first
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync(path, { recursive: true }); // remove
|
2018-09-10 23:40:03 -04:00
|
|
|
// We then check again after remove
|
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// Directory is gone
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2020-02-21 10:36:13 -05:00
|
|
|
|
2018-09-10 23:40:03 -04:00
|
|
|
// REMOVE NON-EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
path = Deno.makeTempDirSync() + "/dir/subdir";
|
2018-09-10 23:40:03 -04:00
|
|
|
const subPath = path + "/subsubdir";
|
2020-02-03 08:20:15 -05:00
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(subPath);
|
|
|
|
pathInfo = Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(pathInfo.isDirectory()); // check exist first
|
2019-02-12 10:08:56 -05:00
|
|
|
const subPathInfo = Deno.statSync(subPath);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(subPathInfo.isDirectory()); // check exist first
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync(path, { recursive: true }); // remove
|
2018-09-10 23:40:03 -04:00
|
|
|
// We then check parent directory again after remove
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// Directory is gone
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
function removeAllSyncFileSuccess(): void {
|
|
|
|
// 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
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
}
|
2020-02-03 08:20:15 -05:00
|
|
|
);
|
2018-09-10 23:40:03 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: true }, function removeAllSyncFail(): void {
|
2018-09-10 23:40:03 -04:00
|
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
// Non-existent
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync("/baddir", { recursive: true });
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: false }, function removeAllSyncPerm(): void {
|
2018-09-10 23:40:03 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.removeSync("/baddir", { recursive: true });
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// ASYNC
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
async function removeDirSuccess(): Promise<void> {
|
|
|
|
// REMOVE EMPTY DIRECTORY
|
|
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
|
|
|
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
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
}
|
2020-02-03 08:20:15 -05:00
|
|
|
);
|
2018-09-10 23:40:03 -04:00
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
async function removeFileSuccess(): Promise<void> {
|
|
|
|
// 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
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
}
|
2020-02-03 08:20:15 -05:00
|
|
|
);
|
2018-09-10 23:40:03 -04:00
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm({ write: true, read: true }, async function removeFail(): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-09-10 23:40:03 -04:00
|
|
|
// NON-EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
2018-09-10 23:40:03 -04:00
|
|
|
const subPath = path + "/subsubdir";
|
2020-02-03 08:20:15 -05:00
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(subPath);
|
|
|
|
const pathInfo = Deno.statSync(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(pathInfo.isDirectory()); // check exist first
|
2019-02-12 10:08:56 -05:00
|
|
|
const subPathInfo = Deno.statSync(subPath);
|
2018-09-10 23:40:03 -04:00
|
|
|
assert(subPathInfo.isDirectory()); // check exist first
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
// Should not be able to recursively remove
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove(path);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-21 10:36:13 -05:00
|
|
|
assert(err instanceof Error);
|
2018-09-10 23:40:03 -04:00
|
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
|
|
try {
|
|
|
|
// Non-existent
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove("/baddir");
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
async function removeDanglingSymlinkSuccess(): Promise<void> {
|
|
|
|
const danglingSymlinkPath = Deno.makeTempDirSync() + "/dangling_symlink";
|
|
|
|
// TODO(#3832): Remove "Not Implemented" error checking when symlink creation is implemented for Windows
|
|
|
|
let errOnWindows;
|
|
|
|
try {
|
|
|
|
Deno.symlinkSync("unexistent_file", danglingSymlinkPath);
|
|
|
|
} catch (e) {
|
|
|
|
errOnWindows = e;
|
|
|
|
}
|
|
|
|
if (Deno.build.os === "win") {
|
|
|
|
assertEquals(errOnWindows.message, "Not implemented");
|
|
|
|
} else {
|
|
|
|
const pathInfo = Deno.lstatSync(danglingSymlinkPath);
|
|
|
|
assert(pathInfo.isSymlink());
|
|
|
|
await Deno.remove(danglingSymlinkPath);
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
Deno.lstatSync(danglingSymlinkPath);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2020-02-03 08:20:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
async function removeValidSymlinkSuccess(): Promise<void> {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const data = encoder.encode("Test");
|
|
|
|
const tempDir = Deno.makeTempDirSync();
|
|
|
|
const filePath = tempDir + "/test.txt";
|
|
|
|
const validSymlinkPath = tempDir + "/valid_symlink";
|
|
|
|
Deno.writeFileSync(filePath, data, { perm: 0o666 });
|
|
|
|
// TODO(#3832): Remove "Not Implemented" error checking when symlink creation is implemented for Windows
|
|
|
|
let errOnWindows;
|
|
|
|
try {
|
|
|
|
Deno.symlinkSync(filePath, validSymlinkPath);
|
|
|
|
} catch (e) {
|
|
|
|
errOnWindows = e;
|
|
|
|
}
|
|
|
|
if (Deno.build.os === "win") {
|
|
|
|
assertEquals(errOnWindows.message, "Not implemented");
|
|
|
|
} else {
|
|
|
|
const symlinkPathInfo = Deno.statSync(validSymlinkPath);
|
|
|
|
assert(symlinkPathInfo.isFile());
|
|
|
|
await Deno.remove(validSymlinkPath);
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
Deno.statSync(validSymlinkPath);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
Deno.removeSync(filePath);
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2020-02-03 08:20:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: false }, async function removePerm(): Promise<void> {
|
2018-09-10 23:40:03 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove("/baddir");
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
async function removeAllDirSuccess(): Promise<void> {
|
|
|
|
// REMOVE EMPTY DIRECTORY
|
|
|
|
let path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
|
|
|
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
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2020-02-21 10:36:13 -05:00
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
// REMOVE NON-EMPTY DIRECTORY
|
|
|
|
path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
|
|
const subPath = path + "/subsubdir";
|
|
|
|
Deno.mkdirSync(path, { recursive: true });
|
|
|
|
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
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
}
|
2020-02-03 08:20:15 -05:00
|
|
|
);
|
2018-09-10 23:40:03 -04:00
|
|
|
|
2020-02-03 08:20:15 -05:00
|
|
|
testPerm(
|
|
|
|
{ write: true, read: true },
|
|
|
|
async function removeAllFileSuccess(): Promise<void> {
|
|
|
|
// 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
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
}
|
2020-02-03 08:20:15 -05:00
|
|
|
);
|
2018-09-10 23:40:03 -04:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: true }, async function removeAllFail(): Promise<void> {
|
2018-09-10 23:40:03 -04:00
|
|
|
// NON-EXISTENT DIRECTORY/FILE
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
// Non-existent
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove("/baddir", { recursive: true });
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
testPerm({ write: false }, async function removeAllPerm(): Promise<void> {
|
2018-09-10 23:40:03 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2019-02-12 10:08:56 -05:00
|
|
|
await Deno.remove("/baddir", { recursive: true });
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(err instanceof Deno.errors.PermissionDenied);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(err.name, "PermissionDenied");
|
2018-09-10 23:40:03 -04:00
|
|
|
});
|