From 73398c2d5081483e83eb9fae690e4932f7386268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Fri, 19 Mar 2021 15:18:32 -0300 Subject: [PATCH] tests: Add tests for extension extraction --- src/cli.rs | 150 ++++++++++++++++++++++------------------------ src/error.rs | 5 +- src/evaluator.rs | 0 src/extensions.rs | 3 + src/file.rs | 40 +------------ src/main.rs | 19 +++--- src/test.rs | 146 ++++++++++++++++++++++++++++---------------- 7 files changed, 182 insertions(+), 181 deletions(-) create mode 100644 src/evaluator.rs diff --git a/src/cli.rs b/src/cli.rs index 75daad7..f8d5c3e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use std::{convert::TryFrom, ffi::OsStr, path::PathBuf, vec::Vec}; +use std::{convert::TryFrom, path::PathBuf, vec::Vec}; use clap::{Arg, Values}; use colored::Colorize; @@ -61,88 +61,78 @@ pub fn get_matches() -> clap::ArgMatches<'static> { // holy spaghetti code impl TryFrom> for Command { - type Error = error::Error; - fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult { + fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult { + let process_decompressible_input = |input_files: Values| { + let input_files = + input_files.map(|filename| (filename, CompressionExtension::try_from(filename))); - let process_decompressible_input = |input_files: Values| { - let input_files = input_files - .map(|filename| (filename, CompressionExtension::try_from(filename))); - - for file in input_files.clone() { - if let (file, Err(_)) = file { - // eprintln!("{}: file '{}' is not decompressible.", "error".red(), file); - return Err(error::Error::InputsMustHaveBeenDecompressible(file.into())); - } - } - - - Ok(input_files - .map(|(filename, extension)| - (PathBuf::from(filename), extension.unwrap()) - ) - .collect::>()) - }; - - // Possibilities: - // * Case 1: output not supplied, therefore try to infer output by checking if all input files are decompressible - // * Case 2: output supplied - - let output_was_supplied = matches.is_present("output"); - - - let input_files = matches - .values_of("input") - .unwrap(); // Safe to unwrap since input is an obligatory argument - - if output_was_supplied { - let output_file = matches - .value_of("output") - .unwrap(); // Safe unwrap since we've established that output was supplied - - let output_file_extension = CompressionExtension::try_from(output_file); - let output_is_compressible = output_file_extension.is_ok(); - if output_is_compressible { - println!("{}: trying to compress input files into '{}'", "info".yellow(), output_file); - - let input_files = input_files.map(PathBuf::from).collect(); - - return Ok( - Command { - command_type: CommandType::Compression(input_files), - output: Some(File::WithExtension( - (output_file.into(), output_file_extension.unwrap()) - )) - } - ); - - } - else { - // Checking if input files are decompressible - - let input_files = process_decompressible_input(input_files)?; - - println!("{}: attempting to decompress input files into {}", "info".yellow(), output_file); - return Ok( - Command { - command_type: CommandType::Decompression(input_files), - output: Some(File::WithoutExtension(output_file.into())) - } - ); + for file in input_files.clone() { + if let (file, Err(_)) = file { + // eprintln!("{}: file '{}' is not decompressible.", "error".red(), file); + return Err(error::Error::InputsMustHaveBeenDecompressible(file.into())); } - } else { - // else: output file not supplied - // Case 1: all input files are decompressible - // Case 2: error - let input_files = process_decompressible_input(input_files)?; - return Ok( - Command { - command_type: CommandType::Decompression(input_files), - output: None - } - ); - } + + Ok(input_files + .map(|(filename, extension)| (PathBuf::from(filename), extension.unwrap())) + .collect::>()) + }; + + // Possibilities: + // * Case 1: output not supplied, therefore try to infer output by checking if all input files are decompressible + // * Case 2: output supplied + + let output_was_supplied = matches.is_present("output"); + + let input_files = matches.values_of("input").unwrap(); // Safe to unwrap since input is an obligatory argument + + if output_was_supplied { + let output_file = matches.value_of("output").unwrap(); // Safe unwrap since we've established that output was supplied + + let output_file_extension = CompressionExtension::try_from(output_file); + let output_is_compressible = output_file_extension.is_ok(); + if output_is_compressible { + println!( + "{}: trying to compress input files into '{}'", + "info".yellow(), + output_file + ); + + let input_files = input_files.map(PathBuf::from).collect(); + + return Ok(Command { + command_type: CommandType::Compression(input_files), + output: Some(File::WithExtension(( + output_file.into(), + output_file_extension.unwrap(), + ))), + }); + } else { + // Checking if input files are decompressible + + let input_files = process_decompressible_input(input_files)?; + + println!( + "{}: attempting to decompress input files into {}", + "info".yellow(), + output_file + ); + return Ok(Command { + command_type: CommandType::Decompression(input_files), + output: Some(File::WithoutExtension(output_file.into())), + }); + } + } else { + // else: output file not supplied + // Case 1: all input files are decompressible + // Case 2: error + let input_files = process_decompressible_input(input_files)?; + return Ok(Command { + command_type: CommandType::Decompression(input_files), + output: None, + }); } -} \ No newline at end of file + } +} diff --git a/src/error.rs b/src/error.rs index 5560315..2ca7502 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use std::{fmt, path::PathBuf}; +use std::fmt; use colored::Colorize; @@ -6,9 +6,10 @@ use colored::Colorize; pub enum Error { UnknownExtensionError(String), MissingExtensionError(String), + // TODO: get rid of this error variant InvalidUnicode, InvalidInput, - InputsMustHaveBeenDecompressible(String) + InputsMustHaveBeenDecompressible(String), } // This should be placed somewhere else diff --git a/src/evaluator.rs b/src/evaluator.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/extensions.rs b/src/extensions.rs index e4afb80..227618e 100644 --- a/src/extensions.rs +++ b/src/extensions.rs @@ -43,6 +43,9 @@ impl TryFrom<&PathBuf> for CompressionExtension { match ext { "zip" => Ok(Zip), "tar" => Ok(Tar), + "gz" => Ok(Gzip), + "bz" => Ok(Bzip), + "lzma" => Ok(Lzma), other => Err(error::Error::UnknownExtensionError(other.into())), } } diff --git a/src/file.rs b/src/file.rs index 89e7399..873c945 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,43 +1,9 @@ -use std::{convert::TryFrom, path::PathBuf, str::FromStr}; +use std::path::PathBuf; -use crate::error::Error as OuchError; -use crate::error; use crate::extensions::CompressionExtension; -// pub type File = (PathBuf, CompressionExtension); - -// #[derive(Debug)] -// pub struct FileWithExtension { -// pub extension: CompressionExtension, -// pub filename: PathBuf, -// } - #[derive(PartialEq, Eq, Debug)] pub enum File { WithExtension((PathBuf, CompressionExtension)), - WithoutExtension(PathBuf) -} - -// impl TryFrom for FileWithExtension { -// type Error = OuchError; - -// fn try_from(filename: String) -> error::OuchResult { -// // Safe to unwrap (infallible operation) -// let filename = PathBuf::from_str(&filename).unwrap(); - -// let os_str = match filename.extension() { -// Some(os_str) => os_str, -// None => return Err(OuchError::MissingExtensionError(filename.to_string_lossy().to_string())), -// }; - -// let extension = match CompressionExtension::try_from(os_str.into()) { -// Ok(ext) => ext, -// Err(err) => return Err(err), -// }; - -// Ok(Self { -// filename, -// extension, -// }) -// } -// } + WithoutExtension(PathBuf), +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e04b98e..3a9b3ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,13 @@ -use std::{convert::TryFrom, fs::File}; - -use cli::get_matches; +use std::convert::TryFrom; mod cli; -mod file; -mod extensions; mod error; +mod extensions; +mod file; mod test; +mod evaluator; fn main() { - // Just testing // let args: Vec = std::env::args().collect(); @@ -20,7 +18,7 @@ fn main() { // Ok((reader, compression)) => {}, // Err(err) => {} // } - + // let (mut reader, compression) = niffler::sniff(Box::new(&file[..])).unwrap(); // match compression { @@ -35,12 +33,13 @@ fn main() { // dbg!(compression); - let matches = get_matches(); + let matches = cli::get_matches(); match cli::Command::try_from(matches) { - Ok(vals) => { dbg!(vals); }, + Ok(vals) => { + dbg!(vals); + } Err(err) => { print!("{}\n", err); } } - } diff --git a/src/test.rs b/src/test.rs index 2dcabe4..30c4159 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,38 +1,23 @@ -use std::{convert::TryFrom}; - - - #[cfg(test)] mod cli { - use std::convert::TryFrom; use crate::cli::clap_app; use crate::cli::Command; - use crate::file::File; use crate::cli::CommandType::*; - use crate::extensions::CompressionExtension::*; use crate::error::OuchResult; - + use crate::extensions::CompressionExtension::*; + use crate::file::File; + use std::convert::TryFrom; #[test] fn decompress_files_into_folder() -> OuchResult<()> { - 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)?; assert_eq!( command_from_matches, Command { - command_type: Decompression( - vec![ - ( - "file.zip".into(), - Zip, - ), - ], - ), + command_type: Decompression(vec![("file.zip".into(), Zip,),],), output: Some(File::WithoutExtension("folder".into())), } ); @@ -42,27 +27,16 @@ mod cli { #[test] fn decompress_files() -> OuchResult<()> { - let matches = clap_app(). - get_matches_from( - vec!["ouch", "-i", "file.zip", "file.tar"] - ); + let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "file.tar"]); let command_from_matches = Command::try_from(matches)?; assert_eq!( command_from_matches, Command { - command_type: Decompression( - vec![ - ( - "file.zip".into(), - Zip, - ), - ( - "file.tar".into(), - Tar, - ), - ], - ), + command_type: Decompression(vec![ + ("file.zip".into(), Zip,), + ("file.tar".into(), Tar,), + ],), output: None, } ); @@ -72,15 +46,27 @@ mod cli { #[test] fn compress_files() -> OuchResult<()> { - let matches = clap_app(). - get_matches_from( - vec!["ouch", "-i", "file", "file2.jpeg", "file3.ok", "-o", "file.tar"] - ); + let matches = clap_app().get_matches_from(vec![ + "ouch", + "-i", + "file", + "file2.jpeg", + "file3.ok", + "-o", + "file.tar", + ]); let command_from_matches = Command::try_from(matches)?; assert_eq!( command_from_matches, - Command { command_type: Compression(vec!["file".into(), "file2.jpeg".into(), "file3.ok".into()]), output: Some(File::WithExtension(("file.tar".into(), Tar))) } + Command { + command_type: Compression(vec![ + "file".into(), + "file2.jpeg".into(), + "file3.ok".into() + ]), + output: Some(File::WithExtension(("file.tar".into(), Tar))) + } ); Ok(()) @@ -89,24 +75,18 @@ mod cli { #[cfg(test)] mod cli_errors { - + use std::convert::TryFrom; use crate::cli::clap_app; use crate::cli::Command; - use crate::file::File; - use crate::cli::CommandType::*; - use crate::extensions::CompressionExtension::*; - use crate::error::OuchResult; use crate::error::Error; - + use crate::error::OuchResult; #[test] fn compress_files() -> OuchResult<()> { - let matches = clap_app(). - get_matches_from( - vec!["ouch", "-i", "a_file", "file2.jpeg", "file3.ok"] - ); + let matches = + clap_app().get_matches_from(vec!["ouch", "-i", "a_file", "file2.jpeg", "file3.ok"]); let res = Command::try_from(matches); assert_eq!( @@ -116,4 +96,66 @@ mod cli_errors { Ok(()) } -} \ No newline at end of file +} + +#[cfg(test)] +mod extension_extraction { + use crate::error::OuchResult; + use crate::extensions::CompressionExtension; + use std::{convert::TryFrom, path::PathBuf, str::FromStr}; + + #[test] + fn zip() -> OuchResult<()> { + let path = PathBuf::from_str("filename.tar.zip").unwrap(); + assert_eq!( + CompressionExtension::try_from(&path)?, + CompressionExtension::Zip + ); + + Ok(()) + } + + #[test] + fn tar() -> OuchResult<()> { + let path = PathBuf::from_str("pictures.tar").unwrap(); + assert_eq!( + CompressionExtension::try_from(&path)?, + CompressionExtension::Tar + ); + + Ok(()) + } + + #[test] + fn gz() -> OuchResult<()> { + let path = PathBuf::from_str("passwords.tar.gz").unwrap(); + assert_eq!( + CompressionExtension::try_from(&path)?, + CompressionExtension::Gzip + ); + + Ok(()) + } + + #[test] + fn lzma() -> OuchResult<()> { + let path = PathBuf::from_str("mygame.tar.lzma").unwrap(); + assert_eq!( + CompressionExtension::try_from(&path)?, + CompressionExtension::Lzma + ); + + Ok(()) + } + + #[test] + fn bz() -> OuchResult<()> { + let path = PathBuf::from_str("songs.tar.bz").unwrap(); + assert_eq!( + CompressionExtension::try_from(&path)?, + CompressionExtension::Bzip + ); + + Ok(()) + } +}