diff --git a/tests/ui/src/imag_grep.rs b/tests/ui/src/imag_grep.rs index 987eadc5..9eac75a7 100644 --- a/tests/ui/src/imag_grep.rs +++ b/tests/ui/src/imag_grep.rs @@ -17,3 +17,103 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::process::Command; +use std::io::Write; + +use assert_fs::fixture::TempDir; + +pub fn call(tempdir: &TempDir, pattern: &str) -> Vec { + let mut binary = binary(tempdir); + binary.arg("--ignore-ids"); + binary.arg(pattern); + + // ensure that stdin is not used by the child process + binary.stdin(std::process::Stdio::inherit()); + crate::imag::stdout_of_command(binary) +} + +pub fn binary(tempdir: &TempDir) -> Command { + crate::imag::binary(tempdir, "imag-grep") +} + +#[test] +fn test_grepping_in_empty_store() { + crate::setup_logging(); + let imag_home = crate::imag::make_temphome(); + crate::imag_init::call(&imag_home); + + let output = call(&imag_home, "something"); + assert_eq!(output[0], "Processed 0 files, 0 matches, 0 nonmatches"); + assert_eq!(output.len(), 1); +} + +#[test] +fn test_grepping_nonempty_store() { + crate::setup_logging(); + let imag_home = crate::imag::make_temphome(); + crate::imag_init::call(&imag_home); + crate::imag_create::call(&imag_home, &["something"]); + + let output = call(&imag_home, "something"); + assert_eq!(output[0], "Processed 1 files, 0 matches, 1 nonmatches"); + assert_eq!(output.len(), 1); +} + +#[test] +fn test_grepping_not_available_string() { + crate::setup_logging(); + let imag_home = crate::imag::make_temphome(); + crate::imag_init::call(&imag_home); + + let filename = &["something"]; + crate::imag_create::call(&imag_home, filename); + let filepath = crate::imag::store_path(&imag_home, filename); + + { + debug!("Appending to file = {}", filepath.display()); + let mut file = ::std::fs::OpenOptions::new() + .append(true) + .create(false) + .open(&filepath) + .unwrap(); + + let _ = writeln!(file, "unavailable").unwrap(); + } + + let output = call(&imag_home, "something"); + assert_eq!(output[0], "Processed 1 files, 0 matches, 1 nonmatches"); + assert_eq!(output.len(), 1); +} + +#[test] +fn test_grepping_available_string() { + crate::setup_logging(); + let imag_home = crate::imag::make_temphome(); + crate::imag_init::call(&imag_home); + + let filename = &["something"]; + crate::imag_create::call(&imag_home, filename); + let filepath = crate::imag::store_path(&imag_home, filename); + + let filetext = "some text is here"; + { + debug!("Appending to file = {}", filepath.display()); + let mut file = ::std::fs::OpenOptions::new() + .append(true) + .create(false) + .open(&filepath) + .unwrap(); + + let _ = writeln!(file, "{}", filetext).unwrap(); + } + + let output = call(&imag_home, filetext); + debug!("output = {:?}", output); + assert!(!output.is_empty()); + assert_eq!(output[0], format!("{}:", filename[0])); + assert_eq!(output[1], format!(" '{}'", filetext)); + assert_eq!(output[2], ""); + assert_eq!(output[3], "Processed 1 files, 1 matches, 0 nonmatches"); + assert_eq!(output.len(), 4); +} +