Skip to content

Commit 26982d5

Browse files
committed
improve coverage
1 parent 5909991 commit 26982d5

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/locate/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
fn main() {
88
let args = std::env::args().collect::<Vec<String>>();
99
let strs: Vec<&str> = args.iter().map(std::convert::AsRef::as_ref).collect();
10+
#[cfg(unix)]
1011
std::process::exit(findutils::locate::locate_main(strs.as_slice()));
1112
}

src/locate/mod.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ impl Statistics {
145145
}
146146
}
147147

148+
#[derive(Debug)]
148149
enum Patterns {
149150
String(Vec<String>),
150151
Regex(Vec<Regex>),
@@ -154,14 +155,14 @@ impl Patterns {
154155
fn any_match(&self, entry: &str) -> bool {
155156
match self {
156157
Self::String(v) => v.iter().any(|s| entry.contains(s)),
157-
Self::Regex(v) => v.iter().any(|r| r.is_match(entry)),
158+
Self::Regex(v) => v.iter().any(|r| r.find(entry).is_some()),
158159
}
159160
}
160161

161162
fn all_match(&self, entry: &str) -> bool {
162163
match self {
163164
Self::String(v) => v.iter().all(|s| entry.contains(s)),
164-
Self::Regex(v) => v.iter().all(|r| r.is_match(entry)),
165+
Self::Regex(v) => v.iter().all(|r| r.find(entry).is_some()),
165166
}
166167
}
167168
}
@@ -234,15 +235,12 @@ impl From<ArgMatches> for ParsedInfo {
234235
.unwrap()
235236
.cloned()
236237
.collect();
237-
let patterns = if let Some(ty) = value
238-
.get_flag("regex")
239-
.then(|| {
240-
value
241-
.get_one::<String>("regextype")
242-
.and_then(|s| RegexType::from_str(s.as_str()).ok())
243-
})
244-
.flatten()
245-
{
238+
let patterns = if let Some(ty) = value.get_flag("regex").then(|| {
239+
value
240+
.get_one::<String>("regextype")
241+
.and_then(|s| RegexType::from_str(s.as_str()).ok())
242+
.unwrap_or(RegexType::Emacs)
243+
}) {
246244
Patterns::Regex(
247245
patterns
248246
.into_iter()
@@ -638,7 +636,7 @@ pub fn locate_main(args: &[&str]) -> i32 {
638636
Error::NoMatches => {}
639637
_ => writeln!(&mut stderr(), "Error: {e}").unwrap(),
640638
}
641-
1
639+
e.code()
642640
}
643641
}
644642
}

tests/db_tests.rs

+36
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,42 @@ mod tests {
6868
.failure();
6969
}
7070

71+
#[test]
72+
fn test_locate_statistics() {
73+
Command::cargo_bin("locate")
74+
.expect("couldn't find locate binary")
75+
.args(["abbbc", "--statistics", "--database=test_data_db"])
76+
.assert()
77+
.success();
78+
}
79+
80+
#[test]
81+
fn test_locate_regex() {
82+
Command::cargo_bin("locate")
83+
.expect("couldn't find locate binary")
84+
.args(["abbbc", "--regex", "--database=test_data_db"])
85+
.assert()
86+
.success();
87+
}
88+
89+
#[test]
90+
fn test_locate_all() {
91+
Command::cargo_bin("locate")
92+
.expect("couldn't find locate binary")
93+
.args(["abb", "bbc", "--regex", "--database=test_data_db"])
94+
.assert()
95+
.success();
96+
}
97+
98+
#[test]
99+
fn test_locate_all_regex() {
100+
Command::cargo_bin("locate")
101+
.expect("couldn't find locate binary")
102+
.args(["abb", "b*c", "--regex", "--database=test_data_db"])
103+
.assert()
104+
.success();
105+
}
106+
71107
#[test]
72108
fn test_updatedb() {
73109
Command::cargo_bin("updatedb")

0 commit comments

Comments
 (0)