Skip to content

Commit

Permalink
feat: show python segment when Jupyter Notebook files are present
Browse files Browse the repository at this point in the history
  • Loading branch information
akvitberg authored and JanDeDobbeleer committed Oct 8, 2020
1 parent 59ed9c2 commit 0b0fd4c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/docs/segment-python.md
Expand Up @@ -6,7 +6,7 @@ sidebar_label: Python

## What

Display the currently active python version and virtualenv when a folder contains `.py` files.
Display the currently active python version and virtualenv when a folder contains `.py` files or `.ipynb` files.
Supports conda, virtualenv and pyenv.

## Sample Configuration
Expand Down
2 changes: 1 addition & 1 deletion segment_python.go
Expand Up @@ -30,7 +30,7 @@ func (p *python) init(props *properties, env environmentInfo) {
}

func (p *python) enabled() bool {
if !p.env.hasFiles("*.py") {
if !p.env.hasFiles("*.py") && !p.env.hasFiles("*.ipynb") {
return false
}
pythonVersions := []string{
Expand Down
48 changes: 44 additions & 4 deletions segment_python_test.go
Expand Up @@ -15,7 +15,8 @@ type pythonArgs struct {
pathSeparator string
pythonVersion string
python3Version string
hasFiles bool
hasPyFiles bool
hasNotebookFiles bool
}

func newPythonArgs() *pythonArgs {
Expand All @@ -27,13 +28,15 @@ func newPythonArgs() *pythonArgs {
pathSeparator: "/",
pythonVersion: "",
python3Version: "",
hasFiles: true,
hasPyFiles: true,
hasNotebookFiles: true,
}
}

func bootStrapPythonTest(args *pythonArgs) *python {
env := new(MockedEnvironment)
env.On("hasFiles", "*.py").Return(args.hasFiles)
env.On("hasFiles", "*.py").Return(args.hasPyFiles)
env.On("hasFiles", "*.ipynb").Return(args.hasNotebookFiles)
env.On("runCommand", "python", []string{"--version"}).Return(args.pythonVersion)
env.On("runCommand", "python3", []string{"--version"}).Return(args.python3Version)
env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName)
Expand All @@ -49,11 +52,48 @@ func bootStrapPythonTest(args *pythonArgs) *python {

func TestPythonWriterDisabledNoPythonFiles(t *testing.T) {
args := newPythonArgs()
args.hasFiles = false
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)
Expand Down

0 comments on commit 0b0fd4c

Please sign in to comment.