Skip to content

Commit 685ddda

Browse files
test(utils): pin the leaf substitution on a stubbed volume spelling, so it is proved where no volume would hand back two spellings
1 parent 7feb8a1 commit 685ddda

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎src/utils/__tests__/fileLock.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,45 @@ describe("one lock per file, not one lock per spelling", () => {
182182
expect(canonicalLockKey(aliasFile)).toBe(canonicalLockKey(file))
183183
await releaseAlias()
184184
})
185+
186+
// The portable half of the leaf-spelling coverage. The 8.3 row above needs a volume that hands
187+
// back two spellings of one entry and the real-library link rows below need a filesystem that
188+
// lets a leaf be a link; this one needs neither. It substitutes the spelling the volume reports
189+
// and asserts the artefact acquireFileLock asks the library to create, so dropping the
190+
// substitution fails here on every platform, not only where a second spelling exists by luck.
191+
it("names the artefact by the spelling the volume reports for the leaf, not by the caller's", async () => {
192+
const directory = path.join(root(), "substituted-leaf-lock")
193+
fs.mkdirSync(directory, { recursive: true })
194+
const callerPath = path.join(directory, "history-item.json")
195+
const volumePath = path.join(directory, "HISTORY-ITEM.JSON")
196+
fs.writeFileSync(callerPath, "{}", "utf8")
197+
// fs's namespace types these properties read-only, and the module double forwards to the real
198+
// realpathSync object, so the substitution is installed by reading the callable through a
199+
// mutable view of it and restoring the original function in the finally below.
200+
const realpathModule: { native: (target: string) => string } = fs.realpathSync
201+
const actualNative = realpathModule.native
202+
try {
203+
realpathModule.native = (target: string) =>
204+
target === path.resolve(callerPath) ? volumePath : actualNative(target)
205+
const release = await acquireFileLock(callerPath, { immediate: true })
206+
try {
207+
const call = vi.mocked(lockfile.lock).mock.calls.at(-1)
208+
expect(call?.[1]?.lockfilePath).toBe(path.join(directory, "HISTORY-ITEM.JSON.lock"))
209+
expect(String(call?.[0])).toBe(canonicalLockKey(path.resolve(callerPath)))
210+
expect(fs.existsSync(path.join(directory, "HISTORY-ITEM.JSON.lock"))).toBe(false)
211+
expect(fs.existsSync(path.join(directory, "history-item.json.lock"))).toBe(false)
212+
} finally {
213+
await release()
214+
}
215+
} finally {
216+
realpathModule.native = actualNative
217+
}
218+
const after = await acquireFileLock(callerPath, { immediate: true })
219+
await after()
220+
expect(vi.mocked(lockfile.lock).mock.calls.at(-1)?.[1]?.lockfilePath).toBe(
221+
path.join(directory, "history-item.json.lock"),
222+
)
223+
})
185224
itAlias("hands each caller back its own spelling rather than the canonicalised one", async () => {
186225
const longDir = path.join(root(), "a-really-long-directory-name-for-spelling-control")
187226
fs.mkdirSync(longDir, { recursive: true })

0 commit comments

Comments
 (0)