diff --git a/.github/config-schema.json b/.github/config-schema.json index e3cb4b3a..2733295f 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -1396,6 +1396,9 @@ }, "python": { "default": { + "detect_env_vars": [ + "VIRTUAL_ENV" + ], "detect_extensions": [ "py" ], @@ -5258,6 +5261,15 @@ "items": { "type": "string" } + }, + "detect_env_vars": { + "default": [ + "VIRTUAL_ENV" + ], + "type": "array", + "items": { + "type": "string" + } } }, "additionalProperties": false diff --git a/docs/config/README.md b/docs/config/README.md index 7cc1008d..2246edcc 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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_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_env_vars` | `["VIRTUAL_ENV"]` | Which environmental variables should trigger this module | | `disabled` | `false` | Disables the `python` module. | ::: tip diff --git a/src/configs/python.rs b/src/configs/python.rs index bcfa713d..3654fa82 100644 --- a/src/configs/python.rs +++ b/src/configs/python.rs @@ -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"], } } } diff --git a/src/modules/python.rs b/src/modules/python.rs index 29974163..d531362b 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -19,9 +19,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { .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()?;