mirror of
https://github.com/astral-sh/ruff
synced 2026-01-24 06:50:59 -05:00
Improve exclusion syntax to match exact files (#209)
This commit is contained in:
47
src/fs.rs
47
src/fs.rs
@@ -1,3 +1,4 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -5,9 +6,11 @@ use std::path::{Path, PathBuf};
|
||||
use anyhow::Result;
|
||||
use glob::Pattern;
|
||||
use log::debug;
|
||||
use path_absolutize::Absolutize;
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
|
||||
fn is_excluded(path: &Path, exclude: &[Pattern]) -> bool {
|
||||
// Check the basename.
|
||||
if let Some(file_name) = path.file_name() {
|
||||
if let Some(file_name) = file_name.to_str() {
|
||||
for pattern in exclude {
|
||||
@@ -15,13 +18,19 @@ fn is_excluded(path: &Path, exclude: &[Pattern]) -> bool {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
||||
// Check the complete path.
|
||||
if let Some(file_name) = path.to_str() {
|
||||
for pattern in exclude {
|
||||
if pattern.matches(file_name) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn is_included(path: &Path) -> bool {
|
||||
@@ -34,7 +43,7 @@ pub fn iter_python_files<'a>(
|
||||
exclude: &'a [Pattern],
|
||||
extend_exclude: &'a [Pattern],
|
||||
) -> impl Iterator<Item = DirEntry> + 'a {
|
||||
WalkDir::new(path)
|
||||
WalkDir::new(normalize_path(path))
|
||||
.follow_links(true)
|
||||
.into_iter()
|
||||
.filter_entry(|entry| {
|
||||
@@ -60,6 +69,20 @@ pub fn iter_python_files<'a>(
|
||||
})
|
||||
}
|
||||
|
||||
pub fn normalize_path(path: &PathBuf) -> PathBuf {
|
||||
if path == Path::new(".") || path == Path::new("..") {
|
||||
return path.clone();
|
||||
}
|
||||
if let Ok(path) = path.absolutize() {
|
||||
if let Ok(root) = env::current_dir() {
|
||||
if let Ok(path) = path.strip_prefix(root) {
|
||||
return Path::new(".").join(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
path.clone()
|
||||
}
|
||||
|
||||
pub fn read_file(path: &Path) -> Result<String> {
|
||||
let file = File::open(path)?;
|
||||
let mut buf_reader = BufReader::new(file);
|
||||
@@ -105,6 +128,18 @@ mod tests {
|
||||
let exclude = vec![Pattern::new("baz.py").unwrap()];
|
||||
assert!(is_excluded(path, &exclude));
|
||||
|
||||
let path = Path::new("foo/bar");
|
||||
let exclude = vec![Pattern::new("foo/bar").unwrap()];
|
||||
assert!(is_excluded(path, &exclude));
|
||||
|
||||
let path = Path::new("foo/bar/baz.py");
|
||||
let exclude = vec![Pattern::new("foo/bar/baz.py").unwrap()];
|
||||
assert!(is_excluded(path, &exclude));
|
||||
|
||||
let path = Path::new("foo/bar/baz.py");
|
||||
let exclude = vec![Pattern::new("foo/bar/*.py").unwrap()];
|
||||
assert!(is_excluded(path, &exclude));
|
||||
|
||||
let path = Path::new("foo/bar/baz.py");
|
||||
let exclude = vec![Pattern::new("baz").unwrap()];
|
||||
assert!(!is_excluded(path, &exclude));
|
||||
|
||||
@@ -261,7 +261,8 @@ other-attribute = 1
|
||||
exclude: None,
|
||||
extend_exclude: Some(vec![
|
||||
Path::new("excluded.py").to_path_buf(),
|
||||
Path::new("migrations").to_path_buf()
|
||||
Path::new("migrations").to_path_buf(),
|
||||
Path::new("./resources/test/fixtures/directory/also_excluded.py").to_path_buf()
|
||||
]),
|
||||
select: Some(vec![
|
||||
CheckCode::E402,
|
||||
|
||||
@@ -24,6 +24,7 @@ impl Hash for Settings {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static DEFAULT_EXCLUDE: Lazy<Vec<Pattern>> = Lazy::new(|| {
|
||||
vec![
|
||||
Pattern::new(".bzr").unwrap(),
|
||||
@@ -57,7 +58,7 @@ impl Settings {
|
||||
.exclude
|
||||
.map(|paths| {
|
||||
paths
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|path| {
|
||||
Pattern::new(&path.to_string_lossy()).expect("Invalid pattern.")
|
||||
})
|
||||
@@ -68,7 +69,7 @@ impl Settings {
|
||||
.extend_exclude
|
||||
.map(|paths| {
|
||||
paths
|
||||
.into_iter()
|
||||
.iter()
|
||||
.map(|path| {
|
||||
Pattern::new(&path.to_string_lossy()).expect("Invalid pattern.")
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user