revert(language): do not error when not installed

this reverts commit f23968a39c
as this was an incorrect solution to #2880, it can stay as is
This commit is contained in:
Jan De Dobbeleer 2022-10-05 08:10:30 +02:00 committed by Jan De Dobbeleer
parent 266fc5671f
commit a7a59b8b01
8 changed files with 27 additions and 39 deletions

View file

@ -33,7 +33,7 @@ func (a *Angular) Enabled() bool {
return a.language.Enabled() return a.language.Enabled()
} }
func (a *Angular) getVersion() string { func (a *Angular) getVersion() (string, error) {
// tested by nx_test.go // tested by nx_test.go
return getNodePackageVersion(a.language.env, filepath.Join("@angular", "core")) return getNodePackageVersion(a.language.env, filepath.Join("@angular", "core"))
} }

View file

@ -39,22 +39,20 @@ func (g *Golang) Init(props properties.Properties, env environment.Environment)
} }
} }
func (g *Golang) getVersion() string { func (g *Golang) getVersion() (string, error) {
if !g.props.GetBool(ParseModFile, false) { if !g.props.GetBool(ParseModFile, false) {
return "" return "", nil
} }
gomod, err := g.language.env.HasParentFilePath("go.mod") gomod, err := g.language.env.HasParentFilePath("go.mod")
if err != nil { if err != nil {
g.env.Log(environment.Debug, "getVersion", err.Error()) return "", nil
return ""
} }
contents := g.language.env.FileContent(gomod.Path) contents := g.language.env.FileContent(gomod.Path)
file, err := modfile.Parse(gomod.Path, []byte(contents), nil) file, err := modfile.Parse(gomod.Path, []byte(contents), nil)
if err != nil { if err != nil {
g.env.Log(environment.Debug, "getVersion", err.Error()) return "", err
return ""
} }
return file.Go.Version return file.Go.Version, nil
} }
func (g *Golang) Enabled() bool { func (g *Golang) Enabled() bool {

View file

@ -10,7 +10,6 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
mock2 "github.com/stretchr/testify/mock"
) )
type mockedLanguageParams struct { type mockedLanguageParams struct {
@ -30,7 +29,6 @@ func getMockedLanguageEnv(params *mockedLanguageParams) (*mock.MockedEnvironment
env.On("TemplateCache").Return(&environment.TemplateCache{ env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string), Env: make(map[string]string),
}) })
env.On("Log", mock2.Anything, mock2.Anything, mock2.Anything)
props := properties.Map{ props := properties.Map{
properties.FetchVersion: true, properties.FetchVersion: true,
} }

View file

@ -17,7 +17,7 @@ type loadContext func()
type inContext func() bool type inContext func() bool
type getVersion func() string type getVersion func() (string, error)
type matchesVersionFile func() bool type matchesVersionFile func() bool
type version struct { type version struct {
@ -171,8 +171,9 @@ func (l *language) setVersion() error {
continue continue
} }
} else { } else {
versionStr = command.getVersion() versionStr, err = command.getVersion()
if len(versionStr) == 0 { if err != nil || versionStr == "" {
lastError = errors.New("cannot get version")
continue continue
} }
} }

View file

@ -35,23 +35,21 @@ func (a *Nx) Enabled() bool {
return a.language.Enabled() return a.language.Enabled()
} }
func (a *Nx) getVersion() string { func (a *Nx) getVersion() (string, error) {
return getNodePackageVersion(a.language.env, "nx") return getNodePackageVersion(a.language.env, "nx")
} }
func getNodePackageVersion(env environment.Environment, nodePackage string) string { func getNodePackageVersion(env environment.Environment, nodePackage string) (string, error) {
const fileName string = "package.json" const fileName string = "package.json"
folder := filepath.Join(env.Pwd(), "node_modules", nodePackage) folder := filepath.Join(env.Pwd(), "node_modules", nodePackage)
if !env.HasFilesInDir(folder, fileName) { if !env.HasFilesInDir(folder, fileName) {
env.Log(environment.Debug, "getNodePackageVersion", fmt.Sprintf("%s not found in %s", fileName, folder)) return "", fmt.Errorf("%s not found in %s", fileName, folder)
return ""
} }
content := env.FileContent(filepath.Join(folder, fileName)) content := env.FileContent(filepath.Join(folder, fileName))
var data ProjectData var data ProjectData
err := json.Unmarshal([]byte(content), &data) err := json.Unmarshal([]byte(content), &data)
if err != nil { if err != nil {
env.Log(environment.Debug, "getNodePackageVersion", err.Error()) return "", err
return ""
} }
return data.Version return data.Version, nil
} }

