From 965338df95d40d4a17f986efd9ff50863a753451 Mon Sep 17 00:00:00 2001 From: Alex Jurkiewicz Date: Tue, 14 Apr 2020 18:26:51 +1000 Subject: [PATCH] feat(python): Add scan_for_pyfiles option (#692) Also adds two new entries to the list of hardcoded files to check: setup.py and __init__.py. --- docs/config/README.md | 5 ++- src/configs/python.rs | 2 + src/modules/python.rs | 23 +++++++----- tests/testsuite/python.rs | 77 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 11 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 5a87152c..56e1273d 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1106,9 +1106,11 @@ The module will be shown if any of the following conditions are met: - The current directory contains a `.python-version` file - The current directory contains a `requirements.txt` file - The current directory contains a `pyproject.toml` file -- The current directory contains a file with the `.py` extension +- The current directory contains a file with the `.py` extension (and `scan_for_pyfiles` is true) - The current directory contains a `Pipfile` file - The current directory contains a `tox.ini` file +- The current directory contains a `setup.py` file +- The current directory contains a `__init__.py` file - A virtual environment is currently activated ### Options @@ -1118,6 +1120,7 @@ The module will be shown if any of the following conditions are met: | `symbol` | `"🐍 "` | The symbol used before displaying the version of Python. | | `pyenv_version_name` | `false` | Use pyenv to get Python version | | `pyenv_prefix` | `"pyenv "` | Prefix before pyenv version display (default display is `pyenv MY_VERSION`) | +| `scan_for_pyfiles` | `true` | If false, Python files in the current directory will not show this module. | | `style` | `"bold yellow"` | The style for the module. | | `disabled` | `false` | Disables the `python` module. | diff --git a/src/configs/python.rs b/src/configs/python.rs index 736283f4..bd986e91 100644 --- a/src/configs/python.rs +++ b/src/configs/python.rs @@ -9,6 +9,7 @@ pub struct PythonConfig<'a> { pub version: SegmentConfig<'a>, pub pyenv_prefix: SegmentConfig<'a>, pub pyenv_version_name: bool, + pub scan_for_pyfiles: bool, pub style: Style, pub disabled: bool, } @@ -20,6 +21,7 @@ impl<'a> RootModuleConfig<'a> for PythonConfig<'a> { version: SegmentConfig::default(), pyenv_prefix: SegmentConfig::new("pyenv "), pyenv_version_name: false, + scan_for_pyfiles: true, style: Color::Yellow.bold(), disabled: false, } diff --git a/src/modules/python.rs b/src/modules/python.rs index 34a9bca9..9b6b63b7 100644 --- a/src/modules/python.rs +++ b/src/modules/python.rs @@ -15,17 +15,25 @@ use crate::utils; /// - Current directory contains a `Pipfile` file /// - Current directory contains a `tox.ini` file pub fn module<'a>(context: &'a Context) -> Option> { - let is_py_project = context - .try_begin_scan()? - .set_files(&[ + let mut module = context.new_module("python"); + let config: PythonConfig = PythonConfig::try_load(module.config); + + let is_py_project = { + let base = context.try_begin_scan()?.set_files(&[ "requirements.txt", ".python-version", "pyproject.toml", "Pipfile", "tox.ini", - ]) - .set_extensions(&["py"]) - .is_match(); + "setup.py", + "__init__.py", + ]); + if config.scan_for_pyfiles { + base.set_extensions(&["py"]).is_match() + } else { + base.is_match() + } + }; let is_venv = env::var("VIRTUAL_ENV").ok().is_some(); @@ -33,9 +41,6 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let mut module = context.new_module("python"); - let config: PythonConfig = PythonConfig::try_load(module.config); - module.set_style(config.style); module.create_segment("symbol", &config.symbol); diff --git a/tests/testsuite/python.rs b/tests/testsuite/python.rs index bc106700..b667958e 100644 --- a/tests/testsuite/python.rs +++ b/tests/testsuite/python.rs @@ -4,7 +4,7 @@ use std::io; use ansi_term::Color; use tempfile; -use crate::common; +use crate::common::{self, TestCommand}; #[test] #[ignore] @@ -91,6 +91,40 @@ fn folder_with_tox() -> io::Result<()> { dir.close() } +#[test] +#[ignore] +fn folder_with_setup_py() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("setup.py"))?.sync_all()?; + + let output = common::render_module("python") + .arg("--path") + .arg(dir.path()) + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + + let expected = format!("via {} ", Color::Yellow.bold().paint("🐍 v3.7.5")); + assert_eq!(expected, actual); + Ok(()) +} + +#[test] +#[ignore] +fn folder_with_init_py() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("__init__.py"))?.sync_all()?; + + let output = common::render_module("python") + .arg("--path") + .arg(dir.path()) + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + + let expected = format!("via {} ", Color::Yellow.bold().paint("🐍 v3.7.5")); + assert_eq!(expected, actual); + Ok(()) +} + #[test] #[ignore] fn folder_with_py_file() -> io::Result<()> { @@ -141,3 +175,44 @@ fn with_active_venv() -> io::Result<()> { assert_eq!(expected, actual); dir.close() } + +#[test] +fn disabled_scan_for_pyfiles_and_folder_with_ignored_py_file() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("foo.py"))?.sync_all()?; + + let output = common::render_module("python") + .use_config(toml::toml! { + [python] + scan_for_pyfiles = false + }) + .arg("--path") + .arg(dir.path()) + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + + let expected = ""; + assert_eq!(expected, actual); + Ok(()) +} + +#[test] +#[ignore] +fn disabled_scan_for_pyfiles_and_folder_with_setup_py() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("setup.py"))?.sync_all()?; + + let output = common::render_module("python") + .use_config(toml::toml! { + [python] + scan_for_pyfiles = false + }) + .arg("--path") + .arg(dir.path()) + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + + let expected = format!("via {} ", Color::Yellow.bold().paint("🐍 v3.7.5")); + assert_eq!(expected, actual); + Ok(()) +}