/
check.test.js
165 lines (110 loc) · 4.07 KB
/
check.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
'use strict';
const fs = require('graceful-fs');
const rimraf = require('rimraf');
const pDelay = require('delay');
const mkdirp = require('mkdirp');
const lockfile = require('../');
const unlockAll = require('./util/unlockAll');
const tmpDir = `${__dirname}/tmp`;
beforeAll(() => mkdirp.sync(tmpDir));
afterAll(() => rimraf.sync(tmpDir));
afterEach(async () => {
await unlockAll();
rimraf.sync(`${tmpDir}/*`);
});
it('should fail if the file does not exist by default', async () => {
expect.assertions(1);
try {
await lockfile.check(`${tmpDir}/some-file-that-will-never-exist`);
} catch (err) {
expect(err.code).toBe('ENOENT');
}
});
it('should not fail if the file does not exist and realpath is false', async () => {
await lockfile.check(`${tmpDir}/some-file-that-will-never-exist`, { realpath: false });
});
it('should return a promise', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const promise = lockfile.check(`${tmpDir}/foo`);
expect(typeof promise.then).toBe('function');
await promise;
});
it('should resolve with true if file is locked', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
await lockfile.lock(`${tmpDir}/foo`);
const isLocked = await lockfile.check(`${tmpDir}/foo`);
expect(isLocked).toBe(true);
});
it('should resolve with false if file is not locked', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const isLocked = await lockfile.check(`${tmpDir}/foo`);
expect(isLocked).toBe(false);
});
it('should use the custom fs', async () => {
const customFs = {
...fs,
realpath: (path, callback) => callback(new Error('foo')),
};
expect.assertions(1);
try {
await lockfile.check(`${tmpDir}/foo`, { fs: customFs });
} catch (err) {
expect(err.message).toBe('foo');
}
});
it('should resolve symlinks by default', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
fs.symlinkSync(`${tmpDir}/foo`, `${tmpDir}/bar`);
await lockfile.lock(`${tmpDir}/bar`);
let isLocked = await lockfile.check(`${tmpDir}/bar`);
expect(isLocked).toBe(true);
isLocked = await lockfile.check(`${tmpDir}/foo`);
expect(isLocked).toBe(true);
});
it('should not resolve symlinks if realpath is false', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
fs.symlinkSync(`${tmpDir}/foo`, `${tmpDir}/bar`);
await lockfile.lock(`${tmpDir}/bar`, { realpath: false });
let isLocked = await lockfile.check(`${tmpDir}/bar`, { realpath: false });
expect(isLocked).toBe(true);
isLocked = await lockfile.check(`${tmpDir}/foo`, { realpath: false });
expect(isLocked).toBe(false);
});
it('should fail if stating the lockfile errors out when verifying staleness', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const mtime = new Date(Date.now() - 60000);
const customFs = {
...fs,
stat: (path, callback) => callback(new Error('foo')),
};
fs.mkdirSync(`${tmpDir}/foo.lock`);
fs.utimesSync(`${tmpDir}/foo.lock`, mtime, mtime);
expect.assertions(1);
try {
await lockfile.check(`${tmpDir}/foo`, { fs: customFs });
} catch (err) {
expect(err.message).toBe('foo');
}
});
it('should set stale to a minimum of 2000', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
fs.mkdirSync(`${tmpDir}/foo.lock`);
expect.assertions(2);
await pDelay(200);
let isLocked = await lockfile.check(`${tmpDir}/foo`, { stale: 100 });
expect(isLocked).toBe(true);
await pDelay(2000);
isLocked = await lockfile.check(`${tmpDir}/foo`, { stale: 100 });
expect(isLocked).toBe(false);
});
it('should set stale to a minimum of 2000 (falsy)', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
fs.mkdirSync(`${tmpDir}/foo.lock`);
expect.assertions(2);
await pDelay(200);
let isLocked = await lockfile.check(`${tmpDir}/foo`, { stale: false });
expect(isLocked).toBe(true);
await pDelay(2000);
isLocked = await lockfile.check(`${tmpDir}/foo`, { stale: false });
expect(isLocked).toBe(false);
});