|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest' |
| 4 | +import { detectShellFamily, printEnvironmentCommand } from './shellCommand' |
| 5 | + |
| 6 | +const POWERSHELL_COMMAND = |
| 7 | + "Get-ChildItem Env: | Sort-Object Name | ForEach-Object { '{0}={1}' -f $_.Name, $_.Value }" |
| 8 | + |
| 9 | +describe('detectShellFamily', () => { |
| 10 | + it('should detect fish', () => { |
| 11 | + expect(detectShellFamily('/usr/bin/fish', false)).toBe('fish') |
| 12 | + expect(detectShellFamily('/opt/homebrew/bin/fish', false)).toBe('fish') |
| 13 | + }) |
| 14 | + |
| 15 | + it('should detect POSIX shells', () => { |
| 16 | + expect(detectShellFamily('/bin/bash', false)).toBe('posix') |
| 17 | + expect(detectShellFamily('/bin/zsh', false)).toBe('posix') |
| 18 | + expect(detectShellFamily('/bin/sh', false)).toBe('posix') |
| 19 | + expect(detectShellFamily('/usr/bin/dash', false)).toBe('posix') |
| 20 | + expect(detectShellFamily('/usr/bin/ksh', false)).toBe('posix') |
| 21 | + }) |
| 22 | + |
| 23 | + it('should detect Windows shells, ignoring the executable extension', () => { |
| 24 | + expect(detectShellFamily('C:\\Windows\\System32\\cmd.exe', true)).toBe( |
| 25 | + 'cmd', |
| 26 | + ) |
| 27 | + expect( |
| 28 | + detectShellFamily( |
| 29 | + 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', |
| 30 | + true, |
| 31 | + ), |
| 32 | + ).toBe('powershell') |
| 33 | + expect( |
| 34 | + detectShellFamily('C:\\Program Files\\PowerShell\\7\\pwsh.exe', true), |
| 35 | + ).toBe('powershell') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should detect a POSIX shell installed on Windows', () => { |
| 39 | + expect( |
| 40 | + detectShellFamily('C:\\Program Files\\Git\\bin\\bash.exe', true), |
| 41 | + ).toBe('posix') |
| 42 | + }) |
| 43 | + |
| 44 | + it('should accept a bare shell name without a path', () => { |
| 45 | + expect(detectShellFamily('fish', false)).toBe('fish') |
| 46 | + expect(detectShellFamily('pwsh', false)).toBe('powershell') |
| 47 | + }) |
| 48 | + |
| 49 | + it('should ignore the case of the shell name', () => { |
| 50 | + expect(detectShellFamily('/usr/bin/FISH', false)).toBe('fish') |
| 51 | + expect(detectShellFamily('C:\\WINDOWS\\SYSTEM32\\CMD.EXE', true)).toBe( |
| 52 | + 'cmd', |
| 53 | + ) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should fall back on the platform for an unknown shell', () => { |
| 57 | + expect(detectShellFamily('/usr/bin/elvish', false)).toBe('posix') |
| 58 | + expect(detectShellFamily('C:\\tools\\elvish.exe', true)).toBe('cmd') |
| 59 | + }) |
| 60 | + |
| 61 | + it('should fall back on the platform when no shell is reported', () => { |
| 62 | + expect(detectShellFamily('', false)).toBe('posix') |
| 63 | + expect(detectShellFamily('', true)).toBe('cmd') |
| 64 | + expect(detectShellFamily(undefined, false)).toBe('posix') |
| 65 | + expect(detectShellFamily(undefined, true)).toBe('cmd') |
| 66 | + }) |
| 67 | +}) |
| 68 | + |
| 69 | +describe('printEnvironmentCommand', () => { |
| 70 | + it('should use the exported-only set in fish', () => { |
| 71 | + expect(printEnvironmentCommand('/usr/bin/fish', false)).toBe('set -x') |
| 72 | + }) |
| 73 | + |
| 74 | + it('should use env | sort in POSIX shells', () => { |
| 75 | + expect(printEnvironmentCommand('/bin/bash', false)).toBe('env | sort') |
| 76 | + expect(printEnvironmentCommand('/bin/zsh', false)).toBe('env | sort') |
| 77 | + expect(printEnvironmentCommand('/bin/sh', false)).toBe('env | sort') |
| 78 | + }) |
| 79 | + |
| 80 | + it('should use set in cmd', () => { |
| 81 | + expect( |
| 82 | + printEnvironmentCommand('C:\\Windows\\System32\\cmd.exe', true), |
| 83 | + ).toBe('set') |
| 84 | + }) |
| 85 | + |
| 86 | + it('should not use set in PowerShell, where it prompts for a variable name', () => { |
| 87 | + expect( |
| 88 | + printEnvironmentCommand( |
| 89 | + 'C:\\Program Files\\PowerShell\\7\\pwsh.exe', |
| 90 | + true, |
| 91 | + ), |
| 92 | + ).toBe(POWERSHELL_COMMAND) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should use the PowerShell command for pwsh off Windows', () => { |
| 96 | + expect(printEnvironmentCommand('/usr/local/bin/pwsh', false)).toBe( |
| 97 | + POWERSHELL_COMMAND, |
| 98 | + ) |
| 99 | + expect(printEnvironmentCommand('pwsh', false)).toBe(POWERSHELL_COMMAND) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should keep the previous behaviour when the shell is unknown', () => { |
| 103 | + expect(printEnvironmentCommand(undefined, false)).toBe('env | sort') |
| 104 | + expect(printEnvironmentCommand(undefined, true)).toBe('set') |
| 105 | + }) |
| 106 | +}) |
0 commit comments