2020-10-02 02:55:27 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pythonArgs struct {
|
|
|
|
virtualEnvName string
|
|
|
|
condaEnvName string
|
|
|
|
condaDefaultName string
|
|
|
|
pyEnvName string
|
|
|
|
pathSeparator string
|
|
|
|
pythonVersion string
|
|
|
|
python3Version string
|
2020-10-16 08:43:02 -07:00
|
|
|
hasPyFiles bool
|
|
|
|
hasNotebookFiles bool
|
2020-10-02 02:55:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPythonArgs() *pythonArgs {
|
|
|
|
return &pythonArgs{
|
|
|
|
virtualEnvName: "",
|
|
|
|
condaEnvName: "",
|
|
|
|
condaDefaultName: "",
|
|
|
|
pyEnvName: "",
|
|
|
|
pathSeparator: "/",
|
|
|
|
pythonVersion: "",
|
|
|
|
python3Version: "",
|
2020-10-08 02:27:45 -07:00
|
|
|
hasPyFiles: true,
|
|
|
|
hasNotebookFiles: true,
|
2020-10-02 02:55:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func bootStrapPythonTest(args *pythonArgs) *python {
|
|
|
|
env := new(MockedEnvironment)
|
2020-10-08 02:27:45 -07:00
|
|
|
env.On("hasFiles", "*.py").Return(args.hasPyFiles)
|
|
|
|
env.On("hasFiles", "*.ipynb").Return(args.hasNotebookFiles)
|
2020-10-16 08:43:02 -07:00
|
|
|
env.On("runCommand", "python", []string{"--version"}).Return(args.pythonVersion, nil)
|
|
|
|
env.On("runCommand", "python3", []string{"--version"}).Return(args.python3Version, nil)
|
2020-10-02 02:55:27 -07:00
|
|
|
env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName)
|
|
|
|
env.On("getenv", "CONDA_ENV_PATH").Return(args.condaEnvName)
|
|
|
|
env.On("getenv", "CONDA_DEFAULT_ENV").Return(args.condaDefaultName)
|
|
|
|
env.On("getenv", "PYENV_VERSION").Return(args.pyEnvName)
|
|
|
|
env.On("getPathSeperator", nil).Return(args.pathSeparator)
|
|
|
|
python := &python{
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
return python
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterDisabledNoPythonFiles(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
2020-10-08 02:27:45 -07:00
|
|
|
args.hasPyFiles = false
|
|
|
|
args.hasNotebookFiles = false
|
|
|
|
args.python3Version = "3.4.5"
|
2020-10-02 02:55:27 -07:00
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.False(t, python.enabled(), "there are no Python files in the current folder")
|
|
|
|
}
|
|
|
|
|
2020-10-08 02:27:45 -07:00
|
|
|
func TestPythonWriterDisabledHasPythonFiles(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.hasPyFiles = true
|
|
|
|
args.hasNotebookFiles = false
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled(), "there should be a Python file in the current folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterDisabledHasJupyterNotebookFiles(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.hasPyFiles = false
|
|
|
|
args.hasNotebookFiles = true
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled(), "there should be a Jupyter Notebook file in the current folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterDisabledHasPyAndJupyterNotebookFiles(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.hasPyFiles = true
|
|
|
|
args.hasNotebookFiles = true
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled(), "there should be a Jupyter Notebook file in the current folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterDisabledHasPyAndJupyterNotebookFilesButNoVersion(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.hasPyFiles = true
|
|
|
|
args.hasNotebookFiles = true
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.False(t, python.enabled(), "there should be a Jupyter Notebook file in the current folder")
|
|
|
|
}
|
|
|
|
|
2020-10-02 02:55:27 -07:00
|
|
|
func TestPythonWriterDisabledNoPythonInstalled(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.False(t, python.enabled(), "Python isn't installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterEnabledNoVirtualEnv(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, args.python3Version, python.string())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterEnabledVirtualEnvOverrule(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
args.condaEnvName = "myenv"
|
|
|
|
props := &properties{
|
|
|
|
values: map[Property]interface{}{
|
|
|
|
DisplayVirtualEnv: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
python.props = props
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, args.python3Version, python.string())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterEnabledVirtualEnv(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
args.condaEnvName = "myenv"
|
|
|
|
expected := fmt.Sprintf("%s %s", args.condaEnvName, args.python3Version)
|
|
|
|
props := &properties{
|
|
|
|
values: map[Property]interface{}{
|
|
|
|
DisplayVirtualEnv: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
python.props = props
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, expected, python.string())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterEnabledWithVirtualEnv(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.virtualEnvName = "venv"
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
expected := fmt.Sprintf("%s %s", args.virtualEnvName, args.python3Version)
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, expected, python.string())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterEnabledWithCondaEnvPath(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.condaEnvName = "conda"
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
expected := fmt.Sprintf("%s %s", args.condaEnvName, args.python3Version)
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, expected, python.string())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterEnabledWithCondaDefaultEnv(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.condaDefaultName = "conda2"
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
expected := fmt.Sprintf("%s %s", args.condaDefaultName, args.python3Version)
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, expected, python.string())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterEnabledWithTwoValidEnvs(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.condaEnvName = "conda"
|
|
|
|
args.condaDefaultName = "conda2"
|
|
|
|
args.python3Version = "3.4.5"
|
|
|
|
expected := fmt.Sprintf("%s %s", args.condaEnvName, args.python3Version)
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, expected, python.string())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPythonWriterNameTrailingSlash(t *testing.T) {
|
|
|
|
args := newPythonArgs()
|
|
|
|
args.virtualEnvName = "python/"
|
|
|
|
args.pythonVersion = "2.7.3"
|
|
|
|
python := bootStrapPythonTest(args)
|
|
|
|
assert.True(t, python.enabled())
|
|
|
|
assert.Equal(t, "python", python.venvName)
|
|
|
|
}
|