From 0bdad6fa9594198c7e3183ab99fbe44c69313252 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 27 Jul 2022 07:22:41 +0200 Subject: [PATCH] fix(language): do not fail on first error resolves #2569 --- src/segments/cmake_test.go | 2 +- src/segments/language.go | 16 ++++++++++------ src/segments/python_test.go | 9 +++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/segments/cmake_test.go b/src/segments/cmake_test.go index 86307810..cb90d9e1 100644 --- a/src/segments/cmake_test.go +++ b/src/segments/cmake_test.go @@ -15,7 +15,7 @@ func TestCmake(t *testing.T) { }{ {Case: "Cmake 3.23.2", ExpectedString: "3.23.2", Version: "cmake version 3.23.2"}, {Case: "Cmake 2.3.13", ExpectedString: "2.3.12", Version: "cmake version 2.3.12"}, - {Case: "", ExpectedString: "", Version: ""}, + {Case: "", ExpectedString: "err parsing info from cmake with", Version: ""}, } for _, tc := range cases { params := &mockedLanguageParams{ diff --git a/src/segments/language.go b/src/segments/language.go index dceff639..f23351d6 100644 --- a/src/segments/language.go +++ b/src/segments/language.go @@ -156,6 +156,7 @@ func (l *language) hasLanguageFolders() bool { // setVersion parses the version string returned by the command func (l *language) setVersion() error { + var lastError error for _, command := range l.commands { var versionStr string var err error @@ -166,20 +167,20 @@ func (l *language) setVersion() error { versionStr, err = l.env.RunCommand(command.executable, command.args...) if exitErr, ok := err.(*environment.CommandError); ok { l.exitCode = exitErr.ExitCode - return fmt.Errorf("err executing %s with %s", command.executable, command.args) + lastError = fmt.Errorf("err executing %s with %s", command.executable, command.args) + continue } } else { versionStr, err = command.getVersion() - if err != nil { + if err != nil || versionStr == "" { + lastError = errors.New("cannot get version") continue } } - if versionStr == "" { - continue - } version, err := command.parse(versionStr) if err != nil { - return fmt.Errorf("err parsing info from %s with %s", command.executable, versionStr) + lastError = fmt.Errorf("err parsing info from %s with %s", command.executable, versionStr) + continue } l.version = *version if command.versionURLTemplate != "" { @@ -189,6 +190,9 @@ func (l *language) setVersion() error { l.version.Executable = command.executable return nil } + if lastError != nil { + return lastError + } return errors.New(l.props.GetString(MissingCommandText, "")) } diff --git a/src/segments/python_test.go b/src/segments/python_test.go index e9e935ec..7c4c7c2f 100644 --- a/src/segments/python_test.go +++ b/src/segments/python_test.go @@ -66,6 +66,15 @@ func TestPythonTemplate(t *testing.T) { Template: "{{ if ne .Venv \"default\" }}{{ .Venv }} {{ end }}{{ .Major }}.{{ .Minor }}", ResolveSymlink: ResolveSymlink{Path: "/home/user.pyenv/versions/3.8.8", Err: nil}, }, + { + Case: "Pyenv virtual env version name", + FetchVersion: true, + VirtualEnvName: "demo", + Expected: "3.8.4", + PythonPath: "/home/user/.pyenv/shims/python", + Template: "{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}", + ResolveSymlink: ResolveSymlink{Path: "/home/user/.pyenv/versions/demo", Err: nil}, + }, } for _, tc := range cases {