deno.land / std@0.166.0 / fs / copy_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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// 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 { copy, copySync } from "./copy.ts";import { existsSync } from "./exists.ts";import { ensureDir, ensureDirSync } from "./ensure_dir.ts";import { ensureFile, ensureFileSync } from "./ensure_file.ts";import { ensureSymlink, ensureSymlinkSync } from "./ensure_symlink.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));const testdataDir = path.resolve(moduleDir, "testdata");
function testCopy( name: string, cb: (tempDir: string) => Promise<void>, ignore = false,) { Deno.test({ name, async fn() { const tempDir = await Deno.makeTempDir({ prefix: "deno_std_copy_async_test_", }); await cb(tempDir); await Deno.remove(tempDir, { recursive: true }); }, ignore, });}
function testCopySync(name: string, cb: (tempDir: string) => void) { Deno.test({ name, fn: () => { const tempDir = Deno.makeTempDirSync({ prefix: "deno_std_copy_sync_test_", }); cb(tempDir); Deno.removeSync(tempDir, { recursive: true }); }, });}
testCopy( "[fs] copy file if it does no exist", async (tempDir: string) => { const srcFile = path.join(testdataDir, "copy_file_not_exists.txt"); const destFile = path.join(tempDir, "copy_file_not_exists_1.txt"); await assertRejects( async () => { await copy(srcFile, destFile); }, ); },);
testCopy( "[fs] copy if src and dest are the same paths", async (tempDir: string) => { const srcFile = path.join(tempDir, "copy_file_same.txt"); const destFile = path.join(tempDir, "copy_file_same.txt"); await assertRejects( async () => { await copy(srcFile, destFile); }, Error, "Source and destination cannot be the same.", ); },);
testCopy( "[fs] copy file", async (tempDir: string) => { const srcFile = path.join(testdataDir, "copy_file.txt"); const destFile = path.join(tempDir, "copy_file_copy.txt");
const srcContent = new TextDecoder().decode(await Deno.readFile(srcFile));
assert(await Deno.lstat(srcFile), "source should exist before copy"); await assertRejects( async () => await Deno.lstat(destFile), "destination should not exist before copy", );
await copy(srcFile, destFile);
assert(await Deno.lstat(srcFile), "source should exist after copy"); assert(await Deno.lstat(destFile), "destination should exist after copy");
const destContent = new TextDecoder().decode(await Deno.readFile(destFile));
assertEquals( srcContent, destContent, "source and destination should have the same content", );
// Copy again and it should throw an error. await assertRejects( async () => { await copy(srcFile, destFile); }, Error, `'${destFile}' already exists.`, );
// Modify destination file. await Deno.writeFile(destFile, new TextEncoder().encode("txt copy"));
assertEquals( new TextDecoder().decode(await Deno.readFile(destFile)), "txt copy", );
// Copy again with overwrite option. await copy(srcFile, destFile, { overwrite: true });
// Make sure the file has been overwritten. assertEquals( new TextDecoder().decode(await Deno.readFile(destFile)), "txt", ); },);
testCopy( "[fs] copy with preserve timestamps", async (tempDir: string) => { const srcFile = path.join(testdataDir, "copy_file.txt"); const destFile = path.join(tempDir, "copy_file_copy.txt");
const srcStatInfo = await Deno.stat(srcFile);
assert(srcStatInfo.atime instanceof Date); assert(srcStatInfo.mtime instanceof Date);
// Copy with overwrite and preserve timestamps options. await copy(srcFile, destFile, { overwrite: true, preserveTimestamps: true, });
const destStatInfo = await Deno.stat(destFile);
assert(destStatInfo.atime instanceof Date); assert(destStatInfo.mtime instanceof Date); assertEquals(destStatInfo.atime, srcStatInfo.atime); assertEquals(destStatInfo.mtime, srcStatInfo.mtime); },);
testCopy( "[fs] copy directory to its subdirectory", async (tempDir: string) => { const srcDir = path.join(tempDir, "parent"); const destDir = path.join(srcDir, "child");
await ensureDir(srcDir);
await assertRejects( async () => { await copy(srcDir, destDir); }, Error, `Cannot copy '${srcDir}' to a subdirectory of itself, '${destDir}'.`, ); },);
testCopy( "[fs] copy directory and destination exist and not a directory", async (tempDir: string) => { const srcDir = path.join(tempDir, "parent"); const destDir = path.join(tempDir, "child.txt");
await ensureDir(srcDir); await ensureFile(destDir);
await assertRejects( async () => { await copy(srcDir, destDir); }, Error, `Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'.`, ); },);
testCopy( "[fs] copy directory", async (tempDir: string) => { const srcDir = path.join(testdataDir, "copy_dir"); const destDir = path.join(tempDir, "copy_dir"); const srcFile = path.join(srcDir, "0.txt"); const destFile = path.join(destDir, "0.txt"); const srcNestFile = path.join(srcDir, "nest", "0.txt"); const destNestFile = path.join(destDir, "nest", "0.txt");
await copy(srcDir, destDir);
assert(await Deno.lstat(destFile)); assert(await Deno.lstat(destNestFile));
// After copy. The source and destination should have the same content. assertEquals( new TextDecoder().decode(await Deno.readFile(srcFile)), new TextDecoder().decode(await Deno.readFile(destFile)), ); assertEquals( new TextDecoder().decode(await Deno.readFile(srcNestFile)), new TextDecoder().decode(await Deno.readFile(destNestFile)), );
// Copy again without overwrite option and it should throw an error. await assertRejects( async () => { await copy(srcDir, destDir); }, Error, `'${destDir}' already exists.`, );
// Modify the file in the destination directory. await Deno.writeFile(destNestFile, new TextEncoder().encode("nest copy")); assertEquals( new TextDecoder().decode(await Deno.readFile(destNestFile)), "nest copy", );
// Copy again with overwrite option. await copy(srcDir, destDir, { overwrite: true });
// Make sure the file has been overwritten. assertEquals( new TextDecoder().decode(await Deno.readFile(destNestFile)), "nest", ); },);
testCopy( "[fs] copy symlink file", async (tempDir: string) => { const dir = path.join(testdataDir, "copy_dir_link_file"); const srcLink = path.join(dir, "0.txt"); const destLink = path.join(tempDir, "0_copy.txt");
assert( (await Deno.lstat(srcLink)).isSymlink, `'${srcLink}' should be symlink type`, );
await copy(srcLink, destLink);
const statInfo = await Deno.lstat(destLink);
assert(statInfo.isSymlink, `'${destLink}' should be symlink type`); },);
testCopy( "[fs] copy symlink directory", async (tempDir: string) => { const srcDir = path.join(testdataDir, "copy_dir"); const srcLink = path.join(tempDir, "copy_dir_link"); const destLink = path.join(tempDir, "copy_dir_link_copy");
await ensureSymlink(srcDir, srcLink);
assert( (await Deno.lstat(srcLink)).isSymlink, `'${srcLink}' should be symlink type`, );
await copy(srcLink, destLink);
const statInfo = await Deno.lstat(destLink);
assert(statInfo.isSymlink); },);
testCopySync( "[fs] copy file synchronously if it does not exist", (tempDir: string) => { const srcFile = path.join(testdataDir, "copy_file_not_exists_sync.txt"); const destFile = path.join(tempDir, "copy_file_not_exists_1_sync.txt"); assertThrows(() => { copySync(srcFile, destFile); }); },);
testCopySync( "[fs] copy synchronously with preserve timestamps", (tempDir: string) => { const srcFile = path.join(testdataDir, "copy_file.txt"); const destFile = path.join(tempDir, "copy_file_copy.txt");
const srcStatInfo = Deno.statSync(srcFile);
assert(srcStatInfo.atime instanceof Date); assert(srcStatInfo.mtime instanceof Date);
// Copy with overwrite and preserve timestamps options. copySync(srcFile, destFile, { overwrite: true, preserveTimestamps: true, });
const destStatInfo = Deno.statSync(destFile);
assert(destStatInfo.atime instanceof Date); assert(destStatInfo.mtime instanceof Date); // TODO(bartlomieju): Activate test when https://github.com/denoland/deno/issues/2411 // is fixed // assertEquals(destStatInfo.atime, srcStatInfo.atime); // assertEquals(destStatInfo.mtime, srcStatInfo.mtime); },);
testCopySync( "[fs] copy synchronously if src and dest are the same paths", () => { const srcFile = path.join(testdataDir, "copy_file_same_sync.txt"); assertThrows( () => { copySync(srcFile, srcFile); }, Error, "Source and destination cannot be the same.", ); },);
testCopySync("[fs] copy file synchronously", (tempDir: string) => { const srcFile = path.join(testdataDir, "copy_file.txt"); const destFile = path.join(tempDir, "copy_file_copy_sync.txt");
const srcContent = new TextDecoder().decode(Deno.readFileSync(srcFile));
assertEquals(existsSync(srcFile), true); assertEquals(existsSync(destFile), false);
copySync(srcFile, destFile);
assertEquals(existsSync(srcFile), true); assertEquals(existsSync(destFile), true);
const destContent = new TextDecoder().decode(Deno.readFileSync(destFile));
assertEquals(srcContent, destContent);
// Copy again without overwrite option and it should throw an error. assertThrows( () => { copySync(srcFile, destFile); }, Error, `'${destFile}' already exists.`, );
// Modify destination file. Deno.writeFileSync(destFile, new TextEncoder().encode("txt copy"));
assertEquals( new TextDecoder().decode(Deno.readFileSync(destFile)), "txt copy", );
// Copy again with overwrite option. copySync(srcFile, destFile, { overwrite: true });
// Make sure the file has been overwritten. assertEquals(new TextDecoder().decode(Deno.readFileSync(destFile)), "txt");});
testCopySync( "[fs] copy directory synchronously to its subdirectory", (tempDir: string) => { const srcDir = path.join(tempDir, "parent"); const destDir = path.join(srcDir, "child");
ensureDirSync(srcDir);
assertThrows( () => { copySync(srcDir, destDir); }, Error, `Cannot copy '${srcDir}' to a subdirectory of itself, '${destDir}'.`, ); },);
testCopySync( "[fs] copy directory synchronously, and destination exist and not a " + "directory", (tempDir: string) => { const srcDir = path.join(tempDir, "parent_sync"); const destDir = path.join(tempDir, "child.txt");
ensureDirSync(srcDir); ensureFileSync(destDir);
assertThrows( () => { copySync(srcDir, destDir); }, Error, `Cannot overwrite non-directory '${destDir}' with directory '${srcDir}'.`, ); },);
testCopySync("[fs] copy directory synchronously", (tempDir: string) => { const srcDir = path.join(testdataDir, "copy_dir"); const destDir = path.join(tempDir, "copy_dir_copy_sync"); const srcFile = path.join(srcDir, "0.txt"); const destFile = path.join(destDir, "0.txt"); const srcNestFile = path.join(srcDir, "nest", "0.txt"); const destNestFile = path.join(destDir, "nest", "0.txt");
copySync(srcDir, destDir);
assertEquals(existsSync(destFile), true); assertEquals(existsSync(destNestFile), true);
// After copy. The source and destination should have the same content. assertEquals( new TextDecoder().decode(Deno.readFileSync(srcFile)), new TextDecoder().decode(Deno.readFileSync(destFile)), ); assertEquals( new TextDecoder().decode(Deno.readFileSync(srcNestFile)), new TextDecoder().decode(Deno.readFileSync(destNestFile)), );
// Copy again without overwrite option and it should throw an error. assertThrows( () => { copySync(srcDir, destDir); }, Error, `'${destDir}' already exists.`, );
// Modify the file in the destination directory. Deno.writeFileSync(destNestFile, new TextEncoder().encode("nest copy")); assertEquals( new TextDecoder().decode(Deno.readFileSync(destNestFile)), "nest copy", );
// Copy again with overwrite option. copySync(srcDir, destDir, { overwrite: true });
// Make sure the file has been overwritten. assertEquals( new TextDecoder().decode(Deno.readFileSync(destNestFile)), "nest", );});
testCopySync( "[fs] copy symlink file synchronously", (tempDir: string) => { const dir = path.join(testdataDir, "copy_dir_link_file"); const srcLink = path.join(dir, "0.txt"); const destLink = path.join(tempDir, "0_copy.txt");
assert( Deno.lstatSync(srcLink).isSymlink, `'${srcLink}' should be symlink type`, );
copySync(srcLink, destLink);
const statInfo = Deno.lstatSync(destLink);
assert(statInfo.isSymlink, `'${destLink}' should be symlink type`); },);
testCopySync( "[fs] copy symlink directory synchronously", (tempDir: string) => { const originDir = path.join(testdataDir, "copy_dir"); const srcLink = path.join(tempDir, "copy_dir_link"); const destLink = path.join(tempDir, "copy_dir_link_copy");
ensureSymlinkSync(originDir, srcLink);
assert( Deno.lstatSync(srcLink).isSymlink, `'${srcLink}' should be symlink type`, );
copySync(srcLink, destLink);
const statInfo = Deno.lstatSync(destLink);
assert(statInfo.isSymlink); },);
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago