Skip to content

Commit 496017d

Browse files
committed
fix(test/releases-github): use mkdtemp to avoid cache-hit races
Two TOCTOU-protection tests in `downloadGitHubRelease` named their scratch dir `test-github-dl-${Date.now()}`. Millisecond precision isn't enough isolation: a prior run's cached `.version` + binary left behind in `/tmp/test-github-dl-<prev>` could, under parallel worker scheduling or same-ms retry, land at the same path as the next run's `testDownloadDir`. The first `downloadGitHubRelease()` call then hit the cache-hit branch (`existsSync(versionPath) && existsSync(binary)`) instead of invoking `httpDownload`, causing the `toHaveBeenCalledTimes(1)` assertion to fail with 0. Switches both tests to `fs.mkdtemp`, which gives OS-guaranteed unique paths.
1 parent fd973e8 commit 496017d

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

test/unit/releases-github.test.mts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,15 @@ describe('releases/github', () => {
842842
const { promises: fs } = await import('node:fs')
843843
const path = await import('node:path')
844844

845-
const tmpDir = tmpdir()
846-
const testDownloadDir = path.join(tmpDir, `test-github-dl-${Date.now()}`)
845+
// mkdtemp gives an OS-guaranteed unique dir. Previous `Date.now()`
846+
// naming collided across test runs on the same millisecond and
847+
// across parallel vitest workers, occasionally letting a prior
848+
// run's cached .version + binary satisfy the cache-hit branch
849+
// inside downloadGitHubRelease — so `httpDownload` was called 0
850+
// times and the assertion below saw the wrong count.
851+
const testDownloadDir = await fs.mkdtemp(
852+
path.join(tmpdir(), 'test-github-dl-'),
853+
)
847854

848855
try {
849856
// Mock successful HTTP download
@@ -940,10 +947,10 @@ describe('releases/github', () => {
940947
const { promises: fs } = await import('node:fs')
941948
const path = await import('node:path')
942949

943-
const tmpDir = tmpdir()
944-
const testDownloadDir = path.join(
945-
tmpDir,
946-
`test-github-dl-missing-${Date.now()}`,
950+
// mkdtemp avoids collisions with prior test runs' leftover cache
951+
// dirs. See note on the sibling test above.
952+
const testDownloadDir = await fs.mkdtemp(
953+
path.join(tmpdir(), 'test-github-dl-missing-'),
947954
)
948955

949956
try {

0 commit comments

Comments
 (0)