-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (shell) : Improve shell detection on windows (#3767)
We should detect the usage of $SHELL environment variable when using CRC from linux like environments on Windows. We should also convert the CRC binary paths to unix path format whenever unix shells are detected. Signed-off-by: Rohan Kumar <[email protected]>
- Loading branch information
1 parent
130a608
commit 04f1995
Showing
5 changed files
with
107 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,52 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package shell | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDetectBash(t *testing.T) { | ||
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL")) | ||
os.Setenv("SHELL", "/bin/bash") | ||
|
||
shell, err := detect() | ||
|
||
assert.Equal(t, "bash", shell) | ||
assert.NoError(t, err) | ||
func TestGetPathEnvString(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
userShell string | ||
path string | ||
expectedStr string | ||
}{ | ||
{"fish shell", "fish", "C:\\Users\\foo\\.crc\\bin\\oc", "contains C:\\Users\\foo\\.crc\\bin\\oc $fish_user_paths; or set -U fish_user_paths C:\\Users\\foo\\.crc\\bin\\oc $fish_user_paths"}, | ||
{"powershell shell", "powershell", "C:\\Users\\foo\\oc.exe", "$Env:PATH = \"C:\\Users\\foo\\oc.exe;$Env:PATH\""}, | ||
{"cmd shell", "cmd", "C:\\Users\\foo\\oc.exe", "SET PATH=C:\\Users\\foo\\oc.exe;%PATH%"}, | ||
{"bash with windows path", "bash", "C:\\Users\\foo.exe", "export PATH=\"/C/Users/foo.exe:$PATH\""}, | ||
{"unknown with windows path", "unknown", "C:\\Users\\foo.exe", "export PATH=\"C:\\Users\\foo.exe:$PATH\""}, | ||
{"unknown shell with unix path", "unknown", "/home/foo/.crc/bin/oc", "export PATH=\"/home/foo/.crc/bin/oc:$PATH\""}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := GetPathEnvString(tt.userShell, tt.path) | ||
if result != tt.expectedStr { | ||
t.Errorf("GetPathEnvString(%s, %s) = %s; want %s", tt.userShell, tt.path, result, tt.expectedStr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDetectFish(t *testing.T) { | ||
defer func(shell string) { os.Setenv("SHELL", shell) }(os.Getenv("SHELL")) | ||
os.Setenv("SHELL", "/bin/fish") | ||
|
||
shell, err := detect() | ||
|
||
assert.Equal(t, "fish", shell) | ||
assert.NoError(t, err) | ||
func TestConvertToLinuxStylePath(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
userShell string | ||
path string | ||
expectedPath string | ||
}{ | ||
{"bash on windows, should convert", "bash", "C:\\Users\\foo\\.crc\\bin\\oc", "/C/Users/foo/.crc/bin/oc"}, | ||
{"zsh on windows, should convert", "zsh", "C:\\Users\\foo\\.crc\\bin\\oc", "/C/Users/foo/.crc/bin/oc"}, | ||
{"fish on windows, should convert", "fish", "C:\\Users\\foo\\.crc\\bin\\oc", "/C/Users/foo/.crc/bin/oc"}, | ||
{"powershell on windows, should NOT convert", "powershell", "C:\\Users\\foo\\.crc\\bin\\oc", "C:\\Users\\foo\\.crc\\bin\\oc"}, | ||
{"cmd on windows, should NOT convert", "cmd", "C:\\Users\\foo\\.crc\\bin\\oc", "C:\\Users\\foo\\.crc\\bin\\oc"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := convertToLinuxStylePath(tt.userShell, tt.path) | ||
if result != tt.expectedPath { | ||
t.Errorf("convertToLinuxStylePath(%s, %s) = %s; want %s", tt.userShell, tt.path, result, tt.expectedPath) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters