2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2019-07-08 15:09:23 +00:00
|
|
|
import {
|
|
|
|
assertEquals,
|
2021-08-09 05:59:52 +00:00
|
|
|
assertRejects,
|
2020-10-26 15:03:30 +00:00
|
|
|
assertStringIncludes,
|
2019-07-08 15:09:23 +00:00
|
|
|
assertThrows,
|
2024-04-29 02:57:30 +00:00
|
|
|
} from "@std/assert";
|
|
|
|
import * as path from "@std/path";
|
2019-03-11 18:19:52 +00:00
|
|
|
import { emptyDir, emptyDirSync } from "./empty_dir.ts";
|
|
|
|
|
2024-03-04 07:19:00 +00:00
|
|
|
const testdataDir = path.join(import.meta.dirname!, "testdata");
|
2019-03-11 18:19:52 +00:00
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("emptyDir() creates a new dir if it does not exist", async function () {
|
2019-03-11 18:19:52 +00: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 05:40:30 +00:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
} finally {
|
|
|
|
// remove the test dir
|
2020-03-18 23:25:55 +00:00
|
|
|
await Deno.remove(testDir, { recursive: true });
|
2019-03-11 18:19:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("emptyDirSync() creates a new dir if it does not exist", function () {
|
2019-03-11 18:19:52 +00:00
|
|
|
const testDir = path.join(testdataDir, "empty_dir_test_2");
|
|
|
|
const testNestDir = path.join(testDir, "nest");
|
2020-05-16 13:31:21 +00:00
|
|
|
// empty a dir which does not exist, then it will a create new one.
|
2019-03-11 18:19:52 +00:00
|
|
|
emptyDirSync(testNestDir);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// check the dir
|
|
|
|
const stat = Deno.statSync(testNestDir);
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
} finally {
|
|
|
|
// remove the test dir
|
2020-03-18 23:25:55 +00:00
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
2019-03-11 18:19:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("emptyDir() empties nested dirs and files", async function () {
|
2019-03-11 18:19:52 +00: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 05:40:30 +00:00
|
|
|
assertEquals(beforeFileStat.isFile, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
|
|
|
|
const beforeDirStat = await Deno.stat(testNestDir);
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(beforeDirStat.isDirectory, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
|
|
|
|
await emptyDir(testDir);
|
|
|
|
|
2020-05-16 13:31:21 +00:00
|
|
|
// after empty: file/directory have already been removed
|
2019-03-11 18:19:52 +00:00
|
|
|
try {
|
|
|
|
// test dir still there
|
|
|
|
const stat = await Deno.stat(testDir);
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
|
2020-05-16 13:31:21 +00:00
|
|
|
// nest directory have been removed
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-04-24 11:41:23 +00:00
|
|
|
await Deno.stat(testNestDir);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-03-11 18:19:52 +00:00
|
|
|
|
2020-05-16 13:31:21 +00:00
|
|
|
// test file have been removed
|
2021-08-09 05:59:52 +00:00
|
|
|
await assertRejects(
|
2021-04-05 11:49:05 +00:00
|
|
|
async () => {
|
2019-04-24 11:41:23 +00:00
|
|
|
await Deno.stat(testDirFile);
|
2020-07-14 19:24:17 +00:00
|
|
|
},
|
2019-04-24 11:41:23 +00:00
|
|
|
);
|
2019-03-11 18:19:52 +00:00
|
|
|
} finally {
|
|
|
|
// remote test dir
|
|
|
|
await Deno.remove(testDir, { recursive: true });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test("emptyDirSync() empties nested dirs and files", function () {
|
2019-03-11 18:19:52 +00: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 05:40:30 +00:00
|
|
|
assertEquals(beforeFileStat.isFile, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
|
|
|
|
const beforeDirStat = Deno.statSync(testNestDir);
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(beforeDirStat.isDirectory, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
|
|
|
|
emptyDirSync(testDir);
|
|
|
|
|
|
|
|
// after empty: file/directory have already remove
|
|
|
|
try {
|
2020-05-16 13:31:21 +00:00
|
|
|
// test dir still present
|
2019-03-11 18:19:52 +00:00
|
|
|
const stat = Deno.statSync(testDir);
|
2020-04-16 05:40:30 +00:00
|
|
|
assertEquals(stat.isDirectory, true);
|
2019-03-11 18:19:52 +00:00
|
|
|
|
2020-05-16 13:31:21 +00:00
|
|
|
// nest directory have been removed
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
Deno.statSync(testNestDir);
|
|
|
|
});
|
2019-03-11 18:19:52 +00:00
|
|
|
|
2020-05-16 13:31:21 +00:00
|
|
|
// test file have been removed
|
2022-08-24 01:21:57 +00:00
|
|
|
assertThrows(() => {
|
2019-11-13 18:42:34 +00:00
|
|
|
Deno.statSync(testDirFile);
|
|
|
|
});
|
2019-03-11 18:19:52 +00:00
|
|
|
} finally {
|
|
|
|
// remote test dir
|
|
|
|
Deno.removeSync(testDir, { recursive: true });
|
|
|
|
}
|
|
|
|
});
|
2019-12-18 10:12:36 +00:00
|
|
|
|
2020-02-27 20:12:04 +00: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 17:03:49 +00:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 20:12:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
async: false,
|
2020-03-28 17:03:49 +00:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 20:12:04 +00:00
|
|
|
},
|
|
|
|
// 2
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
async: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
output: "run again with the --allow-write flag",
|
2020-02-27 20:12:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
async: false,
|
2020-03-28 17:03:49 +00:00
|
|
|
output: "run again with the --allow-write flag",
|
2020-02-27 20:12:04 +00:00
|
|
|
},
|
|
|
|
// 3
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
async: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 20:12:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
async: false,
|
2020-03-28 17:03:49 +00:00
|
|
|
output: "run again with the --allow-read flag",
|
2020-02-27 20:12:04 +00:00
|
|
|
},
|
|
|
|
// 4
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
async: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
output: "success",
|
2020-02-27 20:12:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
async: false,
|
2020-03-28 17:03:49 +00:00
|
|
|
output: "success",
|
|
|
|
},
|
2020-02-27 20:12:04 +00:00
|
|
|
];
|
|
|
|
for (const s of scenes) {
|
2023-11-23 06:59:59 +00:00
|
|
|
let title = `${s.async ? "emptyDir()" : "emptyDirSync()"}`;
|
|
|
|
title += ` test ("testdata/testfolder") ${s.read ? "with" : "without"}`;
|
2020-02-27 20:12:04 +00:00
|
|
|
title += ` --allow-read & ${s.write ? "with" : "without"} --allow-write`;
|
2023-11-23 06:59:59 +00:00
|
|
|
Deno.test(`${title} permission`, async function (): Promise<
|
2020-02-27 20:12:04 +00:00
|
|
|
void
|
|
|
|
> {
|
|
|
|
const testfolder = path.join(testdataDir, "testfolder");
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Deno.mkdir(testfolder);
|
|
|
|
|
2023-11-03 04:41:18 +00:00
|
|
|
await Deno.writeTextFile(
|
2020-02-27 20:12:04 +00:00
|
|
|
path.join(testfolder, "child.txt"),
|
2023-11-03 04:41:18 +00:00
|
|
|
"hello world",
|
2019-12-18 10:12:36 +00:00
|
|
|
);
|
|
|
|
|
2020-02-27 20:12:04 +00:00
|
|
|
try {
|
2024-03-04 07:19:00 +00:00
|
|
|
const args = [
|
|
|
|
"run",
|
|
|
|
"--no-lock",
|
|
|
|
"--quiet",
|
|
|
|
"--no-prompt",
|
|
|
|
];
|
2019-12-18 10:12:36 +00:00
|
|
|
|
2020-02-27 20:12:04 +00:00
|
|
|
if (s.read) {
|
|
|
|
args.push("--allow-read");
|
|
|
|
}
|
2019-12-18 10:12:36 +00:00
|
|
|
|
2020-02-27 20:12:04 +00:00
|
|
|
if (s.write) {
|
|
|
|
args.push("--allow-write");
|
|
|
|
}
|
2019-12-18 10:12:36 +00:00
|
|
|
|
2020-02-27 20:12:04 +00:00
|
|
|
args.push(
|
2020-07-14 19:24:17 +00:00
|
|
|
path.join(
|
|
|
|
testdataDir,
|
|
|
|
s.async ? "empty_dir.ts" : "empty_dir_sync.ts",
|
|
|
|
),
|
2020-02-27 20:12:04 +00:00
|
|
|
);
|
2019-12-18 10:12:36 +00:00
|
|
|
|
2022-11-15 06:00:59 +00:00
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
2022-05-25 09:08:27 +00:00
|
|
|
args,
|
2024-09-13 05:43:13 +00:00
|
|
|
stderr: "inherit",
|
2020-02-27 20:12:04 +00:00
|
|
|
});
|
2024-09-13 05:43:13 +00:00
|
|
|
const { stdout } = await command.output();
|
2022-05-25 09:08:27 +00:00
|
|
|
assertStringIncludes(new TextDecoder().decode(stdout), s.output);
|
2020-02-27 20:12:04 +00:00
|
|
|
} catch (err) {
|
2024-09-13 05:43:13 +00:00
|
|
|
// deno-lint-ignore no-console
|
2023-12-28 02:45:18 +00:00
|
|
|
console.log(err);
|
2020-02-27 20:12:04 +00:00
|
|
|
await Deno.remove(testfolder, { recursive: true });
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
// Make the test rerunnable
|
2020-05-16 13:31:21 +00:00
|
|
|
// Otherwise it would throw an error due to mkdir fail.
|
2020-02-27 20:12:04 +00:00
|
|
|
await Deno.remove(testfolder, { recursive: true });
|
|
|
|
// done
|
2019-12-18 10:12:36 +00:00
|
|
|
}
|
2020-02-27 20:12:04 +00:00
|
|
|
});
|
|
|
|
}
|