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-12-04 02:56:55 -08:00
|
|
|
func TestPythonTemplate(t *testing.T) {
|
2021-01-20 04:09:13 -08:00
|
|
|
cases := []struct {
|
2021-12-04 02:56:55 -08:00
|
|
|
Case string
|
|
|
|
Expected string
|
|
|
|
ExpectedDisabled bool
|
|
|
|
Template string
|
|
|
|
VirtualEnvName string
|
|
|
|
FetchVersion bool
|
2021-01-20 04:09:13 -08:00
|
|
|
}{
|
2021-12-04 02:56:55 -08:00
|
|
|
{Case: "No virtual env present", FetchVersion: true, Expected: "3.8.4", Template: "{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}"},
|
|
|
|
{Case: "Virtual env present", FetchVersion: true, Expected: "VENV 3.8.4", VirtualEnvName: "VENV", Template: "{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}"},
|
|
|
|
{
|
|
|
|
Case: "Virtual env major and minor dot",
|
|
|
|
FetchVersion: true,
|
|
|
|
Expected: "VENV 3.8",
|
|
|
|
VirtualEnvName: "VENV",
|
|
|
|
Template: "{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Major }}.{{ .Minor }}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Virtual env hide on default",
|
|
|
|
FetchVersion: true,
|
|
|
|
Expected: "3.8",
|
|
|
|
VirtualEnvName: "default",
|
|
|
|
Template: "{{ if ne .Venv \"default\" }}{{ .Venv }} {{ end }}{{ .Major }}.{{ .Minor }}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Virtual env show non default",
|
|
|
|
FetchVersion: true,
|
|
|
|
Expected: "billy 3.8",
|
|
|
|
VirtualEnvName: "billy",
|
|
|
|
Template: "{{ if ne .Venv \"default\" }}{{ .Venv }} {{ end }}{{ .Major }}.{{ .Minor }}",
|
|
|
|
},
|
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)
|
2022-01-23 12:37:51 -08:00
|
|
|
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.VirtualEnvName)
|
|
|
|
env.On("Getenv", "CONDA_DEFAULT_ENV").Return(tc.VirtualEnvName)
|
|
|
|
env.On("Getenv", "PYENV_VERSION").Return(tc.VirtualEnvName)
|
|
|
|
env.On("PathSeperator").Return("")
|
|
|
|
env.On("Pwd").Return("/usr/home/project")
|
|
|
|
env.On("Home").Return("/usr/home")
|
2022-01-01 11:08:08 -08:00
|
|
|
props := properties{
|
2022-01-23 12:37:51 -08:00
|
|
|
FetchVersion: tc.FetchVersion,
|
|
|
|
DisplayMode: DisplayModeAlways,
|
2021-01-20 04:09:13 -08:00
|
|
|
}
|
2022-01-23 12:37:51 -08:00
|
|
|
env.On("TemplateCache").Return(&TemplateCache{
|
|
|
|
Env: make(map[string]string),
|
|
|
|
})
|
2021-01-20 04:09:13 -08:00
|
|
|
python := &python{}
|
|
|
|
python.init(props, env)
|
2021-10-24 10:04:18 -07:00
|
|
|
assert.Equal(t, !tc.ExpectedDisabled, python.enabled(), tc.Case)
|
2022-01-23 12:37:51 -08:00
|
|
|
assert.Equal(t, tc.Expected, renderTemplate(env, tc.Template, python), tc.Case)
|
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)
|
2022-01-23 12:37:51 -08:00
|
|
|
env.On("PathSeperator").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("")
|
2021-01-20 04:09:13 -08:00
|
|
|
python := &python{}
|
2022-01-01 11:08:08 -08:00
|
|
|
python.init(properties{}, env)
|
2021-01-20 04:09:13 -08:00
|
|
|
python.loadContext()
|
|
|
|
assert.Equal(t, tc.Expected, python.inContext())
|
|
|
|
}
|
2021-01-12 11:38:13 -08:00
|
|
|
}
|