-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpackage-environment.npm-version.test.mts
More file actions
107 lines (90 loc) · 3.18 KB
/
package-environment.npm-version.test.mts
File metadata and controls
107 lines (90 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { describe, expect, it, vi } from 'vitest'
const spawnMock = vi.fn(async () => ({ stdout: '11.6.0' }))
const resolveBinPathSyncMock = vi.fn(() => '/fake/npm-cli.js')
const whichBinMock = vi.fn(async () => 'npm')
vi.mock('@socketsecurity/registry/lib/spawn', () => ({
spawn: spawnMock,
}))
vi.mock('@socketsecurity/registry/lib/bin', () => ({
resolveBinPathSync: resolveBinPathSyncMock,
whichBin: whichBinMock,
}))
vi.mock('../src/utils/fs.mts', () => ({
findUp: vi.fn(async () => undefined),
}))
// Mock constants to simulate Windows platform for these tests.
// These tests specifically verify Windows-specific npm version detection behavior.
vi.mock('../src/constants.mts', async importOriginal => {
const actual = (await importOriginal()) as unknown
return {
...actual,
default: {
...actual.default,
WIN32: true,
},
}
})
describe('detectPackageEnvironment - Windows npm version detection', () => {
it('detects npm version when resolved to JS entrypoint', async () => {
spawnMock.mockClear()
resolveBinPathSyncMock.mockClear()
whichBinMock.mockClear()
resolveBinPathSyncMock.mockReturnValue('/fake/npm-cli.js')
spawnMock.mockResolvedValue({ stdout: '11.6.0' })
const { detectPackageEnvironment } = await import(
'../src/utils/package-environment.mts'
)
const details = await detectPackageEnvironment({ cwd: process.cwd() })
expect(details.agent).toBe('npm')
expect(details.agentVersion?.major).toBe(11)
expect(spawnMock).toHaveBeenCalledWith(
expect.any(String),
expect.arrayContaining(['/fake/npm-cli.js', '--version']),
expect.objectContaining({ cwd: process.cwd() }),
)
})
it('falls back to direct spawn when resolveBinPathSync fails', async () => {
spawnMock.mockClear()
resolveBinPathSyncMock.mockClear()
whichBinMock.mockClear()
resolveBinPathSyncMock.mockImplementation(() => {
throw new Error('Resolution failed')
})
spawnMock.mockResolvedValue({ stdout: '10.5.0' })
const { detectPackageEnvironment } = await import(
'../src/utils/package-environment.mts'
)
const details = await detectPackageEnvironment({ cwd: process.cwd() })
expect(details.agent).toBe('npm')
expect(details.agentVersion?.major).toBe(10)
expect(spawnMock).toHaveBeenCalledWith(
expect.any(String),
['--version'],
expect.objectContaining({
cwd: process.cwd(),
shell: true,
}),
)
})
it('uses direct spawn when resolved to non-JS executable', async () => {
spawnMock.mockClear()
resolveBinPathSyncMock.mockClear()
whichBinMock.mockClear()
resolveBinPathSyncMock.mockReturnValue('/fake/npm.cmd')
spawnMock.mockResolvedValue({ stdout: '9.8.1' })
const { detectPackageEnvironment } = await import(
'../src/utils/package-environment.mts'
)
const details = await detectPackageEnvironment({ cwd: process.cwd() })
expect(details.agent).toBe('npm')
expect(details.agentVersion?.major).toBe(9)
expect(spawnMock).toHaveBeenCalledWith(
expect.any(String),
['--version'],
expect.objectContaining({
cwd: process.cwd(),
shell: true,
}),
)
})
})