Add interactive integration tests (#208)

This commit is contained in:
sigmaSd 2021-11-16 21:43:17 +01:00 committed by GitHub
parent 5c22bc0060
commit 4d9f5e412f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 1 deletions

View File

@ -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");
}
}

View File

@ -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);