From 797e9586a61eeac37c0d7bf3522017c4c9b1a3c2 Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Thu, 15 Feb 2024 12:24:49 +0100 Subject: [PATCH] chore: fix test --- .../src/__mocks__/proper-lockfile.ts | 33 +++++++++++++++++++ .../src/__tests__/basic.spec.ts | 1 + .../src/__tests__/issues.spec.ts | 1 + 3 files changed, 35 insertions(+) create mode 100644 tests/internal-tests/src/__mocks__/proper-lockfile.ts diff --git a/tests/internal-tests/src/__mocks__/proper-lockfile.ts b/tests/internal-tests/src/__mocks__/proper-lockfile.ts new file mode 100644 index 00000000..9394f285 --- /dev/null +++ b/tests/internal-tests/src/__mocks__/proper-lockfile.ts @@ -0,0 +1,33 @@ +const locks = new Set() +export async function lock( + filePath: string, + _options: { + onCompromised: (err: Error) => void + } +): Promise<() => Promise> { + await sleep(1) + + if (locks.has(filePath)) { + const err = new Error('ELOCKED: File is already locked') + ;(err as any).code = 'ELOCKED' + throw err + } else { + locks.add(filePath) + } + + return async () => { + // release lock + + if (!locks.has(filePath)) { + const err = new Error('ELOCKED: File is already released') + ;(err as any).code = 'ERELEASED' + throw err + } else { + locks.delete(filePath) + } + } +} + +async function sleep(duration: number): Promise { + return new Promise((r) => setTimeout(r, duration)) +} diff --git a/tests/internal-tests/src/__tests__/basic.spec.ts b/tests/internal-tests/src/__tests__/basic.spec.ts index aebf9729..eb995fe0 100644 --- a/tests/internal-tests/src/__tests__/basic.spec.ts +++ b/tests/internal-tests/src/__tests__/basic.spec.ts @@ -24,6 +24,7 @@ jest.mock('child_process') jest.mock('windows-network-drive') jest.mock('tv-automation-quantel-gateway-client') jest.mock('@parcel/watcher') +jest.mock('proper-lockfile') const fs = fsOrg as any as typeof fsMockType const WND = WNDOrg as any as typeof WNDType diff --git a/tests/internal-tests/src/__tests__/issues.spec.ts b/tests/internal-tests/src/__tests__/issues.spec.ts index 369dc554..fc60735b 100644 --- a/tests/internal-tests/src/__tests__/issues.spec.ts +++ b/tests/internal-tests/src/__tests__/issues.spec.ts @@ -14,6 +14,7 @@ jest.mock('child_process') jest.mock('windows-network-drive') jest.mock('tv-automation-quantel-gateway-client') jest.mock('@parcel/watcher') +jest.mock('proper-lockfile') const fs = fsOrg as any as typeof fsMockType