2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-09-10 23:40:03 -04:00
|
|
|
import { testPerm, assert, assertEqual } from "./test_util.ts";
|
|
|
|
|
|
|
|
// SYNC
|
|
|
|
|
|
|
|
testPerm({ write: true }, function removeSyncDirSuccess() {
|
|
|
|
// REMOVE EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
|
|
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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, function removeSyncFileSuccess() {
|
|
|
|
// 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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, function removeSyncFail() {
|
|
|
|
// 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";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(path);
|
|
|
|
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?
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.Other);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "Other");
|
|
|
|
// 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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false }, function removeSyncPerm() {
|
|
|
|
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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "PermissionDenied");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, function removeAllSyncDirSuccess() {
|
|
|
|
// REMOVE EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
let path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
|
|
Deno.mkdirSync(path);
|
|
|
|
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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
// 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";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(path);
|
|
|
|
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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, function removeAllSyncFileSuccess() {
|
|
|
|
// 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, { 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(filename);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// File is gone
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, function removeAllSyncFail() {
|
|
|
|
// 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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false }, function removeAllSyncPerm() {
|
|
|
|
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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "PermissionDenied");
|
|
|
|
});
|
|
|
|
|
|
|
|
// ASYNC
|
|
|
|
|
|
|
|
testPerm({ write: true }, async function removeDirSuccess() {
|
|
|
|
// REMOVE EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
const path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
|
|
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
|
|
|
await Deno.remove(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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, async function removeFileSuccess() {
|
|
|
|
// 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
|
|
|
await Deno.remove(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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, async function removeFail() {
|
|
|
|
// 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";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(path);
|
|
|
|
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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.Other);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "Other");
|
|
|
|
// 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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false }, async function removePerm() {
|
|
|
|
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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "PermissionDenied");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, async function removeAllDirSuccess() {
|
|
|
|
// REMOVE EMPTY DIRECTORY
|
2019-02-12 10:08:56 -05:00
|
|
|
let path = Deno.makeTempDirSync() + "/dir/subdir";
|
|
|
|
Deno.mkdirSync(path);
|
|
|
|
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
|
|
|
await Deno.remove(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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
// 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";
|
2019-02-12 10:08:56 -05:00
|
|
|
Deno.mkdirSync(path);
|
|
|
|
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
|
|
|
await Deno.remove(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
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, async function removeAllFileSuccess() {
|
|
|
|
// 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
|
|
|
await Deno.remove(filename, { 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(filename);
|
2018-09-10 23:40:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
// File is gone
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: true }, async function removeAllFail() {
|
|
|
|
// 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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.NotFound);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "NotFound");
|
|
|
|
});
|
|
|
|
|
|
|
|
testPerm({ write: false }, async function removeAllPerm() {
|
|
|
|
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;
|
|
|
|
}
|
2019-02-12 10:08:56 -05:00
|
|
|
assertEqual(err.kind, Deno.ErrorKind.PermissionDenied);
|
2018-09-10 23:40:03 -04:00
|
|
|
assertEqual(err.name, "PermissionDenied");
|
|
|
|
});
|