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()
}
func (a *Angular) getVersion() string {
func (a *Angular) getVersion() (string, error) {
// tested by nx_test.go
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) {
return ""
return "", nil
}
gomod, err := g.language.env.HasParentFilePath("go.mod")
if err != nil {
g.env.Log(environment.Debug, "getVersion", err.Error())
return ""
return "", nil
}
contents := g.language.env.FileContent(gomod.Path)
file, err := modfile.Parse(gomod.Path, []byte(contents), nil)
if err != nil {
g.env.Log(environment.Debug, "getVersion", err.Error())
return ""
return "", err
}
return file.Go.Version
return file.Go.Version, nil
}
func (g *Golang) Enabled() bool {

View file

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

View file

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

View file

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

View file

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

View file

@ -1,6 +1,7 @@
package segments
import (
"errors"
"fmt"
"oh-my-posh/environment"
"oh-my-posh/properties"
@ -98,7 +99,7 @@ func (p *Python) canUseVenvName(name string) bool {
return true
}
func (p *Python) pyenvVersion() string {
func (p *Python) pyenvVersion() (string, error) {
// Use `pyenv root` instead of $PYENV_ROOT?
// Is our Python executable at $PYENV_ROOT/bin/python ?
// Should p.env expose command paths?
@ -107,43 +108,37 @@ func (p *Python) pyenvVersion() string {
path = p.env.CommandPath("python3")
}
if len(path) == 0 {
p.env.Log(environment.Debug, "pyenvVersion", "python not found")
return ""
return "", errors.New("no python executable found")
}
pyEnvRoot := p.env.Getenv("PYENV_ROOT")
// TODO: pyenv-win has this at $PYENV_ROOT/pyenv-win/shims
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 ""
return "", fmt.Errorf("executable at %s is not a pyenv shim", path)
}
// pyenv version-name will return current version or virtualenv
cmdOutput, err := p.env.RunCommand("pyenv", "version-name")
if err != nil {
p.env.Log(environment.Debug, "pyenvVersion", err.Error())
return ""
return "", err
}
versionString := strings.Split(cmdOutput, ":")[0]
if len(versionString) == 0 {
p.env.Log(environment.Debug, "pyenvVersion", "no pyenv version-name found")
return ""
return "", errors.New("no pyenv version-name found")
}
// $PYENV_ROOT/versions + versionString (symlinks resolved) == $PYENV_ROOT/versions/(version)[/envs/(virtualenv)]
realPath, err := p.env.ResolveSymlink(filepath.Join(pyEnvRoot, "versions", versionString))
if err != nil {
p.env.Log(environment.Debug, "pyenvVersion", err.Error())
return ""
return "", err
}
// ../versions/(version)[/envs/(virtualenv)]
shortPath, err := filepath.Rel(filepath.Join(pyEnvRoot, "versions"), realPath)
if err != nil {
p.env.Log(environment.Debug, "pyenvVersion", err.Error())
return ""
return "", err
}
// override virtualenv if pyenv set one
parts := strings.Split(shortPath, string(filepath.Separator))
if len(parts) > 2 && p.canUseVenvName(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("Home").Return("/usr/home")
env.On("ResolveSymlink", mock2.Anything).Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err)
env.On("Log", mock2.Anything, mock2.Anything, mock2.Anything)
props := properties.Map{
properties.FetchVersion: tc.FetchVersion,
UsePythonVersionFile: true,