forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocking.test.js
More file actions
223 lines (173 loc) · 7.06 KB
/
locking.test.js
File metadata and controls
223 lines (173 loc) · 7.06 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
import consola from 'consola'
import fs from 'fs-extra'
import properlock from 'proper-lockfile'
import onExit from 'signal-exit'
import { lockPaths, defaultLockOptions, getLockOptions, createLockPath, getLockPath, lock } from '../src/locking'
jest.mock('fs-extra')
jest.mock('proper-lockfile')
jest.mock('signal-exit')
describe('util: locking', () => {
const lockConfig = {
id: 'id',
dir: 'dist',
root: '/project-root'
}
beforeEach(() => jest.resetAllMocks())
beforeEach(() => lockPaths.clear())
test('onCompromised lock warns on compromise by default', () => {
defaultLockOptions.onCompromised()
expect(consola.warn).toHaveBeenCalledTimes(1)
})
test('can override default options', () => {
const options = getLockOptions({ onCompromised: err => consola.fatal(err) })
options.onCompromised()
expect(consola.fatal).toHaveBeenCalledTimes(1)
})
test('createLockPath creates the same lockPath for identical locks', () => {
const path1 = createLockPath(lockConfig)
const path2 = createLockPath(Object.assign({}, lockConfig))
expect(path1).toBe(path2)
})
test('createLockPath creates unique lockPaths for different ids', () => {
const path1 = createLockPath(lockConfig)
const path2 = createLockPath(Object.assign({}, lockConfig, { id: 'id2' }))
expect(path1).not.toBe(path2)
})
test('createLockPath creates unique lockPaths for different dirs', () => {
const path1 = createLockPath(lockConfig)
const path2 = createLockPath(Object.assign({}, lockConfig, { dir: 'dir2' }))
expect(path1).not.toBe(path2)
})
test('createLockPath creates unique lockPaths for different roots', () => {
const path1 = createLockPath(lockConfig)
const path2 = createLockPath(Object.assign({}, lockConfig, { root: '/project-root2' }))
expect(path1).not.toBe(path2)
})
test('getLockPath creates lockPath when it doesnt exists', () => {
getLockPath(lockConfig)
expect(fs.ensureDir).toHaveBeenCalledTimes(1)
})
test('lock creates a lock and returns a release fn', async () => {
properlock.lock.mockReturnValue(true)
const fn = await lock(lockConfig)
expect(properlock.check).toHaveBeenCalledTimes(1)
expect(properlock.lock).toHaveBeenCalledTimes(1)
expect(fs.ensureDir).toHaveBeenCalledTimes(1)
expect(fn).toEqual(expect.any(Function))
expect(consola.error).not.toHaveBeenCalled()
expect(consola.fatal).not.toHaveBeenCalled()
expect(consola.warn).not.toHaveBeenCalled()
})
test('lock throws error when lock already exists', async () => {
properlock.check.mockReturnValue(true)
await lock(lockConfig)
expect(properlock.check).toHaveBeenCalledTimes(1)
expect(consola.fatal).toHaveBeenCalledTimes(1)
expect(consola.fatal).toHaveBeenCalledWith(`A lock with id '${lockConfig.id}' already exists on ${lockConfig.dir}`)
})
test('lock logs warning when it couldnt get a lock', async () => {
properlock.lock.mockReturnValue(false)
const fn = await lock(lockConfig)
expect(fn).toBe(false)
expect(properlock.lock).toHaveBeenCalledTimes(1)
expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.warn).toHaveBeenCalledWith(`Unable to get a lock with id '${lockConfig.id}' on ${lockConfig.dir} (but will continue)`)
})
test('lock logs warning when proper.lock threw error', async () => {
properlock.lock.mockImplementation(() => {
throw new Error('test error')
})
await lock(lockConfig)
expect(properlock.lock).toHaveBeenCalledTimes(1)
expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.warn).toHaveBeenCalledWith(`Unable to get a lock with id '${lockConfig.id}' on ${lockConfig.dir} (but will continue)`)
})
test('lock returns a release method for unlocking both lockfile as lockPath', async () => {
const release = jest.fn()
properlock.lock.mockImplementation(() => release)
const fn = await lock(lockConfig)
await fn()
expect(release).toHaveBeenCalledTimes(1)
expect(fs.remove).toHaveBeenCalledTimes(1)
})
test('lock release also cleanup onExit set', async () => {
const release = jest.fn()
properlock.lock.mockImplementation(() => release)
const fn = await lock(lockConfig)
expect(lockPaths.size).toBe(1)
await fn()
expect(lockPaths.size).toBe(0)
})
test('lock release only logs error when error thrown', async () => {
const release = jest.fn(() => {
throw new Error('test error')
})
properlock.lock.mockImplementation(() => release)
const fn = await lock(lockConfig)
await expect(fn()).resolves.not.toThrow()
expect(consola.debug).toHaveBeenCalledTimes(1)
})
test('lock check only logs error when error thrown', async () => {
const testError = new Error('check error')
properlock.lock.mockImplementation(() => () => {})
properlock.check.mockImplementation(() => {
throw testError
})
const fn = await lock(lockConfig)
expect(fn).toEqual(expect.any(Function))
expect(consola.debug).toHaveBeenCalledTimes(1)
expect(consola.debug).toHaveBeenCalledWith(`Check for an existing lock with id '${lockConfig.id}' on ${lockConfig.dir} failed`, testError)
})
test('lock release doesnt log error when error thrown because lock compromised', async () => {
fs.exists.mockReturnValue(true)
const testError = new Error('Lock is already released')
const release = jest.fn(() => {
throw testError
})
properlock.lock.mockImplementation((path, options) => {
options.onCompromised()
return release
})
const fn = await lock({
...lockConfig,
options: {
// overwrite default compromised which calls consola.warn
onCompromised () {}
}
})
await expect(fn()).resolves.not.toThrow()
expect(consola.warn).not.toHaveBeenCalled()
})
test('lock sets exit listener once to remove lockPaths', async () => {
properlock.lock.mockReturnValue(true)
await lock(lockConfig)
await lock(lockConfig)
expect(onExit).toHaveBeenCalledTimes(1)
})
test('exit listener removes all lockPaths when called', async () => {
properlock.lock.mockReturnValue(true)
let callback
onExit.mockImplementation(cb => (callback = cb))
const lockConfig2 = Object.assign({}, lockConfig, { id: 'id2' })
const path1 = createLockPath(lockConfig)
const path2 = createLockPath(lockConfig2)
await lock(lockConfig)
await lock(lockConfig2)
expect(onExit).toHaveBeenCalledTimes(1)
expect(lockPaths.size).toBe(2)
expect(callback).toBeDefined()
callback()
expect(fs.removeSync).toHaveBeenCalledWith(path1)
expect(fs.removeSync).toHaveBeenCalledWith(path2)
})
test('lock uses setLockOptions to set defaults', async () => {
const spy = properlock.lock.mockReturnValue(true)
await lock(lockConfig)
expect(spy).toHaveBeenCalledWith(expect.any(String), expect.any(Object))
const options = spy.mock.calls[0][1]
expect(options.stale).toBeDefined()
expect(options.onCompromised).toBeDefined()
expect(() => options.onCompromised()).not.toThrow()
expect(consola.fatal).not.toHaveBeenCalled()
})
})