diff --git a/src/find/matchers/mod.rs b/src/find/matchers/mod.rs index d412edd..0f13959 100644 --- a/src/find/matchers/mod.rs +++ b/src/find/matchers/mod.rs @@ -375,7 +375,6 @@ fn build_matcher_tree(args: &[&str], mod tests { use walkdir::{DirEntry, WalkDir}; use find::Config; - use find::tests::fix_up_slashes; use find::tests::FakeDependencies; use super::*; @@ -385,7 +384,7 @@ mod tests { /// probably be a string starting with "test_data/" (cargo's tests run with /// a working directory set to the root findutils folder). pub fn get_dir_entry_for(directory: &str, filename: &str) -> DirEntry { - for wrapped_dir_entry in WalkDir::new(fix_up_slashes(directory)) { + for wrapped_dir_entry in WalkDir::new(directory) { let dir_entry = wrapped_dir_entry.unwrap(); if dir_entry.file_name().to_string_lossy() == filename { return dir_entry; @@ -405,8 +404,7 @@ mod tests { assert!(matcher.matches(&abbbc_lower, &mut deps.new_matcher_io())); assert!(!matcher.matches(&abbbc_upper, &mut deps.new_matcher_io())); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/simple/abbbc\n"); } #[test] @@ -421,7 +419,7 @@ mod tests { assert!(matcher.matches(&abbbc_lower, &mut deps.new_matcher_io())); assert!(matcher.matches(&abbbc_upper, &mut deps.new_matcher_io())); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n./test_data/simple/subdir/ABBBC\n")); + "./test_data/simple/abbbc\n./test_data/simple/subdir/ABBBC\n"); } #[test] @@ -435,8 +433,7 @@ mod tests { .unwrap(); assert!(matcher.matches(&abbbc_lower, &mut deps.new_matcher_io())); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/simple/abbbc\n"); } } @@ -524,8 +521,7 @@ mod tests { // build a matcher using an explicit -a argument let matcher = build_top_level_matcher(&["-true", "-a", "-true"], &mut config).unwrap(); assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io())); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/simple/abbbc\n"); } #[test] @@ -540,8 +536,7 @@ mod tests { let matcher = build_top_level_matcher(args, &mut config).unwrap(); assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io())); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/simple/abbbc\n"); } let mut config = Config::default(); @@ -572,8 +567,7 @@ mod tests { let matcher = build_top_level_matcher(&["-true", "-true"], &mut config).unwrap(); assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io())); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/simple/abbbc\n"); } #[test] @@ -589,7 +583,7 @@ mod tests { assert!(!matcher.matches(&abbbc, &mut deps.new_matcher_io())); // two print matchers means doubled output assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n./test_data/simple/abbbc\n")); + "./test_data/simple/abbbc\n./test_data/simple/abbbc\n"); } #[test] diff --git a/src/find/matchers/perm.rs b/src/find/matchers/perm.rs index 90cedc3..defad4b 100644 --- a/src/find/matchers/perm.rs +++ b/src/find/matchers/perm.rs @@ -10,7 +10,6 @@ use std::error::Error; use std::io::{stderr, Write}; -#[cfg(unix)] use std::str::FromStr; use walkdir::DirEntry; @@ -18,7 +17,6 @@ use find::matchers::{Matcher, MatcherIO}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] -#[cfg(unix)] pub enum ComparisonType { /// mode bits have to match exactly Exact, @@ -29,7 +27,6 @@ pub enum ComparisonType { AnyOf, } -#[cfg(unix)] impl FromStr for ComparisonType { type Err = Box; fn from_str(s: &str) -> Result> { @@ -46,7 +43,6 @@ impl FromStr for ComparisonType { } } -#[cfg(unix)] impl ComparisonType { fn mode_bits_match(&self, pattern: u32, value: u32) -> bool { match *self { @@ -57,7 +53,6 @@ impl ComparisonType { } } -#[cfg(unix)] mod parsing { use regex::Regex; use std::error::Error; @@ -260,15 +255,11 @@ mod parsing { } } -#[cfg(unix)] pub struct PermMatcher { pattern: u32, comparison_type: ComparisonType, } -#[cfg(not(unix))] -pub struct PermMatcher {} - impl PermMatcher { #[cfg(unix)] pub fn new(pattern: &str) -> Result> { @@ -280,8 +271,8 @@ impl PermMatcher { } #[cfg(not(unix))] - pub fn new(_dummy_pattern: &str) -> Result> { - Err(From::from("Permission matching is not available on this platform")) + pub fn new(pattern: &str) -> Result> { + Err(From("Permission matching is not available on this platform")) } pub fn new_box(pattern: &str) -> Result, Box> { @@ -309,17 +300,14 @@ impl Matcher for PermMatcher { } #[cfg(not(unix))] - fn matches(&self, _dummy_file_info: &DirEntry, _: &mut MatcherIO) -> bool { - writeln!(&mut stderr(), - "Permission matching not available on this platform!") - .unwrap(); + fn matches(&self, file_info: &DirEntry, _: &mut MatcherIO) -> bool { + stderr().write("Permission matching not available on this platform!"); return false; } } #[cfg(test)] -#[cfg(unix)] mod tests { use find::matchers::Matcher; use find::matchers::tests::get_dir_entry_for; @@ -487,6 +475,7 @@ mod tests { } #[test] + #[cfg(unix)] fn perm_matches() { let file_info = get_dir_entry_for("test_data/simple", "abbbc"); let deps = FakeDependencies::new(); diff --git a/src/find/matchers/printer.rs b/src/find/matchers/printer.rs index 1926315..1a67d47 100644 --- a/src/find/matchers/printer.rs +++ b/src/find/matchers/printer.rs @@ -41,7 +41,6 @@ mod tests { use find::matchers::tests::get_dir_entry_for; use find::matchers::Matcher; use find::tests::FakeDependencies; - use find::tests::fix_up_slashes; use super::*; #[test] @@ -51,7 +50,6 @@ mod tests { let matcher = Printer::new(); let deps = FakeDependencies::new(); assert!(matcher.matches(&abbbc, &mut deps.new_matcher_io())); - assert_eq!(fix_up_slashes("./test_data/simple/abbbc\n"), - deps.get_output_as_string()); + assert_eq!("./test_data/simple/abbbc\n", deps.get_output_as_string()); } } diff --git a/src/find/matchers/time.rs b/src/find/matchers/time.rs index 62e4f55..5b63b9c 100644 --- a/src/find/matchers/time.rs +++ b/src/find/matchers/time.rs @@ -6,7 +6,7 @@ use std; use std::error::Error; -use std::fs::{self, Metadata}; +use std::fs::{File, Metadata}; use std::io::{stderr, Write}; use std::time::SystemTime; use walkdir::DirEntry; @@ -22,7 +22,8 @@ pub struct NewerMatcher { impl NewerMatcher { pub fn new(path_to_file: &str) -> Result> { - let metadata = fs::metadata(path_to_file)?; + let f = File::open(path_to_file)?; + let metadata = f.metadata()?; Ok(NewerMatcher { given_modification_time: metadata.modified()? }) } diff --git a/src/find/mod.rs b/src/find/mod.rs index 2374974..f1c5e58 100644 --- a/src/find/mod.rs +++ b/src/find/mod.rs @@ -219,18 +219,6 @@ mod tests { use super::*; - #[cfg(windows)] - /// Windows-only bodge for converting between path separators. - pub fn fix_up_slashes(path: &str) -> String { - path.replace("/", "\\") - } - - #[cfg(not(windows))] - /// Do nothing equivalent of the above for non-windows systems. - pub fn fix_up_slashes(path: &str) -> String { - path.to_string() - } - /// A struct that implements Dependencies, but uses faked implementations, /// allowing us to check output, set the time returned by clocks etc. pub struct FakeDependencies { @@ -279,15 +267,14 @@ mod tests { let deps = FakeDependencies::new(); - let rc = find_main(&["find", &fix_up_slashes("./test_data/simple"), "-sorted"], - &deps); + let rc = find_main(&["find", "./test_data/simple", "-sorted"], &deps); assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple\n\ + "./test_data/simple\n\ ./test_data/simple/abbbc\n\ ./test_data/simple/subdir\n\ - ./test_data/simple/subdir/ABBBC\n")); + ./test_data/simple/subdir/ABBBC\n"); } #[test] @@ -295,53 +282,46 @@ mod tests { let deps = FakeDependencies::new(); - let rc = find_main(&["find", &fix_up_slashes("./test_data/simple"), "-sorted", "-depth"], - &deps); + let rc = find_main(&["find", "./test_data/simple", "-sorted", "-depth"], &deps); assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/abbbc\n\ + "./test_data/simple/abbbc\n\ ./test_data/simple/subdir/ABBBC\n\ ./test_data/simple/subdir\n\ - ./test_data/simple\n")); + ./test_data/simple\n"); } #[test] fn find_maxdepth() { let deps = FakeDependencies::new(); - let rc = - find_main(&["find", &fix_up_slashes("./test_data/depth"), "-sorted", "-maxdepth", "2"], - &deps); + let rc = find_main(&["find", "./test_data/depth", "-sorted", "-maxdepth", "2"], + &deps); assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/depth\n\ + "./test_data/depth\n\ ./test_data/depth/1\n\ ./test_data/depth/1/2\n\ ./test_data/depth/1/f1\n\ - ./test_data/depth/f0\n")); + ./test_data/depth/f0\n"); } #[test] fn find_maxdepth_depth_first() { let deps = FakeDependencies::new(); - let rc = find_main(&["find", - &fix_up_slashes("./test_data/depth"), - "-sorted", - "-maxdepth", - "2", - "-depth"], + let rc = find_main(&["find", "./test_data/depth", "-sorted", "-maxdepth", "2", "-depth"], &deps); assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/depth/1/2\n\ + "./test_data/depth/1/2\n\ ./test_data/depth/1/f1\n\ ./test_data/depth/1\n\ ./test_data/depth/f0\n\ - ./test_data/depth\n")); + ./test_data/depth\n"); } #[test] @@ -349,7 +329,7 @@ mod tests { let deps = FakeDependencies::new(); let rc = find_main(&["find", - &fix_up_slashes("./test_data/depth"), + "./test_data/depth", "-sorted", "-print", ",", @@ -360,64 +340,54 @@ mod tests { assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/depth\n\ + "./test_data/depth\n\ ./test_data/depth/1\n\ - ./test_data/depth/f0\n")); + ./test_data/depth/f0\n"); } #[test] fn find_zero_maxdepth() { let deps = FakeDependencies::new(); - let rc = find_main(&["find", &fix_up_slashes("./test_data/depth"), "-maxdepth", "0"], - &deps); + let rc = find_main(&["find", "./test_data/depth", "-maxdepth", "0"], &deps); assert_eq!(rc, 0); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/depth\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/depth\n"); } #[test] fn find_zero_maxdepth_depth_first() { let deps = FakeDependencies::new(); - let rc = - find_main(&["find", &fix_up_slashes("./test_data/depth"), "-maxdepth", "0", "-depth"], - &deps); + let rc = find_main(&["find", "./test_data/depth", "-maxdepth", "0", "-depth"], + &deps); assert_eq!(rc, 0); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/depth\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/depth\n"); } #[test] fn find_mindepth() { let deps = FakeDependencies::new(); - let rc = - find_main(&["find", &fix_up_slashes("./test_data/depth"), "-sorted", "-mindepth", "3"], - &deps); + let rc = find_main(&["find", "./test_data/depth", "-sorted", "-mindepth", "3"], + &deps); assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/depth/1/2/3\n\ + "./test_data/depth/1/2/3\n\ ./test_data/depth/1/2/3/f3\n\ - ./test_data/depth/1/2/f2\n")); + ./test_data/depth/1/2/f2\n"); } #[test] fn find_mindepth_depth_first() { let deps = FakeDependencies::new(); - let rc = find_main(&["find", - &fix_up_slashes("./test_data/depth"), - "-sorted", - "-mindepth", - "3", - "-depth"], + let rc = find_main(&["find", "./test_data/depth", "-sorted", "-mindepth", "3", "-depth"], &deps); assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/depth/1/2/3/f3\n\ + "./test_data/depth/1/2/3/f3\n\ ./test_data/depth/1/2/3\n\ - ./test_data/depth/1/2/f2\n")); + ./test_data/depth/1/2/f2\n"); } #[test] @@ -432,7 +402,7 @@ mod tests { let rc = find_main(&["find", &new_dir.path().to_string_lossy(), "-newer", - &fix_up_slashes("./test_data/simple/abbbc")], + "./test_data/simple/abbbc"], &deps); assert_eq!(rc, 0); @@ -442,7 +412,7 @@ mod tests { // now do it the other way around, and nothing should be output let deps = FakeDependencies::new(); let rc = find_main(&["find", - &fix_up_slashes("./test_data/simple/abbbc"), + "./test_data/simple/abbbc", "-newer", &new_dir.path().to_string_lossy()], &deps); @@ -491,17 +461,12 @@ mod tests { let mut deps = FakeDependencies::new(); deps.set_time(file_time); - let rc = find_main(&["find", - &fix_up_slashes("./test_data/simple/subdir"), - "-type", - "f", - arg, - "0"], + let rc = find_main(&["find", "./test_data/simple/subdir", "-type", "f", arg, "0"], &deps); assert_eq!(rc, 0); assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/simple/subdir/ABBBC\n")); + "./test_data/simple/subdir/ABBBC\n"); } // now Check file time doesn't match a file that's too new @@ -522,13 +487,11 @@ mod tests { let deps = FakeDependencies::new(); // only look at files because the "size" of a directory is a system (and filesystem) // dependent thing and we want these tests to be universal. - let rc = - find_main(&["find", &fix_up_slashes("./test_data/size"), "-type", "f", "-size", "1b"], - &deps); + let rc = find_main(&["find", "./test_data/size", "-type", "f", "-size", "1b"], + &deps); assert_eq!(rc, 0); - assert_eq!(deps.get_output_as_string(), - fix_up_slashes("./test_data/size/512bytes\n")); + assert_eq!(deps.get_output_as_string(), "./test_data/size/512bytes\n"); let deps = FakeDependencies::new(); let rc = find_main(&["find", "./test_data/size", "-type", "f", "-size", "+1b"], diff --git a/src/testing/commandline/main.rs b/src/testing/commandline/main.rs index 2d813ff..bfb0b73 100644 --- a/src/testing/commandline/main.rs +++ b/src/testing/commandline/main.rs @@ -62,13 +62,10 @@ fn main() { // first two args are going to be the path to this executable and // the destination_dir we want to write to. Don't write either of those // as they'll be non-deterministic. - f.write_fmt(format_args!("cwd={}\nargs=\n", - env::current_dir().unwrap().to_string_lossy())) + f.write_fmt(format_args!("cwd={}\nargs={:?}\n", + env::current_dir().unwrap().to_string_lossy(), + &args[2..])) .expect("failed to write to file"); - for arg in &args[2..] { - f.write_fmt(format_args!("{}\n", arg)).expect("failed to write to file"); - } - } std::process::exit(if config.exit_with_failure { 2 } else { 0 }); } diff --git a/tests/common/test_helpers.rs b/tests/common/test_helpers.rs index 4c2680a..11dc332 100644 --- a/tests/common/test_helpers.rs +++ b/tests/common/test_helpers.rs @@ -71,24 +71,12 @@ pub fn path_to_testing_commandline() -> String { .to_string() } -#[cfg(windows)] -/// A copy of find::tests::fix_up_slashes. -/// TODO: find out how to share #[cfg(test)] functions/structs between unit -/// and integration tests. -pub fn fix_up_slashes(path: &str) -> String { - path.replace("/", "\\") -} - -#[cfg(not(windows))] -pub fn fix_up_slashes(path: &str) -> String { - path.to_string() -} /// A copy of find::tests::FakeDependencies. /// TODO: find out how to share #[cfg(test)] functions/structs between unit /// and integration tests. pub fn get_dir_entry_for(directory: &str, filename: &str) -> DirEntry { - for wrapped_dir_entry in WalkDir::new(fix_up_slashes(directory)) { + for wrapped_dir_entry in WalkDir::new(directory) { let dir_entry = wrapped_dir_entry.unwrap(); if dir_entry.file_name().to_string_lossy() == filename { return dir_entry; diff --git a/tests/exec_unit_tests.rs b/tests/exec_unit_tests.rs index f4a6152..7f4e920 100644 --- a/tests/exec_unit_tests.rs +++ b/tests/exec_unit_tests.rs @@ -44,8 +44,8 @@ fn matching_executes_code() { let mut s = String::new(); f.read_to_string(&mut s).expect("failed to read output file"); assert_eq!(s, - fix_up_slashes(&format!("cwd={}\nargs=\nabc\ntest_data/simple/abbbc\nxyz\n", - env::current_dir().unwrap().to_string_lossy()))); + format!("cwd={}\nargs=[\"abc\", \"test_data/simple/abbbc\", \"xyz\"]\n", + env::current_dir().unwrap().to_string_lossy())); } #[test] @@ -66,8 +66,8 @@ fn matching_executes_code_in_files_directory() { let mut s = String::new(); f.read_to_string(&mut s).expect("failed to read output file"); assert_eq!(s, - fix_up_slashes(&format!("cwd={}/test_data/simple\nargs=\nabc\n./abbbc\nxyz\n", - env::current_dir().unwrap().to_string_lossy()))); + format!("cwd={}/test_data/simple\nargs=[\"abc\", \"./abbbc\", \"xyz\"]\n", + env::current_dir().unwrap().to_string_lossy())); } #[test] @@ -92,7 +92,7 @@ fn matching_fails_if_executable_fails() { let mut s = String::new(); f.read_to_string(&mut s).expect("failed to read output file"); assert_eq!(s, - fix_up_slashes(&format!("cwd={}/test_data/simple\nargs=\n--exit_with_failure\nabc\n.\ - /abbbc\nxyz\n", - env::current_dir().unwrap().to_string_lossy()))); + format!("cwd={}/test_data/simple\nargs=[\"--exit_with_failure\", \"abc\", \ + \"./abbbc\", \"xyz\"]\n", + env::current_dir().unwrap().to_string_lossy())); } diff --git a/tests/find_exec_tests.rs b/tests/find_exec_tests.rs index f24e434..2443519 100644 --- a/tests/find_exec_tests.rs +++ b/tests/find_exec_tests.rs @@ -31,7 +31,7 @@ fn find_exec() { let deps = FakeDependencies::new(); let rc = find_main(&["find", - &fix_up_slashes("./test_data/simple/subdir"), + "./test_data/simple/subdir", "-type", "f", "-exec", @@ -53,8 +53,8 @@ fn find_exec() { let mut s = String::new(); f.read_to_string(&mut s).expect("failed to read output file"); assert_eq!(s, - fix_up_slashes(&format!("cwd={}\nargs=\n(\n./test_data/simple/subdir/ABBBC\n-o\n", - env::current_dir().unwrap().to_string_lossy()))); + format!("cwd={}\nargs=[\"(\", \"./test_data/simple/subdir/ABBBC\", \"-o\"]\n", + env::current_dir().unwrap().to_string_lossy())); } #[test] @@ -65,7 +65,7 @@ fn find_execdir() { // only look at files because the "size" of a directory is a system (and filesystem) // dependent thing and we want these tests to be universal. let rc = find_main(&["find", - &fix_up_slashes("./test_data/simple/subdir"), + "./test_data/simple/subdir", "-type", "f", "-execdir", @@ -87,7 +87,7 @@ fn find_execdir() { let mut s = String::new(); f.read_to_string(&mut s).expect("failed to read output file"); assert_eq!(s, - fix_up_slashes(&format!("cwd={}/test_data/simple/subdir\nargs=\n)\n./ABBBC\n,\n", - env::current_dir().unwrap().to_string_lossy()))); + format!("cwd={}/test_data/simple/subdir\nargs=[\")\", \"./ABBBC\", \",\"]\n", + env::current_dir().unwrap().to_string_lossy())); }