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
4 changed files with 54 additions and 2 deletions
+2
View File
@@ -21,6 +21,7 @@ pub struct PythonConfig<'a> {
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
pub detect_env_vars: Vec<&'a str>,
}
impl<'a> Default for PythonConfig<'a> {
@@ -45,6 +46,7 @@ impl<'a> Default for PythonConfig<'a> {
"__init__.py",
],
detect_folders: vec![],
detect_env_vars: vec!["VIRTUAL_ENV"],
}
}
}
+39 -2
View File
@@ -19,9 +19,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.set_folders(&config.detect_folders)
.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;
};
@@ -371,6 +372,42 @@ Python 3.7.9 (7e6e2bb30ac5fbdbd443619cae28c51d5c162a02, Nov 24 2020, 10:03:59)
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]
fn with_active_venv_and_prompt() -> io::Result<()> {
let dir = tempfile::tempdir()?;