-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
157 lines (132 loc) · 4.55 KB
/
Copy pathindex.test.js
File metadata and controls
157 lines (132 loc) · 4.55 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
const path = require('path');
const fs = require('fs-extra');
const execa = require('execa');
const bin = path.resolve('./index.js');
const tempdir = path.resolve('./tmp-node-delete-cli-test');
const cwd = process.cwd();
const del = async (paths) => execa(bin, paths);
beforeAll(async () => {
await fs.emptyDir(tempdir);
await exists(tempdir, true);
process.chdir(tempdir);
});
afterAll(async () => {
process.chdir(cwd);
await fs.remove(tempdir);
await exists(tempdir, false);
});
it('should delete files', async () => {
await setupDir('foo/files');
await del(['foo/files/1.txt', 'foo/files/2.txt']);
await exists('foo/files/1.txt', false);
await exists('foo/files/2.txt', false);
await exists('foo/files/empty', true);
await exists('foo/files/-', true);
await exists('foo/files/3.txt', true);
await exists('foo/files/-.txt', true);
});
it('should delete directories', async () => {
await setupDir('foo/dirs');
await del(['foo/dirs/empty']);
await exists('foo/dirs/empty', false);
await exists('foo/dirs/-', true);
await exists('foo/dirs/1.txt', true);
await exists('foo/dirs/2.txt', true);
await exists('foo/dirs/3.txt', true);
await exists('foo/dirs/-.txt', true);
await setupDir('foo/dirs');
await del(['foo/dirs']);
await exists('foo/dirs', false);
});
it('should not throw error if path does not exist', async () => {
await setupDir('foo/silent');
await exists('foo/silent/invalid', false);
await del(['foo/silent/1.txt', 'foo/silent/invalid', 'foo/silent/2.txt']);
await exists('foo/silent/1.txt', false);
await exists('foo/silent/2.txt', false);
await exists('foo/silent/empty', true);
await exists('foo/silent/-', true);
await exists('foo/silent/3.txt', true);
await exists('foo/silent/-.txt', true);
});
it('should delete files and directories starting with -', async () => {
await setupDir('foo/dash');
await del(['foo/dash/-']);
await exists('foo/dash/-', false);
await exists('foo/dash/-.txt', true);
await del(['foo/dash/-.txt']);
await exists('foo/dash/-.txt', false);
await exists('foo/dash/empty', true);
await exists('foo/dash/1.txt', true);
await exists('foo/dash/2.txt', true);
await exists('foo/dash/3.txt', true);
});
it('should delete write-protected files and directories', async () => {
await setupDir('foo/perm');
await fs.chmod('foo/perm/1.txt', 0o400);
await del(['foo/perm/1.txt']);
await exists('foo/perm/1.txt', false);
await fs.chmod('foo/perm/empty', 0o400);
await del(['foo/perm/empty']);
await exists('foo/perm/empty', false);
await exists('foo/perm/-', true);
await exists('foo/perm/2.txt', true);
await exists('foo/perm/3.txt', true);
await exists('foo/perm/-.txt', true);
});
it('should delete normal directories having write-protected files and directories', async () => {
await setupDir('foo/permsub', 0o400);
await fs.chmod('foo/permsub', 0o700);
await del(['foo/permsub']);
await exists('foo/permsub', false);
});
it('should delete write-protected directories having normal files and directories', async () => {
await setupDir('foo/permparent');
await fs.chmod('foo/permparent', 0o400);
await del(['foo/permparent']);
await exists('foo/permparent', false);
});
it('should delete write-protected directories having write-protected files and directories', async () => {
await setupDir('foo/permparentsub', 0o400);
await del(['foo/permparentsub']);
await exists('foo/permparentsub', false);
});
it('should delete deeply nested write-protected files and directories', async () => {
await setupDir('foo/permnested/sub1/sub1', 0o400);
await setupDir('foo/permnested/sub1/sub2', 0o400);
await setupDir('foo/permnested/sub2/sub1', 0o400);
await setupDir('foo/permnested/sub2/sub2', 0o400);
await setupDir('foo/permnested/sub1', 0o400);
await setupDir('foo/permnested/sub2', 0o400);
await setupDir('foo/permnested', 0o400);
await del(['foo/permnested']);
await exists('foo/permnested', false);
});
const setupDir = async (p, mode) => {
await mkdir(p + '/empty', mode);
await mkdir(p + '/-', mode);
await touch(p + '/1.txt', mode);
await touch(p + '/2.txt', mode);
await touch(p + '/3.txt', mode);
await touch(p + '/-.txt', mode);
if (mode) {
await fs.chmod(p, mode);
}
};
const mkdir = async (p, mode) => {
await fs.ensureDir(p);
if (mode) {
await fs.chmod(p, mode);
}
await exists(p, true);
};
const touch = async (p, mode) => {
await fs.ensureFile(p);
if (mode) {
await fs.chmod(p, mode);
}
await exists(p, true);
};
const exists = async (p, expected) => {
expect(await fs.pathExists(p)).toBe(expected);
};