From f9d2f6040eeeb7389001aadf5f9ffd33cf7bcf8a Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 25 Jul 2024 06:47:33 +0200 Subject: [PATCH] feat(cygwin): better detection --- src/runtime/mock/environment.go | 5 +++++ src/runtime/os.go | 7 ++++++- src/runtime/os_unix.go | 5 +++++ src/runtime/os_windows.go | 5 +++++ src/segments/path.go | 3 +-- src/segments/path_test.go | 5 ++++- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/runtime/mock/environment.go b/src/runtime/mock/environment.go index 1fece075..2442f908 100644 --- a/src/runtime/mock/environment.go +++ b/src/runtime/mock/environment.go @@ -172,6 +172,11 @@ func (env *Environment) IsWsl2() bool { return args.Bool(0) } +func (env *Environment) IsCygwin() bool { + args := env.Called() + return args.Bool(0) +} + func (env *Environment) TerminalWidth() (int, error) { args := env.Called() return args.Int(0), args.Error(1) diff --git a/src/runtime/os.go b/src/runtime/os.go index 5dd46e9d..78a25be3 100644 --- a/src/runtime/os.go +++ b/src/runtime/os.go @@ -175,6 +175,7 @@ type Environment interface { HTTPRequest(url string, body io.Reader, timeout int, requestModifiers ...http.RequestModifier) ([]byte, error) IsWsl() bool IsWsl2() bool + IsCygwin() bool StackCount() int TerminalWidth() (int, error) CachePath() string @@ -264,9 +265,13 @@ func (term *Terminal) resolveConfigPath() { return } + isCygwin := func() bool { + return term.Platform() == WINDOWS && len(term.Getenv("OSTYPE")) > 0 + } + // Cygwin path always needs the full path as we're on Windows but not really. // Doing filepath actions will convert it to a Windows path and break the init script. - if term.Platform() == WINDOWS && term.Shell() == "bash" { + if isCygwin() { term.Debug("Cygwin detected, using full path for config") return } diff --git a/src/runtime/os_unix.go b/src/runtime/os_unix.go index f095b141..a49c2a3b 100644 --- a/src/runtime/os_unix.go +++ b/src/runtime/os_unix.go @@ -53,6 +53,11 @@ func (term *Terminal) IsWsl2() bool { return strings.Contains(uname, "WSL2") } +func (term *Terminal) IsCygwin() bool { + defer term.Trace(time.Now()) + return false +} + func (term *Terminal) TerminalWidth() (int, error) { defer term.Trace(time.Now()) diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go index c924355b..ffefcd62 100644 --- a/src/runtime/os_windows.go +++ b/src/runtime/os_windows.go @@ -87,6 +87,11 @@ func (term *Terminal) IsWsl2() bool { return false } +func (term *Terminal) IsCygwin() bool { + defer term.Trace(time.Now()) + return len(term.Getenv("OSTYPE")) > 0 +} + func (term *Terminal) TerminalWidth() (int, error) { defer term.Trace(time.Now()) diff --git a/src/segments/path.go b/src/segments/path.go index c6479fe0..38c44e29 100644 --- a/src/segments/path.go +++ b/src/segments/path.go @@ -126,8 +126,7 @@ func (pt *Path) setPaths() { return false } - isCygwin := pt.env.GOOS() == runtime.WINDOWS && pt.env.Shell() == shell.BASH - return isCygwin + return pt.env.IsCygwin() } pt.cygPath = displayCygpath() diff --git a/src/segments/path_test.go b/src/segments/path_test.go index 3fa39853..51adda43 100644 --- a/src/segments/path_test.go +++ b/src/segments/path_test.go @@ -180,6 +180,7 @@ func TestAgnosterPathStyles(t *testing.T) { MaxDepth int MaxWidth int HideRootLocation bool + Cygwin bool }{ { Style: Unique, @@ -431,6 +432,7 @@ func TestAgnosterPathStyles(t *testing.T) { Pwd: "C:\\Users\\foo\\foobar\\man", GOOS: runtime.WINDOWS, Shell: shell.BASH, + Cygwin: true, Cygpath: "/c/Users/foo/foobar/man", PathSeparator: "\\", FolderSeparatorIcon: " > ", @@ -766,6 +768,7 @@ func TestAgnosterPathStyles(t *testing.T) { env.On("Home").Return(tc.HomePath) env.On("Pwd").Return(tc.Pwd) env.On("GOOS").Return(tc.GOOS) + env.On("IsCygwin").Return(tc.Cygwin) env.On("StackCount").Return(0) env.On("IsWsl").Return(false) args := &runtime.Flags{ @@ -780,7 +783,7 @@ func TestAgnosterPathStyles(t *testing.T) { env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) - displayCygpath := tc.GOOS == runtime.WINDOWS && tc.Shell == shell.BASH + displayCygpath := tc.Cygwin if displayCygpath { env.On("RunCommand", "cygpath", []string{"-u", tc.Pwd}).Return(tc.Cygpath, tc.CygpathError) env.On("RunCommand", "cygpath", testify_.Anything).Return("brrrr", nil)