-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsafeCopy.test.mjs
More file actions
122 lines (93 loc) · 4.27 KB
/
safeCopy.test.mjs
File metadata and controls
122 lines (93 loc) · 4.27 KB
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
'use strict';
import assert from 'node:assert';
import { mkdir, readFile, rm, utimes, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, it } from 'node:test';
import { safeCopy } from '../safeCopy.mjs';
describe('safeCopy', () => {
const testDir = join(import.meta.dirname, 'test-safe-copy');
const srcDir = join(testDir, 'src');
const targetDir = join(testDir, 'target');
beforeEach(async () => {
// Create test directories
await mkdir(srcDir, { recursive: true });
await mkdir(targetDir, { recursive: true });
});
afterEach(async () => {
// Clean up test directories
await rm(testDir, { recursive: true, force: true });
});
it('should copy new files that do not exist in target', async () => {
// Create a file in source
await writeFile(join(srcDir, 'file1.txt'), 'content1');
await safeCopy(srcDir, targetDir);
// Verify file was copied
const content = await readFile(join(targetDir, 'file1.txt'), 'utf-8');
assert.strictEqual(content, 'content1');
});
it('should copy multiple files', async () => {
// Create multiple files in source
await writeFile(join(srcDir, 'file1.txt'), 'content1');
await writeFile(join(srcDir, 'file2.txt'), 'content2');
await writeFile(join(srcDir, 'file3.txt'), 'content3');
await safeCopy(srcDir, targetDir);
// Verify all files were copied
const content1 = await readFile(join(targetDir, 'file1.txt'), 'utf-8');
const content2 = await readFile(join(targetDir, 'file2.txt'), 'utf-8');
const content3 = await readFile(join(targetDir, 'file3.txt'), 'utf-8');
assert.strictEqual(content1, 'content1');
assert.strictEqual(content2, 'content2');
assert.strictEqual(content3, 'content3');
});
it('should skip files with same size and older modification time', async () => {
// Create file in source with specific size
const content = 'same content';
await writeFile(join(srcDir, 'file1.txt'), content);
// Make source file old
const oldTime = new Date(Date.now() - 10000);
await utimes(join(srcDir, 'file1.txt'), oldTime, oldTime);
// Create target file with same size but different content and newer timestamp
await writeFile(join(targetDir, 'file1.txt'), 'other things');
await safeCopy(srcDir, targetDir);
// Verify file was not overwritten (source is older)
const targetContent = await readFile(join(targetDir, 'file1.txt'), 'utf-8');
assert.strictEqual(targetContent, 'other things');
});
it('should copy files when source has newer modification time', async () => {
// Create files in both directories
await writeFile(join(srcDir, 'file1.txt'), 'new content');
await writeFile(join(targetDir, 'file1.txt'), 'old content');
// Make target file older
const oldTime = new Date(Date.now() - 10000);
await utimes(join(targetDir, 'file1.txt'), oldTime, oldTime);
await safeCopy(srcDir, targetDir);
// Verify file was updated
const content = await readFile(join(targetDir, 'file1.txt'), 'utf-8');
assert.strictEqual(content, 'new content');
});
it('should copy files when sizes differ', async () => {
// Create files with different sizes
await writeFile(join(srcDir, 'file1.txt'), 'short');
await writeFile(join(targetDir, 'file1.txt'), 'much longer content');
await safeCopy(srcDir, targetDir);
// Verify file was updated
const content = await readFile(join(targetDir, 'file1.txt'), 'utf-8');
assert.strictEqual(content, 'short');
});
it('should handle empty source directory', async () => {
// Don't create any files in source
await safeCopy(srcDir, targetDir);
});
it('should copy files with same size but different content when mtime is newer', async () => {
// Create files with same size but different content
await writeFile(join(srcDir, 'file1.txt'), 'abcde');
await writeFile(join(targetDir, 'file1.txt'), 'fghij');
// Make target older
const oldTime = new Date(Date.now() - 10000);
await utimes(join(targetDir, 'file1.txt'), oldTime, oldTime);
await safeCopy(srcDir, targetDir);
// Verify file was updated with source content
const content = await readFile(join(targetDir, 'file1.txt'), 'utf-8');
assert.strictEqual(content, 'abcde');
});
});