|
| 1 | +import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest' |
| 2 | +import { platform } from 'node:os' |
| 3 | + |
| 4 | +import { createEnhancedPath } from '../enhanced-path' |
| 5 | + |
| 6 | +vi.mock('node:os', async (importOriginal) => { |
| 7 | + const actual = await importOriginal<typeof import('node:os')>() |
| 8 | + const platformMock = vi.fn(actual.platform) |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + platform: platformMock, |
| 12 | + default: { |
| 13 | + ...actual, |
| 14 | + platform: platformMock, |
| 15 | + }, |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +const mockPlatform = vi.mocked(platform) |
| 20 | + |
| 21 | +describe('createEnhancedPath', () => { |
| 22 | + beforeEach(() => { |
| 23 | + mockPlatform.mockReset() |
| 24 | + }) |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + vi.unstubAllEnvs() |
| 28 | + }) |
| 29 | + |
| 30 | + it('prepends macOS container-tooling paths on darwin', () => { |
| 31 | + mockPlatform.mockReturnValue('darwin') |
| 32 | + vi.stubEnv('PATH', '/usr/bin:/bin') |
| 33 | + vi.stubEnv('HOME', '/Users/test') |
| 34 | + |
| 35 | + const result = createEnhancedPath() |
| 36 | + const entries = result.split(':') |
| 37 | + |
| 38 | + expect(entries.slice(0, 4)).toEqual([ |
| 39 | + '/Applications/Docker.app/Contents/Resources/bin', |
| 40 | + '/opt/homebrew/bin', |
| 41 | + '/usr/local/bin', |
| 42 | + '/Users/test/.rd/bin', |
| 43 | + ]) |
| 44 | + expect(entries).toContain('/usr/bin') |
| 45 | + expect(entries).toContain('/bin') |
| 46 | + }) |
| 47 | + |
| 48 | + it('expands ~ using HOME', () => { |
| 49 | + mockPlatform.mockReturnValue('darwin') |
| 50 | + vi.stubEnv('PATH', '') |
| 51 | + vi.stubEnv('HOME', '/Users/alice') |
| 52 | + |
| 53 | + const result = createEnhancedPath() |
| 54 | + |
| 55 | + expect(result).toContain('/Users/alice/.rd/bin') |
| 56 | + expect(result).not.toContain('~') |
| 57 | + }) |
| 58 | + |
| 59 | + it('keeps ~ verbatim when HOME and USERPROFILE are both unset', () => { |
| 60 | + mockPlatform.mockReturnValue('darwin') |
| 61 | + vi.stubEnv('PATH', '') |
| 62 | + vi.stubEnv('HOME', '') |
| 63 | + vi.stubEnv('USERPROFILE', '') |
| 64 | + |
| 65 | + const result = createEnhancedPath() |
| 66 | + const entries = result.split(':') |
| 67 | + |
| 68 | + expect(entries).toContain('~/.rd/bin') |
| 69 | + expect(entries).not.toContain('/.rd/bin') |
| 70 | + }) |
| 71 | + |
| 72 | + it('uses semicolon as separator on win32', () => { |
| 73 | + mockPlatform.mockReturnValue('win32') |
| 74 | + vi.stubEnv('PATH', 'C:\\Windows\\System32;C:\\Windows') |
| 75 | + |
| 76 | + const result = createEnhancedPath() |
| 77 | + const entries = result.split(';') |
| 78 | + |
| 79 | + expect(entries[0]).toBe('C:\\Program Files\\Docker\\Docker\\resources\\bin') |
| 80 | + expect(entries[1]).toBe('C:\\Program Files\\RedHat\\Podman') |
| 81 | + expect(entries).toContain('C:\\Windows\\System32') |
| 82 | + expect(entries).toContain('C:\\Windows') |
| 83 | + }) |
| 84 | + |
| 85 | + it('prepends linux container-tooling paths', () => { |
| 86 | + mockPlatform.mockReturnValue('linux') |
| 87 | + vi.stubEnv('PATH', '/usr/bin:/bin') |
| 88 | + vi.stubEnv('HOME', '/home/test') |
| 89 | + |
| 90 | + const result = createEnhancedPath() |
| 91 | + const entries = result.split(':') |
| 92 | + |
| 93 | + expect(entries.slice(0, 4)).toEqual([ |
| 94 | + '/usr/local/bin', |
| 95 | + '/opt/homebrew/bin', |
| 96 | + '/snap/bin', |
| 97 | + '/home/test/.rd/bin', |
| 98 | + ]) |
| 99 | + }) |
| 100 | + |
| 101 | + it('preserves existing PATH entries after the prepended paths', () => { |
| 102 | + mockPlatform.mockReturnValue('darwin') |
| 103 | + vi.stubEnv('PATH', '/existing/bin:/another/bin') |
| 104 | + vi.stubEnv('HOME', '/Users/test') |
| 105 | + |
| 106 | + const result = createEnhancedPath() |
| 107 | + const entries = result.split(':') |
| 108 | + |
| 109 | + const existingIndex = entries.indexOf('/existing/bin') |
| 110 | + const anotherIndex = entries.indexOf('/another/bin') |
| 111 | + |
| 112 | + expect(existingIndex).toBeGreaterThan(-1) |
| 113 | + expect(anotherIndex).toBeGreaterThan(existingIndex) |
| 114 | + }) |
| 115 | + |
| 116 | + it('handles empty PATH gracefully', () => { |
| 117 | + mockPlatform.mockReturnValue('darwin') |
| 118 | + vi.stubEnv('PATH', '') |
| 119 | + vi.stubEnv('HOME', '/Users/test') |
| 120 | + |
| 121 | + const result = createEnhancedPath() |
| 122 | + |
| 123 | + expect(result).not.toBe('') |
| 124 | + expect(result).toContain('/usr/local/bin') |
| 125 | + // No trailing empty segments from the split/join |
| 126 | + expect(result.endsWith(':')).toBe(false) |
| 127 | + }) |
| 128 | +}) |
0 commit comments