From a837cff38228d1383b774cd27737af7b2ee29a70 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 20 Oct 2019 13:12:08 +0200 Subject: [PATCH] Add tests for imag-grep Signed-off-by: Matthias Beyer 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); +} +