fix: apply default lock options before setting lock (#5320)

This commit is contained in:
Pim 2019-03-21 21:07:50 +01:00 committed by Pooya Parsa
parent 3153e89645
commit 7c24280a7a
2 changed files with 17 additions and 3 deletions

View File

@ -40,6 +40,7 @@ export async function lock({ id, dir, root, options }) {
consola.fatal(`A lock with id '${id}' already exists on ${dir}`)
}
options = getLockOptions(options)
const release = await properlock.lock(lockPath, options)
if (!release) {

View File

@ -18,16 +18,16 @@ describe('util: locking', () => {
beforeEach(() => jest.resetAllMocks())
beforeEach(() => lockPaths.clear())
test('onCompromised lock is warn error by default', () => {
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.warn(err) })
const options = getLockOptions({ onCompromised: err => consola.fatal(err) })
options.onCompromised()
expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.fatal).toHaveBeenCalledTimes(1)
})
test('createLockPath creates the same lockPath for identical locks', () => {
@ -143,4 +143,17 @@ describe('util: locking', () => {
expect(fs.removeSync).toHaveBeenCalledWith(path1)
expect(fs.removeSync).toHaveBeenCalledWith(path2)
})
test('lock uses setLockOptions to set defaults', async () => {
const spy = properlock.lock.mockImplementationOnce(() => 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()
})
})