summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-10-20 13:12:08 +0200
committerMatthias Beyer <mail@beyermatthias.de>2019-11-03 11:30:33 +0100
commita837cff38228d1383b774cd27737af7b2ee29a70 (patch)
tree65dda1f4a788028cb2e67c0d99ef1907c95da55f /tests
parentf1b5b0915b40beca9c34a38f7799fb795682fa0f (diff)
Add tests for imag-grep
Signed-off-by: Matthias Beyer <mail@beyermatthias.deg
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/src/imag_grep.rs100
1 files changed, 100 insertions, 0 deletions
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<String> {
+ 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);
+}
+