deno.land / std@0.166.0 / fs / move_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertRejects, assertThrows,} from "../testing/asserts.ts";import * as path from "../path/mod.ts";import { move, moveSync } from "./move.ts";import { ensureFile, ensureFileSync } from "./ensure_file.ts";import { ensureDir, ensureDirSync } from "./ensure_dir.ts";import { existsSync } from "./exists.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));const testdataDir = path.resolve(moduleDir, "testdata");
Deno.test("moveDirectoryIfSrcNotExists", async function () { const srcDir = path.join(testdataDir, "move_test_src_1"); const destDir = path.join(testdataDir, "move_test_dest_1"); // if src directory not exist await assertRejects( async () => { await move(srcDir, destDir); }, );});
Deno.test("moveDirectoryIfDestNotExists", async function () { const srcDir = path.join(testdataDir, "move_test_src_2"); const destDir = path.join(testdataDir, "move_test_dest_2");
await Deno.mkdir(srcDir, { recursive: true });
// if dest directory not exist await assertRejects( async () => { await move(srcDir, destDir); throw new Error("should not throw error"); }, Error, "should not throw error", );
await Deno.remove(destDir);});
Deno.test( "moveDirectoryIfDestNotExistsAndOverwrite", async function () { const srcDir = path.join(testdataDir, "move_test_src_2"); const destDir = path.join(testdataDir, "move_test_dest_2");
await Deno.mkdir(srcDir, { recursive: true });
// if dest directory not exist await assertRejects( async () => { await move(srcDir, destDir, { overwrite: true }); throw new Error("should not throw error"); }, Error, "should not throw error", );
await Deno.remove(destDir); },);
Deno.test("moveFileIfSrcNotExists", async function () { const srcFile = path.join(testdataDir, "move_test_src_3", "test.txt"); const destFile = path.join(testdataDir, "move_test_dest_3", "test.txt");
// if src directory not exist await assertRejects( async () => { await move(srcFile, destFile); }, );});
Deno.test("moveFileIfDestExists", async function () { const srcDir = path.join(testdataDir, "move_test_src_4"); const destDir = path.join(testdataDir, "move_test_dest_4"); const srcFile = path.join(srcDir, "test.txt"); const destFile = path.join(destDir, "test.txt"); const srcContent = new TextEncoder().encode("src"); const destContent = new TextEncoder().encode("dest");
// make sure files exists await Promise.all([ensureFile(srcFile), ensureFile(destFile)]);
// write file content await Promise.all([ Deno.writeFile(srcFile, srcContent), Deno.writeFile(destFile, destContent), ]);
// make sure the test file have been created assertEquals(new TextDecoder().decode(await Deno.readFile(srcFile)), "src"); assertEquals(new TextDecoder().decode(await Deno.readFile(destFile)), "dest");
// move it without override await assertRejects( async () => { await move(srcFile, destFile); }, Error, "dest already exists", );
// move again with overwrite await assertRejects( async () => { await move(srcFile, destFile, { overwrite: true }); throw new Error("should not throw error"); }, Error, "should not throw error", );
await assertRejects(async () => await Deno.lstat(srcFile)); assertEquals(new TextDecoder().decode(await Deno.readFile(destFile)), "src");
// clean up await Promise.all([ Deno.remove(srcDir, { recursive: true }), Deno.remove(destDir, { recursive: true }), ]);});
Deno.test("moveDirectory", async function () { const srcDir = path.join(testdataDir, "move_test_src_5"); const destDir = path.join(testdataDir, "move_test_dest_5"); const srcFile = path.join(srcDir, "test.txt"); const destFile = path.join(destDir, "test.txt"); const srcContent = new TextEncoder().encode("src");
await Deno.mkdir(srcDir, { recursive: true }); assert(await Deno.lstat(srcDir)); await Deno.writeFile(srcFile, srcContent);
await move(srcDir, destDir);
await assertRejects(async () => await Deno.lstat(srcDir)); assert(await Deno.lstat(destDir)); assert(await Deno.lstat(destFile));
const destFileContent = new TextDecoder().decode( await Deno.readFile(destFile), ); assertEquals(destFileContent, "src");
await Deno.remove(destDir, { recursive: true });});
Deno.test( "moveIfSrcAndDestDirectoryExistsAndOverwrite", async function () { const srcDir = path.join(testdataDir, "move_test_src_6"); const destDir = path.join(testdataDir, "move_test_dest_6"); const srcFile = path.join(srcDir, "test.txt"); const destFile = path.join(destDir, "test.txt"); const srcContent = new TextEncoder().encode("src"); const destContent = new TextEncoder().encode("dest");
await Promise.all([ Deno.mkdir(srcDir, { recursive: true }), Deno.mkdir(destDir, { recursive: true }), ]); assert(await Deno.lstat(srcDir)); assert(await Deno.lstat(destDir)); await Promise.all([ Deno.writeFile(srcFile, srcContent), Deno.writeFile(destFile, destContent), ]);
await move(srcDir, destDir, { overwrite: true });
await assertRejects(async () => await Deno.lstat(srcDir)); assert(await Deno.lstat(destDir)); assert(await Deno.lstat(destFile));
const destFileContent = new TextDecoder().decode( await Deno.readFile(destFile), ); assertEquals(destFileContent, "src");
await Deno.remove(destDir, { recursive: true }); },);
Deno.test("moveIntoSubDir", async function () { const srcDir = path.join(testdataDir, "move_test_src_7"); const destDir = path.join(srcDir, "nest");
await ensureDir(destDir);
await assertRejects( async () => { await move(srcDir, destDir); }, Error, `Cannot move '${srcDir}' to a subdirectory of itself, '${destDir}'.`, ); await Deno.remove(srcDir, { recursive: true });});
Deno.test("moveSyncDirectoryIfSrcNotExists", function () { const srcDir = path.join(testdataDir, "move_sync_test_src_1"); const destDir = path.join(testdataDir, "move_sync_test_dest_1"); // if src directory not exist assertThrows(() => { moveSync(srcDir, destDir); });});
Deno.test("moveSyncDirectoryIfDestNotExists", function () { const srcDir = path.join(testdataDir, "move_sync_test_src_2"); const destDir = path.join(testdataDir, "move_sync_test_dest_2");
Deno.mkdirSync(srcDir, { recursive: true });
// if dest directory not exist assertThrows( () => { moveSync(srcDir, destDir); throw new Error("should not throw error"); }, Error, "should not throw error", );
Deno.removeSync(destDir);});
Deno.test("moveSyncDirectoryIfDestNotExistsAndOverwrite", function () { const srcDir = path.join(testdataDir, "move_sync_test_src_2"); const destDir = path.join(testdataDir, "move_sync_test_dest_2");
Deno.mkdirSync(srcDir, { recursive: true });
// if dest directory not exist width overwrite assertThrows( () => { moveSync(srcDir, destDir, { overwrite: true }); throw new Error("should not throw error"); }, Error, "should not throw error", );
Deno.removeSync(destDir);});
Deno.test("moveSyncFileIfSrcNotExists", function () { const srcFile = path.join(testdataDir, "move_sync_test_src_3", "test.txt"); const destFile = path.join(testdataDir, "move_sync_test_dest_3", "test.txt");
// if src directory not exist assertThrows(() => { moveSync(srcFile, destFile); });});
Deno.test("moveSyncFileIfDestExists", function () { const srcDir = path.join(testdataDir, "move_sync_test_src_4"); const destDir = path.join(testdataDir, "move_sync_test_dest_4"); const srcFile = path.join(srcDir, "test.txt"); const destFile = path.join(destDir, "test.txt"); const srcContent = new TextEncoder().encode("src"); const destContent = new TextEncoder().encode("dest");
// make sure files exists ensureFileSync(srcFile); ensureFileSync(destFile);
// write file content Deno.writeFileSync(srcFile, srcContent); Deno.writeFileSync(destFile, destContent);
// make sure the test file have been created assertEquals(new TextDecoder().decode(Deno.readFileSync(srcFile)), "src"); assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "dest");
// move it without override assertThrows( () => { moveSync(srcFile, destFile); }, Error, "dest already exists", );
// move again with overwrite assertThrows( () => { moveSync(srcFile, destFile, { overwrite: true }); throw new Error("should not throw error"); }, Error, "should not throw error", );
assertEquals(existsSync(srcFile), false); assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "src");
// clean up Deno.removeSync(srcDir, { recursive: true }); Deno.removeSync(destDir, { recursive: true });});
Deno.test("moveSyncDirectory", function () { const srcDir = path.join(testdataDir, "move_sync_test_src_5"); const destDir = path.join(testdataDir, "move_sync_test_dest_5"); const srcFile = path.join(srcDir, "test.txt"); const destFile = path.join(destDir, "test.txt"); const srcContent = new TextEncoder().encode("src");
Deno.mkdirSync(srcDir, { recursive: true }); assertEquals(existsSync(srcDir), true); Deno.writeFileSync(srcFile, srcContent);
moveSync(srcDir, destDir);
assertEquals(existsSync(srcDir), false); assertEquals(existsSync(destDir), true); assertEquals(existsSync(destFile), true);
const destFileContent = new TextDecoder().decode(Deno.readFileSync(destFile)); assertEquals(destFileContent, "src");
Deno.removeSync(destDir, { recursive: true });});
Deno.test("moveSyncIfSrcAndDestDirectoryExistsAndOverwrite", function () { const srcDir = path.join(testdataDir, "move_sync_test_src_6"); const destDir = path.join(testdataDir, "move_sync_test_dest_6"); const srcFile = path.join(srcDir, "test.txt"); const destFile = path.join(destDir, "test.txt"); const srcContent = new TextEncoder().encode("src"); const destContent = new TextEncoder().encode("dest");
Deno.mkdirSync(srcDir, { recursive: true }); Deno.mkdirSync(destDir, { recursive: true }); assertEquals(existsSync(srcDir), true); assertEquals(existsSync(destDir), true); Deno.writeFileSync(srcFile, srcContent); Deno.writeFileSync(destFile, destContent);
moveSync(srcDir, destDir, { overwrite: true });
assertEquals(existsSync(srcDir), false); assertEquals(existsSync(destDir), true); assertEquals(existsSync(destFile), true);
const destFileContent = new TextDecoder().decode(Deno.readFileSync(destFile)); assertEquals(destFileContent, "src");
Deno.removeSync(destDir, { recursive: true });});
Deno.test("moveSyncIntoSubDir", function () { const srcDir = path.join(testdataDir, "move_sync_test_src_7"); const destDir = path.join(srcDir, "nest");
ensureDirSync(destDir);
assertThrows( () => { moveSync(srcDir, destDir); }, Error, `Cannot move '${srcDir}' to a subdirectory of itself, '${destDir}'.`, ); Deno.removeSync(srcDir, { recursive: true });});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago