2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-07-08 11:09:23 -04:00
|
|
|
import {
|
2020-02-19 15:36:18 -05:00
|
|
|
assert,
|
2019-07-08 11:09:23 -04:00
|
|
|
assertEquals,
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStringContains,
|
2019-07-08 11:09:23 -04:00
|
|
|
assertThrows,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertThrowsAsync,
|
2019-07-08 11:09:23 -04:00
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-11 14:19:52 -04:00
|
|
|
import { emptyDir, emptyDirSync } from "./empty_dir.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("emptyDirIfItNotExist", async function (): Promise<void> {
|
2019-03-11 14:19:52 -04:00
|
|
|
const testDir = path.join(testdataDir, "empty_dir_test_1");
|
|
|
|
const testNestDir = path.join(testDir, "nest");
|
|
|
|
// empty a dir which not exist. then it will create new one
|
|
|
|
await emptyDir(testNestDir);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// check the dir
|
|
|
|
const stat = await Deno.stat(testNestDir);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
} finally {
|
|
|
|
// remove the test dir
|
2020-03-18 19:25:55 -04:00
|
|
|
await Deno.remove(testDir, { recursive: true });
|
2019-03-11 14:19:52 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("emptyDirSyncIfItNotExist", function (): void {
|
2019-03-11 14:19:52 -04:00
|
|
|
const testDir = path.join(testdataDir, "empty_dir_test_2");
|
|
|
|
const testNestDir = path.join(testDir, "nest");
|
2020-05-16 09:31:21 -04:00
|
|
|
// empty a dir which does not exist, then it will a create new one.
|
2019-03-11 14:19:52 -04:00
|
|
|
emptyDirSync(testNestDir);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// check the dir
|
|
|
|
const stat = Deno.statSync(testNestDir);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
} finally {
|
|
|
|
// remove the test dir
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
2019-03-11 14:19:52 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("emptyDirIfItExist", async function (): Promise<void> {
|
2019-03-11 14:19:52 -04:00
|
|
|
const testDir = path.join(testdataDir, "empty_dir_test_3");
|
|
|
|
const testNestDir = path.join(testDir, "nest");
|
|
|
|
// create test dir
|
|
|
|
await emptyDir(testNestDir);
|
|
|
|
const testDirFile = path.join(testNestDir, "test.ts");
|
|
|
|
// create test file in test dir
|
|
|
|
await Deno.writeFile(testDirFile, new Uint8Array());
|
|
|
|
|
|
|
|
// before empty: make sure file/directory exist
|
|
|
|
const beforeFileStat = await Deno.stat(testDirFile);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(beforeFileStat.isFile, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
|
|
|
|
const beforeDirStat = await Deno.stat(testNestDir);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(beforeDirStat.isDirectory, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
|
|
|
|
await emptyDir(testDir);
|
|
|
|
|
2020-05-16 09:31:21 -04:00
|
|
|
// after empty: file/directory have already been removed
|
2019-03-11 14:19:52 -04:00
|
|
|
try {
|
|
|
|
// test dir still there
|
|
|
|
const stat = await Deno.stat(testDir);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
|
2020-05-16 09:31:21 -04:00
|
|
|
// nest directory have been removed
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await Deno.stat(testNestDir);
|
|
|
|
}
|
|
|
|
);
|
2019-03-11 14:19:52 -04:00
|
|
|
|
2020-05-16 09:31:21 -04:00
|
|
|
// test file have been removed
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await Deno.stat(testDirFile);
|
|
|
|
}
|
|
|
|
);
|
2019-03-11 14:19:52 -04:00
|
|
|
} finally {
|
|
|
|
// remote test dir
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("emptyDirSyncIfItExist", function (): void {
|
2019-03-11 14:19:52 -04:00
|
|
|
const testDir = path.join(testdataDir, "empty_dir_test_4");
|
|
|
|
const testNestDir = path.join(testDir, "nest");
|
|
|
|
// create test dir
|
|
|
|
emptyDirSync(testNestDir);
|
|
|
|
const testDirFile = path.join(testNestDir, "test.ts");
|
|
|
|
// create test file in test dir
|
|
|
|
Deno.writeFileSync(testDirFile, new Uint8Array());
|
|
|
|
|
|
|
|
// before empty: make sure file/directory exist
|
|
|
|
const beforeFileStat = Deno.statSync(testDirFile);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(beforeFileStat.isFile, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
|
|
|
|
const beforeDirStat = Deno.statSync(testNestDir);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(beforeDirStat.isDirectory, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
|
|
|
|
emptyDirSync(testDir);
|
|
|
|
|
|
|
|
// after empty: file/directory have already remove
|
|
|
|
try {
|
2020-05-16 09:31:21 -04:00
|
|
|
// test dir still present
|
2019-03-11 14:19:52 -04:00
|
|
|
const stat = Deno.statSync(testDir);
|
2020-04-16 01:40:30 -04:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 14:19:52 -04:00
|
|
|
|
2020-05-16 09:31:21 -04:00
|
|
|
// nest directory have been removed
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testNestDir);
|
|
|
|
});
|
2019-03-11 14:19:52 -04:00
|
|
|
|
2020-05-16 09:31:21 -04:00
|
|
|
// test file have been removed
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
Deno.statSync(testDirFile);
|
|
|
|
});
|
2019-03-11 14:19:52 -04:00
|
|
|
} finally {
|
|
|
|
// remote test dir
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
|
|
|
});
|
2019-12-18 05:12:36 -05:00
|
|
|
|
2020-02-27 15:12:04 -05:00
|
|
|
interface Scenes {
|
|
|
|
read: boolean; // --allow-read
|
|
|
|
write: boolean; // --allow-write
|
|
|
|
async: boolean;
|
|
|
|
output: string;
|
|
|
|
}
|
|
|
|
const scenes: Scenes[] = [
|
|
|
|
// 1
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
async: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
async: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
// 2
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
async: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "run again with the --allow-write flag",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
async: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "run again with the --allow-write flag",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
// 3
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
async: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
async: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
// 4
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
async: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "success",
|
2020-02-27 15:12:04 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
async: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
output: "success",
|
|
|
|
},
|
2020-02-27 15:12:04 -05:00
|
|
|
];
|
|
|
|
for (const s of scenes) {
|
|
|
|
let title = `test ${s.async ? "emptyDir" : "emptyDirSync"}`;
|
|
|
|
title += `("testdata/testfolder") ${s.read ? "with" : "without"}`;
|
|
|
|
title += ` --allow-read & ${s.write ? "with" : "without"} --allow-write`;
|
2020-03-28 13:03:49 -04:00
|
|
|
Deno.test(`[fs] emptyDirPermission ${title}`, async function (): Promise<
|
2020-02-27 15:12:04 -05:00
|
|
|
void
|
|
|
|
> {
|
|
|
|
const testfolder = path.join(testdataDir, "testfolder");
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Deno.mkdir(testfolder);
|
|
|
|
|
|
|
|
await Deno.writeFile(
|
|
|
|
path.join(testfolder, "child.txt"),
|
|
|
|
new TextEncoder().encode("hello world")
|
2019-12-18 05:12:36 -05:00
|
|
|
);
|
|
|
|
|
2020-02-27 15:12:04 -05:00
|
|
|
try {
|
2020-04-30 11:23:40 -04:00
|
|
|
// TODO(lucacasonato): remove unstable when stabilized
|
|
|
|
const args = [Deno.execPath(), "run", "--unstable"];
|
2019-12-18 05:12:36 -05:00
|
|
|
|
2020-02-27 15:12:04 -05:00
|
|
|
if (s.read) {
|
|
|
|
args.push("--allow-read");
|
|
|
|
}
|
2019-12-18 05:12:36 -05:00
|
|
|
|
2020-02-27 15:12:04 -05:00
|
|
|
if (s.write) {
|
|
|
|
args.push("--allow-write");
|
|
|
|
}
|
2019-12-18 05:12:36 -05:00
|
|
|
|
2020-02-27 15:12:04 -05:00
|
|
|
args.push(
|
|
|
|
path.join(testdataDir, s.async ? "empty_dir.ts" : "empty_dir_sync.ts")
|
|
|
|
);
|
|
|
|
args.push("testfolder");
|
2019-12-18 05:12:36 -05:00
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
const p = Deno.run({
|
2020-02-27 15:12:04 -05:00
|
|
|
stdout: "piped",
|
|
|
|
cwd: testdataDir,
|
2020-03-28 13:03:49 -04:00
|
|
|
cmd: args,
|
2020-02-27 15:12:04 -05:00
|
|
|
});
|
2019-12-18 05:12:36 -05:00
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
assert(p.stdout);
|
|
|
|
const output = await p.output();
|
|
|
|
p.close();
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStringContains(new TextDecoder().decode(output), s.output);
|
2020-02-27 15:12:04 -05:00
|
|
|
} catch (err) {
|
|
|
|
await Deno.remove(testfolder, { recursive: true });
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
// Make the test rerunnable
|
2020-05-16 09:31:21 -04:00
|
|
|
// Otherwise it would throw an error due to mkdir fail.
|
2020-02-27 15:12:04 -05:00
|
|
|
await Deno.remove(testfolder, { recursive: true });
|
|
|
|
// done
|
2019-12-18 05:12:36 -05:00
|
|
|
}
|
2020-02-27 15:12:04 -05:00
|
|
|
});
|
|
|
|
}
|