oh-my-posh/segment_python_test.go
Travis Illig 5844faa54d feat: dotnet segment for .NET SDK display
New segment for .NET SDK version (or unsupported version) display.

Includes update for handling command execution errors so segments
can act differently based on exit codes. Using a custom error
type to make it testable rather than passing the OS error directly
to the segment.
2020-10-16 11:39:01 -07:00

191 lines
5.7 KiB
Go
Executable file

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
hasPyFiles bool
hasNotebookFiles bool
}
func newPythonArgs() *pythonArgs {
return &pythonArgs{
virtualEnvName: "",
condaEnvName: "",
condaDefaultName: "",
pyEnvName: "",
pathSeparator: "/",
pythonVersion: "",
python3Version: "",
hasPyFiles: true,
hasNotebookFiles: true,
}
}
func bootStrapPythonTest(args *pythonArgs) *python {
env := new(MockedEnvironment)
env.On("hasFiles", "*.py").Return(args.hasPyFiles)
env.On("hasFiles", "*.ipynb").Return(args.hasNotebookFiles)
env.On("runCommand", "python", []string{"--version"}).Return(args.pythonVersion, nil)
env.On("runCommand", "python3", []string{"--version"}).Return(args.python3Version, nil)
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()
args.hasPyFiles = false
args.hasNotebookFiles = false
args.python3Version = "3.4.5"
python := bootStrapPythonTest(args)
assert.False(t, python.enabled(), "there are no Python files in the current folder")
}
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")
}
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)
}