-
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathunlinkSync.test.ts
More file actions
89 lines (80 loc) 路 2.56 KB
/
Copy pathunlinkSync.test.ts
File metadata and controls
89 lines (80 loc) 路 2.56 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
import { create } from '../util';
describe('unlinkSync', () => {
it('removes a file', () => {
const vol = create({
'/foo.txt': 'bar',
'/baz.txt': 'qux',
});
vol.unlinkSync('/foo.txt');
expect(vol.toJSON()).toEqual({
'/baz.txt': 'qux',
});
});
it('throws ENOENT when file does not exist', () => {
const vol = create({
'/foo.txt': 'bar',
});
expect(() => vol.unlinkSync('/bar.txt')).toThrowError(
new Error("ENOENT: no such file or directory, unlink '/bar.txt'"),
);
});
describe('when path is a directory', () => {
it('throws EPERM for an empty directory', () => {
const vol = create({});
vol.mkdirSync('/dir');
expect(() => vol.unlinkSync('/dir')).toThrowError(new Error("EPERM: operation not permitted, unlink '/dir'"));
expect(vol.existsSync('/dir')).toBe(true);
});
it('throws EPERM for a non-empty directory', () => {
const vol = create({
'/dir/foo.txt': 'bar',
});
expect(() => vol.unlinkSync('/dir')).toThrowError(new Error("EPERM: operation not permitted, unlink '/dir'"));
expect(vol.toJSON()).toEqual({
'/dir/foo.txt': 'bar',
});
});
it('throws EPERM for the root directory', () => {
const vol = create({
'/foo.txt': 'bar',
});
expect(() => vol.unlinkSync('/')).toThrowError(new Error("EPERM: operation not permitted, unlink '/'"));
});
it('has an EPERM error code', () => {
const vol = create({});
vol.mkdirSync('/dir');
try {
vol.unlinkSync('/dir');
throw new Error('not this error');
} catch (error) {
expect(error.code).toBe('EPERM');
expect(error.path).toBe('/dir');
}
});
it('removes a symlink pointing to a directory', () => {
const vol = create({
'/dir/foo.txt': 'bar',
});
vol.symlinkSync('/dir', '/link');
vol.unlinkSync('/link');
expect(vol.existsSync('/link')).toBe(false);
expect(vol.existsSync('/dir')).toBe(true);
});
});
it('async unlink of a directory returns EPERM', done => {
const vol = create({});
vol.mkdirSync('/dir');
vol.unlink('/dir', err => {
expect((err as any).code).toBe('EPERM');
expect(vol.existsSync('/dir')).toBe(true);
done();
});
});
it('promises unlink of a directory rejects with EPERM', async () => {
const vol = create({});
vol.mkdirSync('/dir');
await expect(vol.promises.unlink('/dir')).rejects.toThrowError(
new Error("EPERM: operation not permitted, unlink '/dir'"),
);
});
});