Skip to content

Commit 1f46b22

Browse files
committed
fix(test): stabilize flaky Windows dlx binary tests
Consolidate three near-identical Windows script extension tests (.cmd, .bat, .ps1) into a single loop. Race the spawnPromise with a 5s timeout to prevent hangs when shell execution stalls on CI (e.g. PowerShell execution policy prompts causing the .ps1 test to exceed vitest's 10s default timeout).
1 parent 1f26a9f commit 1f46b22

1 file changed

Lines changed: 48 additions & 96 deletions

File tree

test/unit/dlx/binary.test.mts

Lines changed: 48 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,102 +1190,54 @@ describe.sequential('dlx-binary', () => {
11901190
describe('Windows-specific behavior', () => {
11911191
const originalPlatform = process.platform
11921192

1193-
it.skipIf(process.platform !== 'win32')(
1194-
'should handle .cmd files with shell on Windows',
1195-
async () => {
1196-
await runWithTempDir(async tmpDir => {
1197-
const restoreHome = mockHomeDir(tmpDir)
1198-
1199-
try {
1200-
// Mock Windows platform
1201-
Object.defineProperty(process, 'platform', {
1202-
configurable: true,
1203-
value: 'win32',
1204-
})
1205-
1206-
const url = `${httpBaseUrl}/binary-windows.cmd`
1207-
1208-
const result = await dlxBinary(['--version'], {
1209-
name: 'test.cmd',
1210-
url,
1211-
})
1212-
1213-
expect(result.binaryPath).toContain('.cmd')
1214-
await result.spawnPromise.catch(() => {})
1215-
} finally {
1216-
restoreHome()
1217-
Object.defineProperty(process, 'platform', {
1218-
configurable: true,
1219-
value: originalPlatform,
1220-
})
1221-
}
1222-
}, 'dlxBinary-windows-cmd-')
1223-
},
1224-
)
1225-
1226-
it.skipIf(process.platform !== 'win32')(
1227-
'should handle .bat files with shell on Windows',
1228-
async () => {
1229-
await runWithTempDir(async tmpDir => {
1230-
const restoreHome = mockHomeDir(tmpDir)
1231-
1232-
try {
1233-
Object.defineProperty(process, 'platform', {
1234-
configurable: true,
1235-
value: 'win32',
1236-
})
1237-
1238-
const url = `${httpBaseUrl}/binary-windows.bat`
1239-
1240-
const result = await dlxBinary(['--version'], {
1241-
name: 'test.bat',
1242-
url,
1243-
})
1244-
1245-
expect(result.binaryPath).toContain('.bat')
1246-
await result.spawnPromise.catch(() => {})
1247-
} finally {
1248-
restoreHome()
1249-
Object.defineProperty(process, 'platform', {
1250-
configurable: true,
1251-
value: originalPlatform,
1252-
})
1253-
}
1254-
}, 'dlxBinary-windows-bat-')
1255-
},
1256-
)
1257-
1258-
it.skipIf(process.platform !== 'win32')(
1259-
'should handle .ps1 files with shell on Windows',
1260-
async () => {
1261-
await runWithTempDir(async tmpDir => {
1262-
const restoreHome = mockHomeDir(tmpDir)
1263-
1264-
try {
1265-
Object.defineProperty(process, 'platform', {
1266-
configurable: true,
1267-
value: 'win32',
1268-
})
1269-
1270-
const url = `${httpBaseUrl}/binary-windows.ps1`
1271-
1272-
const result = await dlxBinary(['--version'], {
1273-
name: 'test.ps1',
1274-
url,
1275-
})
1276-
1277-
expect(result.binaryPath).toContain('.ps1')
1278-
await result.spawnPromise.catch(() => {})
1279-
} finally {
1280-
restoreHome()
1281-
Object.defineProperty(process, 'platform', {
1282-
configurable: true,
1283-
value: originalPlatform,
1284-
})
1285-
}
1286-
}, 'dlxBinary-windows-ps1-')
1287-
},
1288-
)
1193+
// Windows script extensions that require shell: true in spawn.
1194+
// Each test verifies dlxBinary downloads the script and resolves
1195+
// the correct binaryPath. The spawnPromise is raced with a timeout
1196+
// to avoid hanging on CI when shell execution stalls (e.g. ps1
1197+
// execution policy prompts).
1198+
const windowsScriptExts = ['.cmd', '.bat', '.ps1'] as const
1199+
1200+
for (const ext of windowsScriptExts) {
1201+
it.skipIf(process.platform !== 'win32')(
1202+
`should handle ${ext} files with shell on Windows`,
1203+
async () => {
1204+
await runWithTempDir(
1205+
async tmpDir => {
1206+
const restoreHome = mockHomeDir(tmpDir)
1207+
1208+
try {
1209+
Object.defineProperty(process, 'platform', {
1210+
configurable: true,
1211+
value: 'win32',
1212+
})
1213+
1214+
const url = `${httpBaseUrl}/binary-windows${ext}`
1215+
1216+
const result = await dlxBinary(['--version'], {
1217+
name: `test${ext}`,
1218+
url,
1219+
})
1220+
1221+
expect(result.binaryPath).toContain(ext)
1222+
// Race the spawn with a 5s timeout to prevent hangs on CI
1223+
// (e.g. PowerShell execution policy prompts).
1224+
await Promise.race([
1225+
result.spawnPromise.catch(() => {}),
1226+
new Promise(resolve => setTimeout(resolve, 5_000)),
1227+
])
1228+
} finally {
1229+
restoreHome()
1230+
Object.defineProperty(process, 'platform', {
1231+
configurable: true,
1232+
value: originalPlatform,
1233+
})
1234+
}
1235+
},
1236+
`dlxBinary-windows-${ext.slice(1)}-`,
1237+
)
1238+
},
1239+
)
1240+
}
12891241
})
12901242

12911243
describe('edge cases', () => {

0 commit comments

Comments
 (0)