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
|
import fs from "fs";
import { describe, test, expect, jest } from "bun:test";
import { tempDirWithFiles } from "harness";
const impls = [
// ["cpSync", fs.cpSync],
["cp", fs.promises.cp],
] as const;
for (const [name, copy] of impls) {
async function copyShouldThrow(...args: Parameters<typeof copy>) {
try {
await (copy as any)(...args);
} catch (e: any) {
if (e?.code?.toUpperCase() === "TODO") {
throw new Error("Expected " + name + "() to throw non TODO error");
}
return e;
}
throw new Error("Expected " + name + "() to throw");
}
function assertContent(path: string, content: string) {
expect(fs.readFileSync(path, "utf8")).toBe(content);
}
describe("fs." + name, () => {
test("single file", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
});
await copy(basename + "/from/a.txt", basename + "/to.txt");
expect(fs.readFileSync(basename + "/to.txt", "utf8")).toBe("a");
});
test("refuse to copy directory with 'recursive: false'", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
});
await copyShouldThrow(basename + "/from", basename + "/result");
});
test("recursive directory structure - no destination", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
"from/b/e.txt": "e",
"from/c.txt": "c",
"from/w/y/x/z.txt": "z",
});
await copy(basename + "/from", basename + "/result", { recursive: true });
assertContent(basename + "/result/a.txt", "a");
assertContent(basename + "/result/b/e.txt", "e");
assertContent(basename + "/result/c.txt", "c");
assertContent(basename + "/result/w/y/x/z.txt", "z");
});
test("recursive directory structure - overwrite existing files by default", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
"from/b/e.txt": "e",
"from/c.txt": "c",
"from/w/y/x/z.txt": "z",
"result/a.txt": "fail",
"result/w/y/x/z.txt": "lose",
"result/w/y/v.txt": "keep this",
});
await copy(basename + "/from", basename + "/result", { recursive: true });
assertContent(basename + "/result/a.txt", "a");
assertContent(basename + "/result/b/e.txt", "e");
assertContent(basename + "/result/c.txt", "c");
assertContent(basename + "/result/w/y/x/z.txt", "z");
assertContent(basename + "/result/w/y/v.txt", "keep this");
});
test("recursive directory structure - 'force: false' does not overwrite existing files", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "lose",
"from/b/e.txt": "e",
"from/c.txt": "c",
"from/w/y/x/z.txt": "lose",
"result/a.txt": "win",
"result/w/y/x/z.txt": "win",
"result/w/y/v.txt": "keep this",
});
await copy(basename + "/from", basename + "/result", { recursive: true, force: false });
assertContent(basename + "/result/a.txt", "win");
assertContent(basename + "/result/b/e.txt", "e");
assertContent(basename + "/result/c.txt", "c");
assertContent(basename + "/result/w/y/x/z.txt", "win");
assertContent(basename + "/result/w/y/v.txt", "keep this");
});
test("'force: false' on a single file doesn't override", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "lose",
"result/a.txt": "win",
});
await copy(basename + "/from/a.txt", basename + "/result/a.txt", { force: false });
assertContent(basename + "/result/a.txt", "win");
});
test("'force: true' on a single file does override", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "win",
"result/a.txt": "lose",
});
await copy(basename + "/from/a.txt", basename + "/result/a.txt", { force: true });
assertContent(basename + "/result/a.txt", "win");
});
test("'force: false' + 'errorOnExist: true' can throw", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "lose",
"result/a.txt": "win",
});
await copyShouldThrow(basename + "/from/a.txt", basename + "/result/a.txt", { force: false, errorOnExist: true });
assertContent(basename + "/result/a.txt", "win");
});
test("symlinks - single file", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
});
fs.symlinkSync(basename + "/from/a.txt", basename + "/from/a_symlink.txt");
await copy(basename + "/from/a_symlink.txt", basename + "/result.txt");
await copy(basename + "/from/a_symlink.txt", basename + "/result2.txt", { recursive: false });
const stats = fs.lstatSync(basename + "/result.txt");
expect(stats.isSymbolicLink()).toBe(true);
expect(fs.readFileSync(basename + "/result.txt", "utf8")).toBe("a");
const stats2 = fs.lstatSync(basename + "/result2.txt");
expect(stats2.isSymbolicLink()).toBe(true);
expect(fs.readFileSync(basename + "/result2.txt", "utf8")).toBe("a");
});
test("symlinks - single file recursive", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
});
fs.symlinkSync(basename + "/from/a.txt", basename + "/from/a_symlink.txt");
await copy(basename + "/from/a_symlink.txt", basename + "/result.txt", { recursive: true });
const stats = fs.lstatSync(basename + "/result.txt");
expect(stats.isSymbolicLink()).toBe(true);
expect(fs.readFileSync(basename + "/result.txt", "utf8")).toBe("a");
});
test("symlinks - directory recursive", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
"from/b.txt": "b",
"from/dir/c.txt": "c",
});
fs.symlinkSync(basename + "/from/a.txt", basename + "/from/a_symlink.txt");
fs.symlinkSync(basename + "/from/dir", basename + "/from/dir_symlink");
await copy(basename + "/from", basename + "/result", { recursive: true });
const statsFile = fs.lstatSync(basename + "/result/a_symlink.txt");
expect(statsFile.isSymbolicLink()).toBe(true);
expect(fs.readFileSync(basename + "/result/a_symlink.txt", "utf8")).toBe("a");
const statsDir = fs.lstatSync(basename + "/result/dir_symlink");
expect(statsDir.isSymbolicLink()).toBe(true);
expect(fs.readdirSync(basename + "/result/dir_symlink")).toEqual(["c.txt"]);
});
test("symlinks - directory recursive 2", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
"from/b.txt": "b",
"from/dir/c.txt": "c",
});
fs.symlinkSync(basename + "/from/a.txt", basename + "/from/a_symlink.txt");
fs.symlinkSync(basename + "/from/dir", basename + "/from/dir_symlink");
fs.mkdirSync(basename + "/result");
await copy(basename + "/from", basename + "/result", { recursive: true });
const statsFile = fs.lstatSync(basename + "/result/a_symlink.txt");
expect(statsFile.isSymbolicLink()).toBe(true);
expect(fs.readFileSync(basename + "/result/a_symlink.txt", "utf8")).toBe("a");
const statsDir = fs.lstatSync(basename + "/result/dir_symlink");
expect(statsDir.isSymbolicLink()).toBe(true);
expect(fs.readdirSync(basename + "/result/dir_symlink")).toEqual(["c.txt"]);
});
test("filter - works", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
"from/b.txt": "b",
});
await copy(basename + "/from", basename + "/result", {
filter: (src: string) => {
return src.endsWith("/from") || src.includes("a.txt");
},
recursive: true,
});
expect(fs.existsSync(basename + "/result/a.txt")).toBe(true);
expect(fs.existsSync(basename + "/result/b.txt")).toBe(false);
});
test("filter - paths given are correct and relative", async () => {
const basename = tempDirWithFiles("cp", {
"from/a.txt": "a",
"from/b.txt": "b",
});
const filter = jest.fn((src: string) => true);
let prev = process.cwd();
process.chdir(basename);
await copy(basename + "/from", basename + "/result", {
filter,
recursive: true,
});
process.chdir(prev);
expect(filter.mock.calls.sort((a, b) => a[0].localeCompare(b[0]))).toEqual([
[basename + "/from", basename + "/result"],
[basename + "/from/a.txt", basename + "/result/a.txt"],
[basename + "/from/b.txt", basename + "/result/b.txt"],
]);
});
});
}
|