Propagate errors from glob::Pattern::new (#858)

This commit is contained in:
Anders Kaseorg
2022-11-21 10:39:19 -08:00
committed by GitHub
parent 1559671093
commit b657d912d9
5 changed files with 22 additions and 19 deletions

View File

@@ -1,5 +1,6 @@
use std::path::PathBuf;
use anyhow::Result;
use clap::{command, Parser};
use regex::Regex;
use rustc_hash::FxHashMap;
@@ -146,7 +147,7 @@ pub fn extract_log_level(cli: &Cli) -> LogLevel {
pub fn collect_per_file_ignores(
pairs: Vec<PatternPrefixPair>,
project_root: Option<&PathBuf>,
) -> Vec<PerFileIgnore> {
) -> Result<Vec<PerFileIgnore>> {
let mut per_file_ignores: FxHashMap<String, Vec<CheckCodePrefix>> = FxHashMap::default();
for pair in pairs {
per_file_ignores

View File

@@ -203,7 +203,7 @@ mod tests {
let exclude = vec![FilePattern::from_user(
"foo",
Some(&project_root.to_path_buf()),
)];
)?];
let (file_path, file_basename) = extract_path_names(&path)?;
assert!(is_excluded(file_path, file_basename, exclude.iter()));
@@ -211,7 +211,7 @@ mod tests {
let exclude = vec![FilePattern::from_user(
"bar",
Some(&project_root.to_path_buf()),
)];
)?];
let (file_path, file_basename) = extract_path_names(&path)?;
assert!(is_excluded(file_path, file_basename, exclude.iter()));
@@ -221,7 +221,7 @@ mod tests {
let exclude = vec![FilePattern::from_user(
"baz.py",
Some(&project_root.to_path_buf()),
)];
)?];
let (file_path, file_basename) = extract_path_names(&path)?;
assert!(is_excluded(file_path, file_basename, exclude.iter()));
@@ -229,7 +229,7 @@ mod tests {
let exclude = vec![FilePattern::from_user(
"foo/bar",
Some(&project_root.to_path_buf()),
)];
)?];
let (file_path, file_basename) = extract_path_names(&path)?;
assert!(is_excluded(file_path, file_basename, exclude.iter()));
@@ -239,7 +239,7 @@ mod tests {
let exclude = vec![FilePattern::from_user(
"foo/bar/baz.py",
Some(&project_root.to_path_buf()),
)];
)?];
let (file_path, file_basename) = extract_path_names(&path)?;
assert!(is_excluded(file_path, file_basename, exclude.iter()));
@@ -249,7 +249,7 @@ mod tests {
let exclude = vec![FilePattern::from_user(
"foo/bar/*.py",
Some(&project_root.to_path_buf()),
)];
)?];
let (file_path, file_basename) = extract_path_names(&path)?;
assert!(is_excluded(file_path, file_basename, exclude.iter()));
@@ -259,7 +259,7 @@ mod tests {
let exclude = vec![FilePattern::from_user(
"baz",
Some(&project_root.to_path_buf()),
)];
)?];
let (file_path, file_basename) = extract_path_names(&path)?;
assert!(!is_excluded(file_path, file_basename, exclude.iter()));

View File

@@ -219,12 +219,12 @@ fn inner_main() -> Result<ExitCode> {
.exclude
.iter()
.map(|path| FilePattern::from_user(path, project_root.as_ref()))
.collect();
.collect::<Result<_>>()?;
let extend_exclude: Vec<FilePattern> = cli
.extend_exclude
.iter()
.map(|path| FilePattern::from_user(path, project_root.as_ref()))
.collect();
.collect::<Result<_>>()?;
let mut configuration =
Configuration::from_pyproject(pyproject.as_ref(), project_root.as_ref())?;
@@ -236,7 +236,7 @@ fn inner_main() -> Result<ExitCode> {
}
if !cli.per_file_ignores.is_empty() {
configuration.per_file_ignores =
collect_per_file_ignores(cli.per_file_ignores, project_root.as_ref());
collect_per_file_ignores(cli.per_file_ignores, project_root.as_ref())?;
}
if !cli.select.is_empty() {
configuration.select = cli.select;

View File

@@ -111,13 +111,14 @@ impl Configuration {
.map(|path| FilePattern::from_user(path, project_root))
.collect()
})
.transpose()?
.unwrap_or_else(|| DEFAULT_EXCLUDE.clone()),
extend_exclude: options
.extend_exclude
.unwrap_or_default()
.iter()
.map(|path| FilePattern::from_user(path, project_root))
.collect(),
.collect::<Result<_>>()?,
extend_ignore: options.extend_ignore.unwrap_or_default(),
select: options
.select
@@ -159,6 +160,7 @@ impl Configuration {
})
.collect()
})
.transpose()?
.unwrap_or_default(),
show_source: options.show_source.unwrap_or_default(),
// Plugins

View File

@@ -51,21 +51,21 @@ pub enum FilePattern {
}
impl FilePattern {
pub fn from_user(pattern: &str, project_root: Option<&PathBuf>) -> Self {
pub fn from_user(pattern: &str, project_root: Option<&PathBuf>) -> Result<Self> {
let path = Path::new(pattern);
let absolute_path = match project_root {
Some(project_root) => fs::normalize_path_to(path, project_root),
None => fs::normalize_path(path),
};
let absolute = Pattern::new(&absolute_path.to_string_lossy()).expect("Invalid pattern.");
let absolute = Pattern::new(&absolute_path.to_string_lossy())?;
let basename = if !pattern.contains(std::path::MAIN_SEPARATOR) {
Some(Pattern::new(pattern).expect("Invalid pattern."))
Some(Pattern::new(pattern)?)
} else {
None
};
FilePattern::Complex(absolute, basename)
Ok(FilePattern::Complex(absolute, basename))
}
}
@@ -80,10 +80,10 @@ impl PerFileIgnore {
pattern: &str,
prefixes: &[CheckCodePrefix],
project_root: Option<&PathBuf>,
) -> Self {
let pattern = FilePattern::from_user(pattern, project_root);
) -> Result<Self> {
let pattern = FilePattern::from_user(pattern, project_root)?;
let codes = BTreeSet::from_iter(prefixes.iter().flat_map(|prefix| prefix.codes()));
Self { pattern, codes }
Ok(Self { pattern, codes })
}
}