Fix --glob behavior with empty pattern

With an empty pattern, --glob results in no matches. This does not
align with the behavior of --fixed-strings or --regex. Update the
initialization code to handle globs with empty patterns.
This commit is contained in:
Seamus Connor 2020-04-26 11:50:32 -07:00 committed by David Peter
parent db6fc44d05
commit b22285ffe4
2 changed files with 29 additions and 14 deletions

View File

@ -122,7 +122,7 @@ fn run() -> Result<ExitCode> {
)); ));
} }
let pattern_regex = if matches.is_present("glob") { let pattern_regex = if matches.is_present("glob") && pattern.len() > 0 {
let glob = GlobBuilder::new(pattern).literal_separator(true).build()?; let glob = GlobBuilder::new(pattern).literal_separator(true).build()?;
glob.regex().to_owned() glob.regex().to_owned()
} else if matches.is_present("fixed-strings") { } else if matches.is_present("fixed-strings") {

View File

@ -52,7 +52,7 @@ fn create_file_with_size<P: AsRef<Path>>(path: P, size_in_bytes: usize) {
f.write_all(content.as_bytes()).unwrap(); f.write_all(content.as_bytes()).unwrap();
} }
/// Simple tests /// Simple test
#[test] #[test]
fn test_simple() { fn test_simple() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES); let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
@ -70,10 +70,13 @@ fn test_simple() {
one/two/three/d.foo one/two/three/d.foo
one/two/three/directory_foo", one/two/three/directory_foo",
); );
}
te.assert_output( /// Test each pattern type with an empty pattern.
&[], #[test]
"a.foo fn test_empty_pattern() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
let expected = "a.foo
e1 e2 e1 e2
one one
one/b.foo one/b.foo
@ -83,7 +86,19 @@ fn test_simple() {
one/two/three one/two/three
one/two/three/d.foo one/two/three/d.foo
one/two/three/directory_foo one/two/three/directory_foo
symlink", symlink";
te.assert_output(
&["--regex"],
expected,
);
te.assert_output(
&["--fixed-strings"],
expected,
);
te.assert_output(
&["--glob"],
expected,
); );
} }