-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathtest.js
More file actions
278 lines (222 loc) · 6.87 KB
/
test.js
File metadata and controls
278 lines (222 loc) · 6.87 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
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
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import {test} from 'node:test';
import assert from 'node:assert/strict';
import tempfile from 'tempfile';
import trash from './index.js';
const temporaryDirectory = tempfile();
fs.mkdirSync(temporaryDirectory);
process.chdir(temporaryDirectory);
test('files', async () => {
const weirdName = process.platform === 'darwin' ? String.raw`weird\\name\"'` : 'fixture3';
fs.writeFileSync('fixture', '');
fs.writeFileSync('fixture2', '');
fs.writeFileSync(weirdName, '');
fs.writeFileSync('123', '');
assert.ok(fs.existsSync('fixture'));
assert.ok(fs.existsSync('fixture2'));
assert.ok(fs.existsSync(weirdName));
assert.ok(fs.existsSync('123'));
await trash([
'fixture',
'fixture2',
weirdName,
123,
]);
assert.ok(!fs.existsSync('fixture'));
assert.ok(!fs.existsSync('fixture2'));
assert.ok(!fs.existsSync(weirdName));
assert.ok(!fs.existsSync('123'));
});
test('glob', async () => {
fs.writeFileSync('fixture.jpg', '');
fs.writeFileSync('fixture.png', '');
assert.ok(fs.existsSync('fixture.jpg'));
assert.ok(fs.existsSync('fixture.png'));
await trash([
'*.jpg',
]);
assert.ok(!fs.existsSync('fixture.jpg'));
assert.ok(fs.existsSync('fixture.png'));
});
test('glob with exclusion', async () => {
fs.writeFileSync('file1.txt', '');
fs.writeFileSync('file2.txt', '');
fs.writeFileSync('keep.txt', '');
assert.ok(fs.existsSync('file1.txt'));
assert.ok(fs.existsSync('file2.txt'));
assert.ok(fs.existsSync('keep.txt'));
await trash([
'*.txt',
'!keep.txt',
]);
assert.ok(!fs.existsSync('file1.txt'));
assert.ok(!fs.existsSync('file2.txt'));
assert.ok(fs.existsSync('keep.txt'));
});
test('no glob', async () => {
if (process.platform !== 'win32') {
fs.writeFileSync('fixture-noglob*.js', '');
}
fs.writeFileSync('fixture-noglob1.js', '');
await trash(['fixture-noglob*.js'], {glob: false});
assert.ok(!fs.existsSync('fixture-noglob*.js'));
assert.ok(fs.existsSync('fixture-noglob1.js'));
});
test('string pattern', async () => {
fs.writeFileSync('a', '');
fs.writeFileSync('b', '');
fs.writeFileSync('ab', '');
assert.ok(fs.existsSync('a'));
assert.ok(fs.existsSync('b'));
assert.ok(fs.existsSync('ab'));
await trash('ab');
assert.ok(!fs.existsSync('ab'));
assert.ok(fs.existsSync('a'));
assert.ok(fs.existsSync('b'));
});
test('directories', async () => {
const d1f1 = path.join('fdir', 'fixture');
const d1f2 = path.join('fdir', 'fixture2');
const d2f1 = path.join('321', 'fixture');
const d2f2 = path.join('321', 'fixture2');
fs.mkdirSync('fdir');
fs.writeFileSync(d1f1, '');
fs.writeFileSync(d1f2, '');
assert.ok(fs.existsSync(d1f1));
assert.ok(fs.existsSync(d1f2));
fs.mkdirSync('321');
fs.writeFileSync(d2f1, '');
fs.writeFileSync(d2f2, '');
assert.ok(fs.existsSync(d2f1));
assert.ok(fs.existsSync(d2f2));
await trash([
'fdir',
321,
]);
assert.ok(!fs.existsSync('fdir'));
assert.ok(!fs.existsSync(321));
});
test('tons of files', async () => {
const FILE_COUNT = 5000;
const paths = [];
for (let i = 0; i < FILE_COUNT; i++) {
paths.push('file' + i);
fs.writeFileSync('file' + i, '');
}
await trash(paths);
for (let i = 0; i < FILE_COUNT; i++) {
assert.ok(!fs.existsSync('file' + i));
}
});
test('symlinks', async () => {
fs.writeFileSync('aaa', '');
fs.symlinkSync('aaa', 'bbb');
fs.symlinkSync('ddd', 'ccc');
assert.ok(fs.lstatSync('aaa'));
assert.ok(fs.lstatSync('bbb'));
assert.ok(fs.lstatSync('ccc'));
await trash([
'bbb',
'ccc',
]);
assert.ok(fs.lstatSync('aaa'));
assert.throws(() => fs.lstatSync('bbb'));
assert.throws(() => fs.lstatSync('ccc'));
});
if (process.platform === 'linux') {
test('create trashinfo', async () => {
fs.writeFileSync('f2 ^', '');
const info = `[Trash Info]\nPath=${path.resolve(encodeURI('f2 ^'))}`;
const files = await trash(['f2 ^']);
const infoFile = fs.readFileSync(files[0].info, 'utf8');
assert.equal(infoFile.trim().indexOf(info.trim()), 0);
});
test('preserve file attributes', async () => {
fs.writeFileSync('f3', '');
const statSource = fs.statSync('f3');
const files = await trash(['f3']);
const statDestination = fs.statSync(files[0].path);
assert.equal(statSource.mode, statDestination.mode);
assert.equal(statSource.uid, statDestination.uid);
assert.equal(statSource.gid, statDestination.gid);
assert.equal(statSource.size, statDestination.size);
});
}
test('non-existent files', async () => {
assert.ok(!fs.existsSync('fixture-enoent'));
await assert.doesNotReject(trash('fixture-enoent'));
});
test('glob with nested directories', async () => {
const directory1 = 'foo';
const file1 = path.join('foo', 'bar.txt');
const file2 = path.join('foo', 'baz.txt');
const directory2 = path.join('foo', 'bar');
const directory3 = path.join('foo', 'baz');
const file3 = path.join(directory1, 'foo.txt');
const file4 = path.join(directory2, 'bar.txt');
fs.mkdirSync(directory1);
fs.mkdirSync(directory2);
fs.mkdirSync(directory3);
fs.writeFileSync(file1, '');
fs.writeFileSync(file2, '');
fs.writeFileSync(file3, '');
fs.writeFileSync(file4, '');
assert.ok(fs.existsSync(file1));
assert.ok(fs.existsSync(file2));
assert.ok(fs.existsSync(file3));
assert.ok(fs.existsSync(file4));
await trash(`${directory1}/**`, {glob: true});
assert.ok(!fs.existsSync(directory1));
assert.ok(!fs.existsSync(directory2));
assert.ok(!fs.existsSync(directory3));
});
test('empty array', async () => {
await assert.doesNotReject(trash([]));
});
test('mixed existing and non-existing files', async () => {
fs.writeFileSync('exists1', '');
fs.writeFileSync('exists2', '');
assert.ok(fs.existsSync('exists1'));
assert.ok(fs.existsSync('exists2'));
assert.ok(!fs.existsSync('does-not-exist'));
await trash(['exists1', 'does-not-exist', 'exists2']);
assert.ok(!fs.existsSync('exists1'));
assert.ok(!fs.existsSync('exists2'));
assert.ok(!fs.existsSync('does-not-exist'));
});
test('single file path', async () => {
fs.writeFileSync('single-file', '');
assert.ok(fs.existsSync('single-file'));
await trash('single-file');
assert.ok(!fs.existsSync('single-file'));
});
test('empty directory', async () => {
fs.mkdirSync('empty-dir');
assert.ok(fs.existsSync('empty-dir'));
await trash('empty-dir');
assert.ok(!fs.existsSync('empty-dir'));
});
test('unicode characters in filename', async () => {
const name = '㊕ 八倍縮時影片⏩ 📺 🚲 test.txt';
fs.writeFileSync(name, '');
assert.ok(fs.existsSync(name));
await trash(name);
assert.ok(!fs.existsSync(name));
});
test('invalid surrogate pair in filename', async () => {
const name = 'test\uDE00\uD83D.txt';
fs.writeFileSync(name, '');
assert.ok(fs.existsSync(name));
if (process.platform === 'linux') {
await assert.rejects(trash(name), {
name: 'TypeError',
message: /Cannot trash file with invalid encoding in filename/,
});
fs.unlinkSync(name);
} else {
await trash(name);
assert.ok(!fs.existsSync(name));
}
});