From 4d9f5e412f69e60d0b71819f5f0d8181e42e897e Mon Sep 17 00:00:00 2001 From: sigmaSd Date: Tue, 16 Nov 2021 21:43:17 +0100 Subject: [PATCH] Add interactive integration tests (#208) --- tests/integration.rs | 33 ++++++++++++++++++++++++++++++++- tests/utils.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/tests/integration.rs b/tests/integration.rs index 49c2a27..06839c1 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,7 +1,7 @@ #[macro_use] mod utils; -use std::{iter::once, path::PathBuf}; +use std::{io::Write, iter::once, path::PathBuf}; use fs_err as fs; use parse_display::Display; @@ -120,3 +120,34 @@ fn multiple_files( ouch!("d", archive, "-d", after); assert_same_directory(before, after, !matches!(ext, DirectoryExtension::Zip)); } + +#[test] +fn test_compress_decompress() { + let dir = tempdir().unwrap(); + let dir = dir.path(); + let i1 = dir.join("i1"); + std::fs::write(&i1, "ouch").unwrap(); + let o1 = dir.join("o1.tar"); + let out = dir.join("out"); + std::fs::create_dir(&out).unwrap(); + + { + assert!(ouch_interactive!("c", &i1, &dir.join("o1.tar")).0.wait().unwrap().success()); + } + { + let (_ouch, mut sin, sout) = ouch_interactive!("d", &o1, "-d", &out); + assert!(sout.recv().unwrap().starts_with("Do you want to overwrite")); + writeln!(&mut sin, "n").unwrap(); + assert!(!out.join("i1").exists()); + } + { + let (_ouch, mut sin, sout) = ouch_interactive!("d", &o1, "-d", &out); + assert!(sout.recv().unwrap().starts_with("Do you want to overwrite")); + writeln!(&mut sin, "Y").unwrap(); + assert!(sout.recv().unwrap().ends_with("created.")); + assert!(sout.recv().unwrap().ends_with("extracted. (4.00 B)")); + assert!(sout.recv().unwrap().starts_with("[INFO] Successfully decompressed archive")); + assert_eq!(sout.recv().unwrap(), "[INFO] Files unpacked: 1"); + assert_eq!(std::fs::read(&dir.join("out/i1")).unwrap(), b"ouch"); + } +} diff --git a/tests/utils.rs b/tests/utils.rs index c4b7baa..ead068b 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -3,6 +3,7 @@ use std::{io::Write, path::PathBuf}; use fs_err as fs; use rand::RngCore; +/// Run ouch with cargo run #[macro_export] macro_rules! ouch { ($($e:expr),*) => { @@ -13,6 +14,40 @@ macro_rules! ouch { } } +/// Run ouch with cargo run and returns (child process, stdin handle and stdout channel receiver) +#[macro_export] +macro_rules! ouch_interactive { + ($($e:expr),*) => { + { + let mut p = ::std::process::Command::new("cargo") + .stdin(::std::process::Stdio::piped()) + .stdout(::std::process::Stdio::piped()) + .arg("run") + .arg("--") + $(.arg($e))* + .spawn() + .unwrap(); + let sin = p.stdin.take().unwrap(); + let mut sout = p.stdout.take().unwrap(); + + let (tx, rx) = ::std::sync::mpsc::channel(); + ::std::thread::spawn(move ||{ + // This thread/loop is used so we can make the output more deterministic + let mut s = [0; 1024]; + loop { + let n = ::std::io::Read::read(&mut sout, &mut s).unwrap(); + let s = ::std::string::String::from_utf8(s[..n].to_vec()).unwrap(); + for l in s.lines() { + tx.send(l.to_string()).unwrap(); + } + } + }); + + (p, sin, rx) + } + }; +} + // write random content to a file pub fn write_random_content(file: &mut impl Write, rng: &mut impl RngCore) { let data = &mut Vec::with_capacity((rng.next_u32() % 8192) as usize);