deno.land / std@0.177.1 / fs / empty_dir_test.ts

empty_dir_test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertRejects, assertStringIncludes, assertThrows,} from "../testing/asserts.ts";import * as path from "../path/mod.ts";import { emptyDir, emptyDirSync } from "./empty_dir.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));const testdataDir = path.resolve(moduleDir, "testdata");
Deno.test("emptyDirIfItNotExist", async function () { 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); assertEquals(stat.isDirectory, true); } finally { // remove the test dir await Deno.remove(testDir, { recursive: true }); }});
Deno.test("emptyDirSyncIfItNotExist", function () { const testDir = path.join(testdataDir, "empty_dir_test_2"); const testNestDir = path.join(testDir, "nest"); // empty a dir which does not exist, then it will a create new one. emptyDirSync(testNestDir);
try { // check the dir const stat = Deno.statSync(testNestDir); assertEquals(stat.isDirectory, true); } finally { // remove the test dir Deno.removeSync(testDir, { recursive: true }); }});
Deno.test("emptyDirIfItExist", async function () { 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); assertEquals(beforeFileStat.isFile, true);
const beforeDirStat = await Deno.stat(testNestDir); assertEquals(beforeDirStat.isDirectory, true);
await emptyDir(testDir);
// after empty: file/directory have already been removed try { // test dir still there const stat = await Deno.stat(testDir); assertEquals(stat.isDirectory, true);
// nest directory have been removed await assertRejects( async () => { await Deno.stat(testNestDir); }, );
// test file have been removed await assertRejects( async () => { await Deno.stat(testDirFile); }, ); } finally { // remote test dir await Deno.remove(testDir, { recursive: true }); }});
Deno.test("emptyDirSyncIfItExist", function () { 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); assertEquals(beforeFileStat.isFile, true);
const beforeDirStat = Deno.statSync(testNestDir); assertEquals(beforeDirStat.isDirectory, true);
emptyDirSync(testDir);
// after empty: file/directory have already remove try { // test dir still present const stat = Deno.statSync(testDir); assertEquals(stat.isDirectory, true);
// nest directory have been removed assertThrows(() => { Deno.statSync(testNestDir); });
// test file have been removed assertThrows(() => { Deno.statSync(testDirFile); }); } finally { // remote test dir Deno.removeSync(testDir, { recursive: true }); }});
interface Scenes { read: boolean; // --allow-read write: boolean; // --allow-write async: boolean; output: string;}const scenes: Scenes[] = [ // 1 { read: false, write: false, async: true, output: "run again with the --allow-read flag", }, { read: false, write: false, async: false, output: "run again with the --allow-read flag", }, // 2 { read: true, write: false, async: true, output: "run again with the --allow-write flag", }, { read: true, write: false, async: false, output: "run again with the --allow-write flag", }, // 3 { read: false, write: true, async: true, output: "run again with the --allow-read flag", }, { read: false, write: true, async: false, output: "run again with the --allow-read flag", }, // 4 { read: true, write: true, async: true, output: "success", }, { read: true, write: true, async: false, output: "success", },];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`; Deno.test(`[fs] emptyDirPermission ${title}`, async function (): Promise< 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"), );
try { const args = ["run", "--quiet", "--no-prompt"];
if (s.read) { args.push("--allow-read"); }
if (s.write) { args.push("--allow-write"); }
args.push( path.join( testdataDir, s.async ? "empty_dir.ts" : "empty_dir_sync.ts", ), ); args.push("testfolder");
const command = new Deno.Command(Deno.execPath(), { cwd: testdataDir, args, }); const { stdout } = await command.output(); assertStringIncludes(new TextDecoder().decode(stdout), s.output); } catch (err) { await Deno.remove(testfolder, { recursive: true }); throw err; } } finally { // Make the test rerunnable // Otherwise it would throw an error due to mkdir fail. await Deno.remove(testfolder, { recursive: true }); // done } });}
std

Version Info

Tagged at
11 months ago