feat(python): make folder fallback env names customizable

This commit is contained in:
Antonin Ruan 2024-10-05 22:19:32 -10:00 committed by Jan De Dobbeleer
parent 9186d931f8
commit daea52598c
4 changed files with 68 additions and 3 deletions

View file

@ -22,6 +22,7 @@ const (
FetchVirtualEnv properties.Property = "fetch_virtual_env"
UsePythonVersionFile properties.Property = "use_python_version_file"
FolderNameFallback properties.Property = "folder_name_fallback"
FallbackNames properties.Property = "fallback_names"
)
func (p *Python) Template() string {
@ -81,10 +82,10 @@ func (p *Python) loadContext() {
}
folderNameFallback := p.language.props.GetBool(FolderNameFallback, true)
defaultVenvNames := []string{
defaultVenvNames := p.language.props.GetStringArray(FallbackNames, []string{
".venv",
"venv",
}
})
var venv string
for _, venvVar := range venvVars {

View file

@ -198,3 +198,48 @@ func TestPythonVirtualEnvIgnoreDefaultVenvNames(t *testing.T) {
assert.Equal(t, tc.Expected, python.Venv)
}
}
func TestPythonVirtualEnvIgnoreCustomVenvNames(t *testing.T) {
cases := []struct {
Expected string
FolderNameFallback bool
FallbackNames []string
VirtualEnvName string
}{
{
Expected: "folder",
FolderNameFallback: true,
FallbackNames: []string{"env"},
VirtualEnvName: "/path/to/folder/env",
},
{
Expected: "venv",
FolderNameFallback: true,
FallbackNames: []string{"env"},
VirtualEnvName: "/path/to/folder/venv",
},
}
for _, tc := range cases {
params := &mockedLanguageParams{}
env, props := getMockedLanguageEnv(params)
env.On("GOOS").Return("")
env.On("PathSeparator").Return("/")
env.On("CommandPath", testify_.Anything).Return("")
env.On("HasFilesInDir", testify_.Anything, "pyvenv.cfg").Return(false)
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("")
env.On("HasParentFilePath", ".python-version", false).Return(&runtime.FileInfo{}, errors.New("no match at root level"))
props[FolderNameFallback] = tc.FolderNameFallback
props[FallbackNames] = tc.FallbackNames
python := &Python{}
python.Init(props, env)
python.loadContext()
assert.Equal(t, tc.Expected, python.Venv)
}
}

View file

@ -2653,6 +2653,24 @@
"items": {
"type": "string"
}
},
"folder_name_fallback": {
"type": "boolean",
"title": "Folder Name Fallback",
"description": "Replace virtual environment names in fallback_names list with parent folder name",
"default": "true"
},
"fallback_names": {
"type": "array",
"title": "Fallback Names",
"description": "Names to replace when folder_name_fallback is true",
"default": [
".venv",
"venv"
],
"items": {
"type": "string"
}
}
}
}

View file

@ -38,7 +38,8 @@ import Config from "@site/src/components/Config.js";
| `extensions` | `[]string` | `*.py, *.ipynb, pyproject.toml, venv.bak` | allows to override the default list of file extensions to validate |
| `folders` | `[]string` | | allows to override the list of folder names to validate |
| `cache_version` | `boolean` | `false` | cache the executable's version or not |
| `folder_name_fallback` | `boolean` | `true` | instead of `venv` or `.venv` (case sensitive), use the parent folder name as the virtual environment's name or not |
| `folder_name_fallback` | `boolean` | `true` | instead of `fallback_names` (case sensitive), use the parent folder name as the virtual environment's name or not |
| `fallback_names` | `[]string` | `.venv, venv` | allows to override the list of environment's name replaced when `folder_name_fallback` is `true` |
## Template ([info][templates])