View file

@ -7,7 +7,6 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
mock2 "github.com/stretchr/testify/mock"
) )
func TestGetNodePackageVersion(t *testing.T) { func TestGetNodePackageVersion(t *testing.T) {
@ -34,12 +33,12 @@ func TestGetNodePackageVersion(t *testing.T) {
env.On("TemplateCache").Return(&environment.TemplateCache{ env.On("TemplateCache").Return(&environment.TemplateCache{
Env: make(map[string]string), Env: make(map[string]string),
}) })
env.On("Log", mock2.Anything, mock2.Anything, mock2.Anything) got, err := getNodePackageVersion(env, "nx")
got := getNodePackageVersion(env, "nx")
if tc.ShouldFail { if tc.ShouldFail {
assert.Empty(t, got, tc.Case) assert.Error(t, err, tc.Case)
return return
} }
assert.Nil(t, err, tc.Case)
assert.Equal(t, tc.Version, got, tc.Case) assert.Equal(t, tc.Version, got, tc.Case)
} }
} }

View file

@ -1,6 +1,7 @@
package segments package segments
import ( import (
"errors"
"fmt" "fmt"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
@ -98,7 +99,7 @@ func (p *Python) canUseVenvName(name string) bool {
return true return true
} }
func (p *Python) pyenvVersion() string { func (p *Python) pyenvVersion() (string, error) {
// Use `pyenv root` instead of $PYENV_ROOT? // Use `pyenv root` instead of $PYENV_ROOT?
// 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?
@ -107,43 +108,37 @@ func (p *Python) pyenvVersion() string {
path = p.env.CommandPath("python3") path = p.env.CommandPath("python3")
} }
if len(path) == 0 { if len(path) == 0 {
p.env.Log(environment.Debug, "pyenvVersion", "python not found") return "", errors.New("no python executable found")
return ""
} }
pyEnvRoot := p.env.Getenv("PYENV_ROOT") 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(pyEnvRoot, "shims", "python") { if path != filepath.Join(pyEnvRoot, "shims", "python") {
p.env.Log(environment.Debug, "pyenvVersion", fmt.Sprintf("executable at %s is not a pyenv shim", path)) return "", fmt.Errorf("executable at %s is not a pyenv shim", path)
return ""
} }
// 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 {
p.env.Log(environment.Debug, "pyenvVersion", err.Error()) return "", err
return ""
} }
versionString := strings.Split(cmdOutput, ":")[0] versionString := strings.Split(cmdOutput, ":")[0]
if len(versionString) == 0 { if len(versionString) == 0 {
p.env.Log(environment.Debug, "pyenvVersion", "no pyenv version-name found") return "", errors.New("no pyenv version-name found")
return ""
} }
// $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(pyEnvRoot, "versions", versionString)) realPath, err := p.env.ResolveSymlink(filepath.Join(pyEnvRoot, "versions", versionString))
if err != nil { if err != nil {
p.env.Log(environment.Debug, "pyenvVersion", err.Error()) return "", err
return ""
} }
// ../versions/(version)[/envs/(virtualenv)] // ../versions/(version)[/envs/(virtualenv)]
shortPath, err := filepath.Rel(filepath.Join(pyEnvRoot, "versions"), realPath) shortPath, err := filepath.Rel(filepath.Join(pyEnvRoot, "versions"), realPath)
if err != nil { if err != nil {
p.env.Log(environment.Debug, "pyenvVersion", err.Error()) return "", err
return ""
} }
// override virtualenv if pyenv set one // override virtualenv if pyenv set one
parts := strings.Split(shortPath, string(filepath.Separator)) parts := strings.Split(shortPath, string(filepath.Separator))
if len(parts) > 2 && p.canUseVenvName(parts[2]) { if len(parts) > 2 && p.canUseVenvName(parts[2]) {
p.Venv = parts[2] p.Venv = parts[2]
} }
return parts[0] return parts[0], nil
} }

View file

@ -93,7 +93,6 @@ func TestPythonTemplate(t *testing.T) {
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", mock2.Anything).Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err) env.On("ResolveSymlink", mock2.Anything).Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err)
env.On("Log", mock2.Anything, mock2.Anything, mock2.Anything)
props := properties.Map{ props := properties.Map{
properties.FetchVersion: tc.FetchVersion, properties.FetchVersion: tc.FetchVersion,
UsePythonVersionFile: true, UsePythonVersionFile: true,