feat(python): make parent folder fallback optional

This commit is contained in:
rocky@HP_OMEN 2024-02-28 12:45:40 +00:00 committed by Jan De Dobbeleer
parent 8401da66e5
commit 21df976c2f
3 changed files with 36 additions and 11 deletions

View file

@ -21,6 +21,7 @@ const (
// FetchVirtualEnv fetches the virtual env
FetchVirtualEnv properties.Property = "fetch_virtual_env"
UsePythonVersionFile properties.Property = "use_python_version_file"
FolderNameFallback properties.Property = "folder_name_fallback"
)
func (p *Python) Template() string {
@ -74,6 +75,7 @@ func (p *Python) loadContext() {
"CONDA_DEFAULT_ENV",
}
folderNameFallback := p.language.props.GetBool(FolderNameFallback, true)
defaultVenvNames := []string{
".venv",
"venv",
@ -87,8 +89,7 @@ func (p *Python) loadContext() {
}
name := platform.Base(p.language.env, venv)
if slices.Contains(defaultVenvNames, name) {
if folderNameFallback && slices.Contains(defaultVenvNames, name) {
venv = strings.TrimSuffix(venv, name)
name = platform.Base(p.language.env, venv)
}

View file

@ -150,14 +150,36 @@ func TestPythonPythonInContext(t *testing.T) {
func TestPythonVirtualEnvIgnoreDefaultVenvNames(t *testing.T) {
cases := []struct {
Expected string
FolderNameFallback bool
VirtualEnvName string
}{
{VirtualEnvName: "/path/to/folder/.venv"},
{VirtualEnvName: "/path/to/folder/venv"},
{
Expected: "folder",
FolderNameFallback: true,
VirtualEnvName: "/path/to/folder/.venv",
},
{
Expected: "folder",
FolderNameFallback: true,
VirtualEnvName: "/path/to/folder/venv",
},
{
Expected: ".venv",
FolderNameFallback: false,
VirtualEnvName: "/path/to/folder/.venv",
},
{
Expected: "venv",
FolderNameFallback: false,
VirtualEnvName: "/path/to/folder/venv",
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
params := &mockedLanguageParams{}
env, props := getMockedLanguageEnv(params)
env.On("GOOS").Return("")
env.On("PathSeparator").Return("/")
env.On("CommandPath", mock2.Anything).Return("")
@ -167,9 +189,12 @@ func TestPythonVirtualEnvIgnoreDefaultVenvNames(t *testing.T) {
env.On("Getenv", "CONDA_DEFAULT_ENV").Return("")
env.On("Getenv", "PYENV_VERSION").Return("")
env.On("HasParentFilePath", ".python-version").Return(&platform.FileInfo{}, errors.New("no match at root level"))
props[FolderNameFallback] = tc.FolderNameFallback
python := &Python{}
python.Init(properties.Map{}, env)
python.Init(props, env)
python.loadContext()
assert.Equal(t, "folder", python.Venv)
assert.Equal(t, tc.Expected, python.Venv)
}
}

View file

@ -9,8 +9,6 @@ sidebar_label: Python
Display the currently active python version and virtualenv.
Supports conda, virtualenv and pyenv (if python points to pyenv shim).
If your virtual environment is a directory named `venv` or `.venv`, the virtual environment's name will be the parent directory.
## Sample Configuration
import Config from "@site/src/components/Config.js";
@ -40,6 +38,7 @@ import Config from "@site/src/components/Config.js";
| `extensions` | `[]string` | 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` | cache the executable's version or not - defaults to `false` |
| `folder_name_fallback` | `boolean` | instead of `venv` or `.venv` (case sensitive), use the parent folder name as the virtual environment's name or not - defaults to `true` |
## Template ([info][templates])