forked from sindresorhus/trash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
170 lines (135 loc) · 3.72 KB
/
test.js
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
import fs from 'fs';
import path from 'path';
import test from 'ava';
import tempfile from 'tempfile';
import m from '.';
const tmpdir = tempfile();
fs.mkdirSync(tmpdir);
process.chdir(tmpdir);
test('files', async t => {
const weirdName = process.platform === 'darwin' ? 'weird\\\\name\\"\'' : 'fixture3';
fs.writeFileSync('fixture', '');
fs.writeFileSync('fixture2', '');
fs.writeFileSync(weirdName, '');
fs.writeFileSync('123', '');
t.true(fs.existsSync('fixture'));
t.true(fs.existsSync('fixture2'));
t.true(fs.existsSync(weirdName));
t.true(fs.existsSync('123'));
await m([
'fixture',
'fixture2',
weirdName,
123
]);
t.false(fs.existsSync('fixture'));
t.false(fs.existsSync('fixture2'));
t.false(fs.existsSync(weirdName));
t.false(fs.existsSync('123'));
});
test('glob', async t => {
fs.writeFileSync('fixture.jpg', '');
fs.writeFileSync('fixture.png', '');
t.true(fs.existsSync('fixture.jpg'));
t.true(fs.existsSync('fixture.png'));
await m([
'*.jpg'
]);
t.false(fs.existsSync('fixture.jpg'));
t.true(fs.existsSync('fixture.png'));
});
test('no glob', async t => {
fs.writeFileSync('fixture-noglob*.js', '');
fs.writeFileSync('fixture-noglob1.js', '');
await m(['fixture-noglob*.js'], {glob: false});
t.false(fs.existsSync('fixture-noglob*.js'));
t.true(fs.existsSync('fixture-noglob1.js'));
});
test('string pattern', async t => {
fs.writeFileSync('a', '');
fs.writeFileSync('b', '');
fs.writeFileSync('ab', '');
t.true(fs.existsSync('a'));
t.true(fs.existsSync('b'));
t.true(fs.existsSync('ab'));
await m(
'ab'
);
t.false(fs.existsSync('ab'));
t.true(fs.existsSync('a'));
t.true(fs.existsSync('b'));
});
test('directories', async t => {
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, '');
t.true(fs.existsSync(d1f1));
t.true(fs.existsSync(d1f2));
fs.mkdirSync('321');
fs.writeFileSync(d2f1, '');
fs.writeFileSync(d2f2, '');
t.true(fs.existsSync(d2f1));
t.true(fs.existsSync(d2f2));
await m([
'fdir',
321
]);
t.false(fs.existsSync('fdir'));
t.false(fs.existsSync(321));
});
(process.platform === 'linux' ? test : test.failing)('tons of files', async t => {
const FILE_COUNT = 5000;
const paths = [];
for (let i = 0; i < FILE_COUNT; i++) {
paths.push('file' + i);
fs.writeFileSync('file' + i, '');
}
await t.notThrows(m(paths));
for (let i = 0; i < FILE_COUNT; i++) {
t.false(fs.existsSync('file' + i));
}
});
test('symlinks', async t => {
fs.writeFileSync('aaa', '');
fs.symlinkSync('aaa', 'bbb');
fs.symlinkSync('ddd', 'ccc');
t.truthy(fs.lstatSync('aaa'));
t.truthy(fs.lstatSync('bbb'));
t.truthy(fs.lstatSync('ccc'));
await m([
'bbb',
'ccc'
]);
t.truthy(fs.lstatSync('aaa'));
t.throws(() => fs.lstatSync('bbb'));
t.throws(() => fs.lstatSync('ccc'));
});
if (process.platform === 'linux') {
test('create trashinfo', async t => {
t.plan(1);
fs.writeFileSync('f2', '');
const info = `[Trash Info]\nPath=${path.resolve('f2')}`;
const files = await m(['f2']);
const infoFile = fs.readFileSync(files[0].info, 'utf8');
t.is(infoFile.trim().indexOf(info.trim()), 0);
});
test('preserve file attributes', async t => {
t.plan(4);
fs.writeFileSync('f3', '');
const statSrc = fs.statSync('f3');
const files = await m(['f3']);
const statDest = fs.statSync(files[0].path);
t.is(statSrc.mode, statDest.mode);
t.is(statSrc.uid, statDest.uid);
t.is(statSrc.gid, statDest.gid);
t.is(statSrc.size, statDest.size);
});
}
test('non-existent files', async t => {
t.false(fs.existsSync('fixture-enoent'));
await t.notThrows(m('fixture-enoent'));
});