fix(python): restore CONDA_DEFAULT_ENV

resolves #360
This commit is contained in:
Jan De Dobbeleer 2021-01-20 13:09:13 +01:00 committed by Jan De Dobbeleer
parent 890d0ad0e1
commit 2af3e672fd
3 changed files with 80 additions and 77 deletions

View file

@ -27,6 +27,8 @@ Supports conda, virtualenv and pyenv.
## Properties
- display_virtual_env: `boolean` - show the name of the virtualenv or not - defaults to `true`
- display_default_env: `boolean` - show the name of the virtualenv when it's default (`system`, `base`)
or not - defaults to `true`
- display_version: `boolean` - display the python version - defaults to `true`
- missing_command_text: `string` - text to display when the command is missing - defaults to empty
- display_mode: `string` - determines when the segment is displayed

View file

@ -10,6 +10,8 @@ type python struct {
const (
// DisplayVirtualEnv shows or hides the virtual env
DisplayVirtualEnv Property = "display_virtual_env"
// DisplayDefaultEnv shows or hides the default env names (system/base)
DisplayDefaultEnv Property = "display_default_env"
)
func (p *python) string() string {
@ -44,13 +46,15 @@ func (p *python) loadContext() {
venvVars := []string{
"VIRTUAL_ENV",
"CONDA_ENV_PATH",
"CONDA_DEFAULT_ENV",
"PYENV_VERSION",
}
var venv string
for _, venvVar := range venvVars {
venv = p.language.env.getenv(venvVar)
if venv != "" {
p.venvName = base(venv, p.language.env)
name := base(venv, p.language.env)
if p.canUseVenvName(name) {
p.venvName = name
break
}
}
@ -59,3 +63,19 @@ func (p *python) loadContext() {
func (p *python) inContext() bool {
return p.venvName != ""
}
func (p *python) canUseVenvName(name string) bool {
if name == "" || name == "." {
return false
}
if p.language.props.getBool(DisplayDefaultEnv, true) {
return true
}
invalidNames := [2]string{"system", "base"}
for _, a := range invalidNames {
if a == name {
return false
}
}
return true
}

View file

@ -6,87 +6,68 @@ import (
"github.com/alecthomas/assert"
)
type pythonArgs struct {
virtualEnvName string
condaEnvName string
pyEnvName string
displayVersion bool
}
func bootStrapPythonTest(args *pythonArgs) *python {
env := new(MockedEnvironment)
env.On("hasCommand", "python").Return(true)
env.On("runCommand", "python", []string{"--version"}).Return("Python 3.8.4", nil)
env.On("hasFiles", "*.py").Return(true)
env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName)
env.On("getenv", "CONDA_ENV_PATH").Return(args.condaEnvName)
env.On("getenv", "PYENV_VERSION").Return(args.pyEnvName)
env.On("getPathSeperator", nil).Return("")
props := &properties{
values: map[Property]interface{}{
DisplayVersion: args.displayVersion,
DisplayVirtualEnv: true,
},
func TestPythonVirtualEnv(t *testing.T) {
cases := []struct {
Expected string
VirtualEnvName string
CondaEnvName string
CondaDefaultEnvName string
PyEnvName string
DisplayVersion bool
DisplayDefaultEnv bool
}{
{Expected: "VENV", VirtualEnvName: "VENV"},
{Expected: "CONDA", CondaEnvName: "CONDA"},
{Expected: "CONDA", CondaDefaultEnvName: "CONDA"},
{Expected: "", CondaDefaultEnvName: "base"},
{Expected: "base", CondaDefaultEnvName: "base", DisplayDefaultEnv: true},
{Expected: "PYENV", PyEnvName: "PYENV"},
{Expected: "PYENV 3.8.4", PyEnvName: "PYENV", DisplayVersion: true},
}
python := &python{}
python.init(props, env)
return python
}
func TestPythonVertualEnv(t *testing.T) {
expected := "VENV"
args := &pythonArgs{
virtualEnvName: expected,
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("hasCommand", "python").Return(true)
env.On("runCommand", "python", []string{"--version"}).Return("Python 3.8.4", nil)
env.On("hasFiles", "*.py").Return(true)
env.On("getenv", "VIRTUAL_ENV").Return(tc.VirtualEnvName)
env.On("getenv", "CONDA_ENV_PATH").Return(tc.CondaEnvName)
env.On("getenv", "CONDA_DEFAULT_ENV").Return(tc.CondaDefaultEnvName)
env.On("getenv", "PYENV_VERSION").Return(tc.PyEnvName)
env.On("getPathSeperator", nil).Return("")
props := &properties{
values: map[Property]interface{}{
DisplayVersion: tc.DisplayVersion,
DisplayVirtualEnv: true,
DisplayDefaultEnv: tc.DisplayDefaultEnv,
},
}
python := &python{}
python.init(props, env)
assert.True(t, python.enabled())
assert.Equal(t, tc.Expected, python.string())
}
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())
assert.Equal(t, expected, python.string())
}
func TestPythonCondaEnv(t *testing.T) {
expected := "CONDA"
args := &pythonArgs{
condaEnvName: expected,
}
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())
assert.Equal(t, expected, python.string())
}
func TestPythonPyEnv(t *testing.T) {
expected := "PYENV"
args := &pythonArgs{
pyEnvName: expected,
}
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())
assert.Equal(t, expected, python.string())
}
func TestPythonPyEnvWithVersion(t *testing.T) {
expected := "PYENV 3.8.4"
args := &pythonArgs{
pyEnvName: "PYENV",
displayVersion: true,
}
python := bootStrapPythonTest(args)
assert.True(t, python.enabled())
assert.Equal(t, expected, python.string())
assert.Equal(t, "3.8.4", python.language.version)
}
func TestPythonPythonInContext(t *testing.T) {
args := &pythonArgs{
pyEnvName: "PYENV",
displayVersion: true,
cases := []struct {
Expected bool
VirtualEnvName string
}{
{Expected: true, VirtualEnvName: "VENV"},
{Expected: false, VirtualEnvName: ""},
}
python := bootStrapPythonTest(args)
python.loadContext()
assert.True(t, python.inContext())
}
func TestPythonPythonNotInContext(t *testing.T) {
python := bootStrapPythonTest(&pythonArgs{})
python.loadContext()
assert.False(t, python.inContext())
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("")
env.On("getenv", "VIRTUAL_ENV").Return(tc.VirtualEnvName)
env.On("getenv", "CONDA_ENV_PATH").Return("")
env.On("getenv", "CONDA_DEFAULT_ENV").Return("")
env.On("getenv", "PYENV_VERSION").Return("")
python := &python{}
python.init(nil, env)
python.loadContext()
assert.Equal(t, tc.Expected, python.inContext())
}
}