Fix CLI tests

This commit is contained in:
Vinícius Rodrigues Miguel 2021-03-25 23:33:27 -03:00
parent d99d8e71d3
commit 71be492cd4

View File

@ -10,10 +10,20 @@ mod cli {
use crate::extension::CompressionFormat::*; use crate::extension::CompressionFormat::*;
use crate::extension::Extension; use crate::extension::Extension;
use crate::file::File; use crate::file::File;
use std::convert::TryFrom; use std::{convert::TryFrom, fs, path::Path};
// ouch's command-line logic uses fs::canonicalize on its inputs so we cannot
// use made-up files for testing.
// make_dummy_file therefores creates a small temporary file to bypass fs::canonicalize errors
fn make_dummy_file<'a, P>(path: P) -> OuchResult<()>
where P: AsRef<Path> + 'a {
fs::write(path.as_ref(), &[2, 3, 4, 5, 6, 7, 8, 9, 10])?;
Ok(())
}
#[test] #[test]
fn decompress_files_into_folder() -> OuchResult<()> { fn decompress_files_into_folder() -> OuchResult<()> {
make_dummy_file("file.zip")?;
let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "-o", "folder/"]); let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "-o", "folder/"]);
let command_from_matches = Command::try_from(matches)?; let command_from_matches = Command::try_from(matches)?;
@ -22,7 +32,7 @@ mod cli {
Command { Command {
kind: Decompression(vec![ kind: Decompression(vec![
File { File {
path: "file.zip".into(), path: fs::canonicalize("file.zip")?,
contents_in_memory: None, contents_in_memory: None,
extension: Some(Extension::from(Zip)) extension: Some(Extension::from(Zip))
} }
@ -35,12 +45,16 @@ mod cli {
} }
); );
fs::remove_file("file.zip")?;
Ok(()) Ok(())
} }
#[test] #[test]
fn decompress_files() -> OuchResult<()> { fn decompress_files() -> OuchResult<()> {
let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "file.tar"]); make_dummy_file("my-cool-file.zip")?;
make_dummy_file("file.tar")?;
let matches = clap_app().get_matches_from(vec!["ouch", "-i", "my-cool-file.zip", "file.tar"]);
let command_from_matches = Command::try_from(matches)?; let command_from_matches = Command::try_from(matches)?;
assert_eq!( assert_eq!(
@ -48,12 +62,12 @@ mod cli {
Command { Command {
kind: Decompression(vec![ kind: Decompression(vec![
File { File {
path: "file.zip".into(), path: fs::canonicalize("my-cool-file.zip")?,
contents_in_memory: None, contents_in_memory: None,
extension: Some(Extension::from(Zip)) extension: Some(Extension::from(Zip))
}, },
File { File {
path: "file.tar".into(), path: fs::canonicalize("file.tar")?,
contents_in_memory: None, contents_in_memory: None,
extension: Some(Extension::from(Tar)) extension: Some(Extension::from(Tar))
} }
@ -62,11 +76,19 @@ mod cli {
} }
); );
fs::remove_file("my-cool-file.zip")?;
fs::remove_file("file.tar")?;
Ok(()) Ok(())
} }
#[test] #[test]
fn compress_files() -> OuchResult<()> { fn compress_files() -> OuchResult<()> {
make_dummy_file("file")?;
make_dummy_file("file2.jpeg")?;
make_dummy_file("file3.ok")?;
let matches = clap_app().get_matches_from(vec![ let matches = clap_app().get_matches_from(vec![
"ouch", "ouch",
"-i", "-i",
@ -82,9 +104,9 @@ mod cli {
command_from_matches, command_from_matches,
Command { Command {
kind: Compression(vec![ kind: Compression(vec![
"file".into(), fs::canonicalize("file")?,
"file2.jpeg".into(), fs::canonicalize("file2.jpeg")?,
"file3.ok".into() fs::canonicalize("file3.ok")?
]), ]),
output: Some( output: Some(
File { File {
@ -96,6 +118,10 @@ mod cli {
} }
); );
fs::remove_file("file")?;
fs::remove_file("file2.jpeg")?;
fs::remove_file("file3.ok")?;
Ok(()) Ok(())
} }
} }
@ -201,34 +227,3 @@ mod extension_extraction {
Ok(()) Ok(())
} }
} }
// #[cfg(test)]
// mod evaluator {
// use crate::extension::Extension;
// use crate::error::OuchResult;
// use crate::file::File;
// use crate::evaluator::Evaluator;
// use crate::decompressors::{Decompressor, TarDecompressor, GzipDecompressor};
// #[test]
// fn test() -> OuchResult<()> {
// let extension = Extension::new("folder.tar.gz")?;
// let file = File {
// path: "folder.tar.gz".into(),
// contents_in_memory: None,
// extension: Some(extension),
// };
// let (fst, snd) = Evaluator::get_decompressor(&file)?;
// let fst = fst.unwrap();
// assert_eq!(
// fst,
// Some(Box::new(TarDecompressor::{})
// );
// Ok(())
// }
// }