mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-10 13:04:04 -08:00
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:
parent
266fc5671f
commit
a7a59b8b01
|
@ -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"))
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue