-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathenhanced-path.test.ts
More file actions
128 lines (101 loc) · 3.55 KB
/
enhanced-path.test.ts
File metadata and controls
128 lines (101 loc) · 3.55 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest'
import { platform } from 'node:os'
import { createEnhancedPath } from '../enhanced-path'
vi.mock('node:os', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:os')>()
const platformMock = vi.fn(actual.platform)
return {
...actual,
platform: platformMock,
default: {
...actual,
platform: platformMock,
},
}
})
const mockPlatform = vi.mocked(platform)
describe('createEnhancedPath', () => {
beforeEach(() => {
mockPlatform.mockReset()
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('prepends macOS container-tooling paths on darwin', () => {
mockPlatform.mockReturnValue('darwin')
vi.stubEnv('PATH', '/usr/bin:/bin')
vi.stubEnv('HOME', '/Users/test')
const result = createEnhancedPath()
const entries = result.split(':')
expect(entries.slice(0, 4)).toEqual([
'/Applications/Docker.app/Contents/Resources/bin',
'/opt/homebrew/bin',
'/usr/local/bin',
'/Users/test/.rd/bin',
])
expect(entries).toContain('/usr/bin')
expect(entries).toContain('/bin')
})
it('expands ~ using HOME', () => {
mockPlatform.mockReturnValue('darwin')
vi.stubEnv('PATH', '')
vi.stubEnv('HOME', '/Users/alice')
const result = createEnhancedPath()
expect(result).toContain('/Users/alice/.rd/bin')
expect(result).not.toContain('~')
})
it('keeps ~ verbatim when HOME and USERPROFILE are both unset', () => {
mockPlatform.mockReturnValue('darwin')
vi.stubEnv('PATH', '')
vi.stubEnv('HOME', '')
vi.stubEnv('USERPROFILE', '')
const result = createEnhancedPath()
const entries = result.split(':')
expect(entries).toContain('~/.rd/bin')
expect(entries).not.toContain('/.rd/bin')
})
it('uses semicolon as separator on win32', () => {
mockPlatform.mockReturnValue('win32')
vi.stubEnv('PATH', 'C:\\Windows\\System32;C:\\Windows')
const result = createEnhancedPath()
const entries = result.split(';')
expect(entries[0]).toBe('C:\\Program Files\\Docker\\Docker\\resources\\bin')
expect(entries[1]).toBe('C:\\Program Files\\RedHat\\Podman')
expect(entries).toContain('C:\\Windows\\System32')
expect(entries).toContain('C:\\Windows')
})
it('prepends linux container-tooling paths', () => {
mockPlatform.mockReturnValue('linux')
vi.stubEnv('PATH', '/usr/bin:/bin')
vi.stubEnv('HOME', '/home/test')
const result = createEnhancedPath()
const entries = result.split(':')
expect(entries.slice(0, 4)).toEqual([
'/usr/local/bin',
'/opt/homebrew/bin',
'/snap/bin',
'/home/test/.rd/bin',
])
})
it('preserves existing PATH entries after the prepended paths', () => {
mockPlatform.mockReturnValue('darwin')
vi.stubEnv('PATH', '/existing/bin:/another/bin')
vi.stubEnv('HOME', '/Users/test')
const result = createEnhancedPath()
const entries = result.split(':')
const existingIndex = entries.indexOf('/existing/bin')
const anotherIndex = entries.indexOf('/another/bin')
expect(existingIndex).toBeGreaterThan(-1)
expect(anotherIndex).toBeGreaterThan(existingIndex)
})
it('handles empty PATH gracefully', () => {
mockPlatform.mockReturnValue('darwin')
vi.stubEnv('PATH', '')
vi.stubEnv('HOME', '/Users/test')
const result = createEnhancedPath()
expect(result).not.toBe('')
expect(result).toContain('/usr/local/bin')
// No trailing empty segments from the split/join
expect(result.endsWith(':')).toBe(false)
})
})