Add prettier workspace resolution test

This commit is contained in:
Kirill Bulatov 2023-11-01 11:53:00 +02:00
parent 6a89f7ce4f
commit d673efebd2
11 changed files with 426 additions and 45 deletions

View file

@ -14,6 +14,7 @@ test-support = ["tempdir", "git2"]
[dependencies]
anyhow.workspace = true
backtrace = "0.3"
globset.workspace = true
log.workspace = true
lazy_static.workspace = true
futures.workspace = true

View file

@ -1,5 +1,6 @@
use std::path::{Path, PathBuf};
use globset::{Glob, GlobMatcher};
use serde::{Deserialize, Serialize};
lazy_static::lazy_static! {
@ -189,6 +190,31 @@ impl<P> PathLikeWithPosition<P> {
}
}
#[derive(Clone, Debug)]
pub struct PathMatcher {
maybe_path: PathBuf,
glob: GlobMatcher,
}
impl std::fmt::Display for PathMatcher {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.maybe_path.to_string_lossy().fmt(f)
}
}
impl PathMatcher {
pub fn new(maybe_glob: &str) -> Result<Self, globset::Error> {
Ok(PathMatcher {
glob: Glob::new(&maybe_glob)?.compile_matcher(),
maybe_path: PathBuf::from(maybe_glob),
})
}
pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
other.as_ref().starts_with(&self.maybe_path) || self.glob.is_match(other)
}
}
#[cfg(test)]
mod tests {
use super::*;