Repository: JanDeDobbeleer/oh-my-posh
Pull request #2577 (2022-07-27 05:23:13)

language fix python
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added/updated (for bug fixes/features)

### Description

- fix(osc7): correct format
- fix(language): do not fail on first error


<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/environment/shell.go b/src/environment/shell.go
index 3033b97e..984f1eef 100644
--- a/src/environment/shell.go
+++ b/src/environment/shell.go
@@ -504,7 +504,7 @@ func (env *ShellEnvironment) RunCommand(command string, args ...string) (string,
 }
 
 func (env *ShellEnvironment) RunShellCommand(shell, command string) string {
-	defer env.Trace(time.Now(), "RunShellCommand", shell, command)
+	defer env.Trace(time.Now(), "RunShellCommand")
 	if out, err := env.RunCommand(shell, "-c", command); err == nil {
 		return out
 	}
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, ""))
 }
 


Test patch:
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/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 {