Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply default lock options before setting lock #5320

Merged
merged 1 commit into from Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/utils/src/locking.js
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
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()
})
})