Skip to content

Commit

Permalink
fix: apply default lock options before setting lock (#5320)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie authored and pi0 committed Mar 21, 2019
1 parent 3153e89 commit 7c24280
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/utils/src/locking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 16 additions & 3 deletions packages/utils/test/locking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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()
})
})

0 comments on commit 7c24280

Please sign in to comment.