1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-04 19:03:38 +00:00

feat(python): Add detect env vars option (#4486)

feat(python): Configure different detect env vars

Allow the env vars that trigger the python module to be configured, if
an empty list is passed then the module will fall back to just
triggering based on the configured files, folders and extensions.
This commit is contained in:
Thomas O'Donnell 2024-08-18 18:11:13 +02:00 committed by GitHub
parent e47dd5ab25
commit 8a2d944bc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 54 additions and 2 deletions

View File

@ -1396,6 +1396,9 @@
}, },
"python": { "python": {
"default": { "default": {
"detect_env_vars": [
"VIRTUAL_ENV"
],
"detect_extensions": [ "detect_extensions": [
"py" "py"
], ],
@ -5258,6 +5261,15 @@
"items": { "items": {
"type": "string" "type": "string"
} }
},
"detect_env_vars": {
"default": [
"VIRTUAL_ENV"
],
"type": "array",
"items": {
"type": "string"
}
} }
}, },
"additionalProperties": false "additionalProperties": false

View File

@ -3673,6 +3673,7 @@ By default, the module will be shown if any of the following conditions are met:
| `detect_extensions` | `['py']` | Which extensions should trigger this module | | `detect_extensions` | `['py']` | Which extensions should trigger this module |
| `detect_files` | `['.python-version', 'Pipfile', '__init__.py', 'pyproject.toml', 'requirements.txt', 'setup.py', 'tox.ini']` | Which filenames should trigger this module | | `detect_files` | `['.python-version', 'Pipfile', '__init__.py', 'pyproject.toml', 'requirements.txt', 'setup.py', 'tox.ini']` | Which filenames should trigger this module |
| `detect_folders` | `[]` | Which folders should trigger this module | | `detect_folders` | `[]` | Which folders should trigger this module |
| `detect_env_vars` | `["VIRTUAL_ENV"]` | Which environmental variables should trigger this module |
| `disabled` | `false` | Disables the `python` module. | | `disabled` | `false` | Disables the `python` module. |
::: tip ::: tip

View File

@ -21,6 +21,7 @@ pub struct PythonConfig<'a> {
pub detect_extensions: Vec<&'a str>, pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>, pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>, pub detect_folders: Vec<&'a str>,
pub detect_env_vars: Vec<&'a str>,
} }
impl<'a> Default for PythonConfig<'a> { impl<'a> Default for PythonConfig<'a> {
@ -45,6 +46,7 @@ impl<'a> Default for PythonConfig<'a> {
"__init__.py", "__init__.py",
], ],
detect_folders: vec![], detect_folders: vec![],
detect_env_vars: vec!["VIRTUAL_ENV"],
} }
} }
} }

View File

@ -19,9 +19,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.set_folders(&config.detect_folders) .set_folders(&config.detect_folders)
.is_match(); .is_match();
let is_venv = context.get_env("VIRTUAL_ENV").is_some(); let has_env_vars =
!config.detect_env_vars.is_empty() && context.detect_env_vars(&config.detect_env_vars);
if !is_py_project && !is_venv { if !is_py_project && !has_env_vars {
return None; return None;
}; };
@ -371,6 +372,42 @@ Python 3.7.9 (7e6e2bb30ac5fbdbd443619cae28c51d5c162a02, Nov 24 2020, 10:03:59)
dir.close() dir.close()
} }
#[test]
fn with_different_env_var() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let actual = ModuleRenderer::new("python")
.path(dir.path())
.env("MY_ENV_VAR", "my_env_var")
.config(toml::toml! {
[python]
detect_env_vars = ["MY_ENV_VAR"]
})
.collect();
let expected = Some(format!("via {}", Color::Yellow.bold().paint("🐍 v3.8.0 ")));
assert_eq!(actual, expected);
dir.close()
}
#[test]
fn with_no_env_var() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let actual = ModuleRenderer::new("python")
.path(dir.path())
.env("VIRTUAL_ENV", "env_var")
.config(toml::toml! {
[python]
detect_env_vars = []
})
.collect();
assert_eq!(actual, None);
dir.close()
}
#[test] #[test]
fn with_active_venv_and_prompt() -> io::Result<()> { fn with_active_venv_and_prompt() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;