2020-10-02 02:55:27 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-11-14 11:04:04 -08:00
|
|
|
"github.com/alecthomas/assert"
|
2020-10-02 02:55:27 -07:00
|
|
|
)
|
|
|
|
|
2021-01-20 04:09:13 -08:00
|
|
|
func TestPythonVirtualEnv(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Expected string
|
|
|
|
VirtualEnvName string
|
|
|
|
CondaEnvName string
|
|
|
|
CondaDefaultEnvName string
|
|
|
|
PyEnvName string
|
|
|
|
DisplayVersion bool
|
2021-02-18 09:47:53 -08:00
|
|
|
DisplayDefault bool
|
2021-01-20 04:09:13 -08:00
|
|
|
}{
|
|
|
|
{Expected: "VENV", VirtualEnvName: "VENV"},
|
|
|
|
{Expected: "CONDA", CondaEnvName: "CONDA"},
|
|
|
|
{Expected: "CONDA", CondaDefaultEnvName: "CONDA"},
|
|
|
|
{Expected: "", CondaDefaultEnvName: "base"},
|
2021-02-18 09:47:53 -08:00
|
|
|
{Expected: "base", CondaDefaultEnvName: "base", DisplayDefault: true},
|
2021-01-20 04:09:13 -08:00
|
|
|
{Expected: "PYENV", PyEnvName: "PYENV"},
|
|
|
|
{Expected: "PYENV 3.8.4", PyEnvName: "PYENV", DisplayVersion: true},
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
2020-10-02 02:55:27 -07:00
|
|
|
|
2021-01-20 04:09:13 -08:00
|
|
|
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("")
|
2021-03-22 11:19:08 -07:00
|
|
|
env.On("getcwd", nil).Return("/usr/home/project")
|
|
|
|
env.On("homeDir", nil).Return("/usr/home")
|
2021-01-20 04:09:13 -08:00
|
|
|
props := &properties{
|
|
|
|
values: map[Property]interface{}{
|
|
|
|
DisplayVersion: tc.DisplayVersion,
|
|
|
|
DisplayVirtualEnv: true,
|
2021-02-18 09:47:53 -08:00
|
|
|
DisplayDefault: tc.DisplayDefault,
|
2021-01-20 04:09:13 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
python := &python{}
|
|
|
|
python.init(props, env)
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, tc.Expected, python.string())
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
2020-10-02 02:55:27 -07:00
|
|
|
}
|
2021-01-12 11:38:13 -08:00
|
|
|
|
|
|
|
func TestPythonPythonInContext(t *testing.T) {
|
2021-01-20 04:09:13 -08:00
|
|
|
cases := []struct {
|
|
|
|
Expected bool
|
|
|
|
VirtualEnvName string
|
|
|
|
}{
|
|
|
|
{Expected: true, VirtualEnvName: "VENV"},
|
|
|
|
{Expected: false, VirtualEnvName: ""},
|
2021-01-12 11:38:13 -08:00
|
|
|
}
|
|
|
|
|
2021-01-20 04:09:13 -08:00
|
|
|
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())
|
|
|
|
}
|
2021-01-12 11:38:13 -08:00
|
|
|
}
|