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.
This commit is contained in:
Alex Jurkiewicz 2020-04-14 18:26:51 +10:00 committed by GitHub
parent beed0acc6c
commit 965338df95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 11 deletions

View File

@ -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. |

View File

@ -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,
}

View File

@ -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<Module<'a>> {
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<Module<'a>> {
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);

View File

@ -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(())
}