Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate over files caught by Glob #765

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions globset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ fnv = "1.0"
log = "0.3"
memchr = "2"
regex = "0.2.1"
walkdir = "2.0"

[dev-dependencies]
glob = "0.2"
tempdir = "0.3"

[features]
simd-accel = ["regex/simd-accel"]
105 changes: 104 additions & 1 deletion globset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ extern crate fnv;
extern crate log;
extern crate memchr;
extern crate regex;
extern crate walkdir;

#[cfg(test)]
extern crate tempdir;

use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::error::Error as StdError;
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::hash;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str;

use aho_corasick::{Automaton, AcAutomaton, FullAcAutomaton};
Expand Down Expand Up @@ -417,6 +421,44 @@ impl GlobSet {
],
})
}

/// Iterate files and directories matching this globset at the given directory.
pub fn iter_at<P: AsRef<Path>>(&self, dir: P) -> GlobWalker {
GlobWalker {
glob: self,
base: dir.as_ref().into(),
walker: walkdir::WalkDir::new(dir).into_iter()
}
}
}

/// An iterator for recursively yielding glob matches.
///
/// The order of elements yielded by this iterator is unspecified.
pub struct GlobWalker<'a> {
glob: &'a GlobSet,
base: PathBuf,
walker: walkdir::IntoIter,
}

impl<'a> Iterator for GlobWalker<'a> {
type Item = walkdir::DirEntry;

fn next(&mut self) -> Option<Self::Item> {
for entry in &mut self.walker {
if let Ok(entry) = entry {
// Strip the common base directory so that the matcher will be
// able to recognize the file name.
// `unwrap` here is safe, since walkdir returns the files with relation
// to the given base-dir.
if self.glob.is_match(entry.path().strip_prefix(&*self.base).unwrap()) {
return Some(entry)
}
}
}

None
}
}

/// GlobSetBuilder builds a group of patterns that can be used to
Expand Down Expand Up @@ -804,6 +846,8 @@ impl RequiredExtensionStrategyBuilder {
mod tests {
use super::GlobSetBuilder;
use glob::Glob;
use ::tempdir::TempDir;
use ::std::fs::{File, create_dir_all};

#[test]
fn set_works() {
Expand Down Expand Up @@ -832,4 +876,63 @@ mod tests {
assert!(!set.is_match(""));
assert!(!set.is_match("a"));
}

fn touch(dir: &TempDir, names: &[&str]) {
for name in names {
File::create(dir.path().join(name)).expect("Failed to create a test file");
}
}

#[test]
fn do_the_globwalk() {
let dir = TempDir::new("globset_walkdir").expect("Failed to create temporary folder");
let dir_path = dir.path();
create_dir_all(dir_path.join("src/some_mod")).expect("");
create_dir_all(dir_path.join("tests")).expect("");
create_dir_all(dir_path.join("contrib")).expect("");

touch(&dir, &[
"a.rs",
"b.rs",
"avocado.rs",
"lib.c",
"src/hello.rs",
"src/world.rs",
"src/some_mod/unexpected.rs",
"src/cruel.txt",
"contrib/README.md",
"contrib/README.rst",
"contrib/lib.rs",
][..]);


let mut builder = GlobSetBuilder::new();
builder.add(Glob::new("src/**/*.rs").unwrap());
builder.add(Glob::new("*.c").unwrap());
builder.add(Glob::new("**/lib.rs").unwrap());
builder.add(Glob::new("**/*.{md,rst}").unwrap());
let set = builder.build().unwrap();

let mut expected = vec!["src/some_mod/unexpected.rs",
"src/world.rs",
"src/hello.rs",
"lib.c",
"contrib/lib.rs",
"contrib/README.md",
"contrib/README.rst"];

for matched_file in set.iter_at(dir_path) {
let path = matched_file.path().strip_prefix(dir_path).unwrap().to_str().unwrap();

let del_idx = if let Some(idx) = expected.iter().position(|n| &path == n) {
idx
} else {
panic!("Iterated file is unexpected: {}", path);
};
expected.remove(del_idx);
}

let empty: &[&str] = &[][..];
assert_eq!(expected, empty);
}
}