fix(python): do not stop at pyenv fail

adds more logging for #2569
This commit is contained in:
Jan De Dobbeleer 2022-07-26 07:28:54 +02:00 committed by Jan De Dobbeleer
parent 000602f28a
commit b5b35d1456
5 changed files with 33 additions and 19 deletions

View file

@ -416,7 +416,14 @@ func (env *ShellEnvironment) HasFolder(folder string) bool {
} }
func (env *ShellEnvironment) ResolveSymlink(path string) (string, error) { func (env *ShellEnvironment) ResolveSymlink(path string) (string, error) {
return filepath.EvalSymlinks(path) defer env.Trace(time.Now(), "ResolveSymlink", path)
link, err := filepath.EvalSymlinks(path)
if err != nil {
env.Log(Error, "ResolveSymlink", err.Error())
return "", err
}
env.Log(Debug, "ResolveSymlink", link)
return link, nil
} }
func (env *ShellEnvironment) FileContent(file string) string { func (env *ShellEnvironment) FileContent(file string) string {
@ -505,18 +512,21 @@ func (env *ShellEnvironment) RunShellCommand(shell, command string) string {
} }
func (env *ShellEnvironment) CommandPath(command string) string { func (env *ShellEnvironment) CommandPath(command string) string {
defer env.Trace(time.Now(), "HasCommand", command) defer env.Trace(time.Now(), "CommandPath", command)
if path, ok := env.cmdCache.get(command); ok { if path, ok := env.cmdCache.get(command); ok {
env.Log(Debug, "CommandPath", path)
return path return path
} }
path, err := exec.LookPath(command) path, err := exec.LookPath(command)
if err == nil { if err == nil {
env.cmdCache.set(command, path) env.cmdCache.set(command, path)
env.Log(Debug, "CommandPath", path)
return path return path
} }
path, err = env.LookWinAppPath(command) path, err = env.LookWinAppPath(command)
if err == nil { if err == nil {
env.cmdCache.set(command, path) env.cmdCache.set(command, path)
env.Log(Debug, "CommandPath", path)
return path return path
} }
env.Log(Error, "CommandPath", err.Error()) env.Log(Error, "CommandPath", err.Error())
@ -524,6 +534,7 @@ func (env *ShellEnvironment) CommandPath(command string) string {
} }
func (env *ShellEnvironment) HasCommand(command string) bool { func (env *ShellEnvironment) HasCommand(command string) bool {
defer env.Trace(time.Now(), "HasCommand", command)
if path := env.CommandPath(command); path != "" { if path := env.CommandPath(command); path != "" {
return true return true
} }

View file

@ -53,7 +53,7 @@ func TestGolang(t *testing.T) {
ParseModFile: true, ParseModFile: true,
HasModFileInParentDir: true, HasModFileInParentDir: true,
InvalidModfile: true, InvalidModfile: true,
ExpectedString: "../go.mod:1: unknown directive: invalid", ExpectedString: "1.16",
Version: "go version go1.16 darwin/amd64", Version: "go version go1.16 darwin/amd64",
}, },
} }

View file

@ -171,7 +171,7 @@ func (l *language) setVersion() error {
} else { } else {
versionStr, err = command.getVersion() versionStr, err = command.getVersion()
if err != nil { if err != nil {
return err continue
} }
} }
if versionStr == "" { if versionStr == "" {

View file

@ -1,6 +1,8 @@
package segments package segments
import ( import (
"errors"
"fmt"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
"path/filepath" "path/filepath"
@ -102,36 +104,36 @@ func (p *Python) pyenvVersion() (string, error) {
// Is our Python executable at $PYENV_ROOT/bin/python ? // Is our Python executable at $PYENV_ROOT/bin/python ?
// Should p.env expose command paths? // Should p.env expose command paths?
path := p.env.CommandPath("python") path := p.env.CommandPath("python")
if path == "" { if len(path) == 0 {
path = p.env.CommandPath("python3") path = p.env.CommandPath("python3")
} }
if path == "" { if len(path) == 0 {
return "", nil return "", errors.New("no python executable found")
} }
pyEnvRoot := p.env.Getenv("PYENV_ROOT")
// TODO: pyenv-win has this at $PYENV_ROOT/pyenv-win/shims // TODO: pyenv-win has this at $PYENV_ROOT/pyenv-win/shims
if path != filepath.Join(p.env.Getenv("PYENV_ROOT"), "shims", "python") { if path != filepath.Join(pyEnvRoot, "shims", "python") {
return "", nil return "", fmt.Errorf("executable at %s is not a pyenv shim", path)
} }
// pyenv version-name will return current version or virtualenv // pyenv version-name will return current version or virtualenv
cmdOutput, err := p.env.RunCommand("pyenv", "version-name") cmdOutput, err := p.env.RunCommand("pyenv", "version-name")
if err != nil { if err != nil {
// TODO: Improve reporting return "", err
return "", nil
} }
versionString := strings.Split(cmdOutput, ":")[0] versionString := strings.Split(cmdOutput, ":")[0]
if versionString == "" { if len(versionString) == 0 {
return "", nil return "", errors.New("no pyenv version-name found")
} }
// $PYENV_ROOT/versions + versionString (symlinks resolved) == $PYENV_ROOT/versions/(version)[/envs/(virtualenv)] // $PYENV_ROOT/versions + versionString (symlinks resolved) == $PYENV_ROOT/versions/(version)[/envs/(virtualenv)]
realPath, err := p.env.ResolveSymlink(filepath.Join(p.env.Getenv("PYENV_ROOT"), "versions", versionString)) realPath, err := p.env.ResolveSymlink(filepath.Join(pyEnvRoot, "versions", versionString))
if err != nil { if err != nil {
return "", nil return "", err
} }
// ../versions/(version)[/envs/(virtualenv)] // ../versions/(version)[/envs/(virtualenv)]
shortPath, err := filepath.Rel(filepath.Join(p.env.Getenv("PYENV_ROOT"), "versions"), realPath) shortPath, err := filepath.Rel(filepath.Join(pyEnvRoot, "versions"), realPath)
if err != nil { if err != nil {
return "", nil return "", err
} }
// Unset whatever loadContext thinks Venv should be // Unset whatever loadContext thinks Venv should be
p.Venv = "" p.Venv = ""

View file

@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/alecthomas/assert" "github.com/alecthomas/assert"
mock2 "github.com/stretchr/testify/mock"
) )
func TestPythonTemplate(t *testing.T) { func TestPythonTemplate(t *testing.T) {
@ -70,7 +71,7 @@ func TestPythonTemplate(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
env.On("HasCommand", "python").Return(true) env.On("HasCommand", "python").Return(true)
env.On("CommandPath", "mock.Anything").Return(tc.PythonPath) env.On("CommandPath", mock2.Anything).Return(tc.PythonPath)
env.On("RunCommand", "python", []string{"--version"}).Return("Python 3.8.4", nil) env.On("RunCommand", "python", []string{"--version"}).Return("Python 3.8.4", nil)
env.On("RunCommand", "pyenv", []string{"version-name"}).Return(tc.VirtualEnvName, nil) env.On("RunCommand", "pyenv", []string{"version-name"}).Return(tc.VirtualEnvName, nil)
env.On("HasFiles", "*.py").Return(true) env.On("HasFiles", "*.py").Return(true)
@ -81,7 +82,7 @@ func TestPythonTemplate(t *testing.T) {
env.On("PathSeparator").Return("") env.On("PathSeparator").Return("")
env.On("Pwd").Return("/usr/home/project") env.On("Pwd").Return("/usr/home/project")
env.On("Home").Return("/usr/home") env.On("Home").Return("/usr/home")
env.On("ResolveSymlink", "mock.Anything").Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err) env.On("ResolveSymlink", mock2.Anything).Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err)
props := properties.Map{ props := properties.Map{
properties.FetchVersion: tc.FetchVersion, properties.FetchVersion: tc.FetchVersion,
UsePythonVersionFile: true, UsePythonVersionFile: true,