Skip to content

Commit 42f4b4c

Browse files
author
easonLiangWorldedtech
committed
test(utils): carry the staging-path check into the lock-key ordering (U2, issue 1375)
The test double for lstat now also answers the file-type question safeWriteText asks, and the recorded order gains the staging-path lstat that runs after the lock is taken.
1 parent 6d63c2d commit 42f4b4c

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎src/utils/__tests__/safeWriteJson.lockKey.spec.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ afterEach(async () => {
5050
// Only isSymbolicLink() is consulted by the guard, so the double carries just
5151
// that method. The mocks reject asynchronously: a synchronous throw would bypass
5252
// resolvePublishTarget's catch and skip the ENOENT/symlink branch under test.
53-
const symlinkStat = (target: unknown) => ({ isSymbolicLink: () => target === currentLink }) as unknown as BigIntStats
53+
const symlinkStat = (target: unknown) => ({
54+
isSymbolicLink: () => target === currentLink,
55+
// The staging-path check in safeWriteText also asks whether the path is a
56+
// regular file, so the double carries that predicate as well.
57+
isFile: () => target !== currentLink,
58+
}) as unknown as BigIntStats
5459
let currentLink = ""
5560

5661
describe("safeWriteJson lock key under a peer commit", () => {
@@ -91,7 +96,10 @@ describe("safeWriteJson lock key under a peer commit", () => {
9196
// The lock key is the key every other writer to this file uses, so the caller
9297
// queued behind the peer instead of failing before the lock.
9398
expect(mockedAcquireFileLock).toHaveBeenCalledWith(referent)
94-
expect(order).toEqual(["resolve-failed", "lstat", "resolve", "resolve", "lock", "resolve", "resolve"])
99+
// The trailing lstat is safeWriteText's staging-path check on the temp file
100+
// this write created: it runs after the key was resolved and the lock taken,
101+
// so it does not change which lock the caller queued behind.
102+
expect(order).toEqual(["resolve-failed", "lstat", "resolve", "resolve", "lock", "resolve", "resolve", "lstat"])
95103
expect(JSON.parse(await fs.readFile(referent, "utf8"))).toEqual({ id: "task-1" })
96104
})
97105

@@ -144,7 +152,7 @@ describe("safeWriteJson lock key under a peer commit", () => {
144152
if (target === file) throw enoent
145153
return canonicalDir
146154
})
147-
mockedLstat.mockImplementation(async () => ({ isSymbolicLink: () => false }) as unknown as BigIntStats)
155+
mockedLstat.mockImplementation(async () => ({ isSymbolicLink: () => false, isFile: () => true }) as unknown as BigIntStats)
148156

149157
expect(await resolveLockKey(file)).toBe(path.join(canonicalDir, "history_item.json"))
150158
})

0 commit comments

Comments
 (0)