mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 19:45:29 +00:00
tests: Add tests for extension extraction
This commit is contained in:
parent
e04aba8d53
commit
73398c2d50
68
src/cli.rs
68
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 clap::{Arg, Values};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
@ -61,14 +61,12 @@ pub fn get_matches() -> clap::ArgMatches<'static> {
|
|||||||
|
|
||||||
// holy spaghetti code
|
// holy spaghetti code
|
||||||
impl TryFrom<clap::ArgMatches<'static>> for Command {
|
impl TryFrom<clap::ArgMatches<'static>> for Command {
|
||||||
|
|
||||||
type Error = error::Error;
|
type Error = error::Error;
|
||||||
|
|
||||||
fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult<Command> {
|
fn try_from(matches: clap::ArgMatches<'static>) -> error::OuchResult<Command> {
|
||||||
|
|
||||||
let process_decompressible_input = |input_files: Values| {
|
let process_decompressible_input = |input_files: Values| {
|
||||||
let input_files = input_files
|
let input_files =
|
||||||
.map(|filename| (filename, CompressionExtension::try_from(filename)));
|
input_files.map(|filename| (filename, CompressionExtension::try_from(filename)));
|
||||||
|
|
||||||
for file in input_files.clone() {
|
for file in input_files.clone() {
|
||||||
if let (file, Err(_)) = file {
|
if let (file, Err(_)) = file {
|
||||||
@ -77,11 +75,8 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Ok(input_files
|
Ok(input_files
|
||||||
.map(|(filename, extension)|
|
.map(|(filename, extension)| (PathBuf::from(filename), extension.unwrap()))
|
||||||
(PathBuf::from(filename), extension.unwrap())
|
|
||||||
)
|
|
||||||
.collect::<Vec<_>>())
|
.collect::<Vec<_>>())
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -91,58 +86,53 @@ impl TryFrom<clap::ArgMatches<'static>> for Command {
|
|||||||
|
|
||||||
let output_was_supplied = matches.is_present("output");
|
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
|
||||||
let input_files = matches
|
|
||||||
.values_of("input")
|
|
||||||
.unwrap(); // Safe to unwrap since input is an obligatory argument
|
|
||||||
|
|
||||||
if output_was_supplied {
|
if output_was_supplied {
|
||||||
let output_file = matches
|
let output_file = matches.value_of("output").unwrap(); // Safe unwrap since we've established that output was supplied
|
||||||
.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_file_extension = CompressionExtension::try_from(output_file);
|
||||||
let output_is_compressible = output_file_extension.is_ok();
|
let output_is_compressible = output_file_extension.is_ok();
|
||||||
if output_is_compressible {
|
if output_is_compressible {
|
||||||
println!("{}: trying to compress input files into '{}'", "info".yellow(), output_file);
|
println!(
|
||||||
|
"{}: trying to compress input files into '{}'",
|
||||||
|
"info".yellow(),
|
||||||
|
output_file
|
||||||
|
);
|
||||||
|
|
||||||
let input_files = input_files.map(PathBuf::from).collect();
|
let input_files = input_files.map(PathBuf::from).collect();
|
||||||
|
|
||||||
return Ok(
|
return Ok(Command {
|
||||||
Command {
|
|
||||||
command_type: CommandType::Compression(input_files),
|
command_type: CommandType::Compression(input_files),
|
||||||
output: Some(File::WithExtension(
|
output: Some(File::WithExtension((
|
||||||
(output_file.into(), output_file_extension.unwrap())
|
output_file.into(),
|
||||||
))
|
output_file_extension.unwrap(),
|
||||||
}
|
))),
|
||||||
);
|
});
|
||||||
|
} else {
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Checking if input files are decompressible
|
// Checking if input files are decompressible
|
||||||
|
|
||||||
let input_files = process_decompressible_input(input_files)?;
|
let input_files = process_decompressible_input(input_files)?;
|
||||||
|
|
||||||
println!("{}: attempting to decompress input files into {}", "info".yellow(), output_file);
|
println!(
|
||||||
return Ok(
|
"{}: attempting to decompress input files into {}",
|
||||||
Command {
|
"info".yellow(),
|
||||||
command_type: CommandType::Decompression(input_files),
|
output_file
|
||||||
output: Some(File::WithoutExtension(output_file.into()))
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
return Ok(Command {
|
||||||
|
command_type: CommandType::Decompression(input_files),
|
||||||
|
output: Some(File::WithoutExtension(output_file.into())),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// else: output file not supplied
|
// else: output file not supplied
|
||||||
// Case 1: all input files are decompressible
|
// Case 1: all input files are decompressible
|
||||||
// Case 2: error
|
// Case 2: error
|
||||||
let input_files = process_decompressible_input(input_files)?;
|
let input_files = process_decompressible_input(input_files)?;
|
||||||
return Ok(
|
return Ok(Command {
|
||||||
Command {
|
|
||||||
command_type: CommandType::Decompression(input_files),
|
command_type: CommandType::Decompression(input_files),
|
||||||
output: None
|
output: None,
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
use std::{fmt, path::PathBuf};
|
use std::fmt;
|
||||||
|
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
@ -6,9 +6,10 @@ use colored::Colorize;
|
|||||||
pub enum Error {
|
pub enum Error {
|
||||||
UnknownExtensionError(String),
|
UnknownExtensionError(String),
|
||||||
MissingExtensionError(String),
|
MissingExtensionError(String),
|
||||||
|
// TODO: get rid of this error variant
|
||||||
InvalidUnicode,
|
InvalidUnicode,
|
||||||
InvalidInput,
|
InvalidInput,
|
||||||
InputsMustHaveBeenDecompressible(String)
|
InputsMustHaveBeenDecompressible(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should be placed somewhere else
|
// This should be placed somewhere else
|
||||||
|
0
src/evaluator.rs
Normal file
0
src/evaluator.rs
Normal file
@ -43,6 +43,9 @@ impl TryFrom<&PathBuf> for CompressionExtension {
|
|||||||
match ext {
|
match ext {
|
||||||
"zip" => Ok(Zip),
|
"zip" => Ok(Zip),
|
||||||
"tar" => Ok(Tar),
|
"tar" => Ok(Tar),
|
||||||
|
"gz" => Ok(Gzip),
|
||||||
|
"bz" => Ok(Bzip),
|
||||||
|
"lzma" => Ok(Lzma),
|
||||||
other => Err(error::Error::UnknownExtensionError(other.into())),
|
other => Err(error::Error::UnknownExtensionError(other.into())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
38
src/file.rs
38
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;
|
use crate::extensions::CompressionExtension;
|
||||||
|
|
||||||
// pub type File = (PathBuf, CompressionExtension);
|
|
||||||
|
|
||||||
// #[derive(Debug)]
|
|
||||||
// pub struct FileWithExtension {
|
|
||||||
// pub extension: CompressionExtension,
|
|
||||||
// pub filename: PathBuf,
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug)]
|
#[derive(PartialEq, Eq, Debug)]
|
||||||
pub enum File {
|
pub enum File {
|
||||||
WithExtension((PathBuf, CompressionExtension)),
|
WithExtension((PathBuf, CompressionExtension)),
|
||||||
WithoutExtension(PathBuf)
|
WithoutExtension(PathBuf),
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl TryFrom<String> for FileWithExtension {
|
|
||||||
// type Error = OuchError;
|
|
||||||
|
|
||||||
// fn try_from(filename: String) -> error::OuchResult<Self> {
|
|
||||||
// // 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,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
17
src/main.rs
17
src/main.rs
@ -1,15 +1,13 @@
|
|||||||
use std::{convert::TryFrom, fs::File};
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use cli::get_matches;
|
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod file;
|
|
||||||
mod extensions;
|
|
||||||
mod error;
|
mod error;
|
||||||
|
mod extensions;
|
||||||
|
mod file;
|
||||||
mod test;
|
mod test;
|
||||||
|
mod evaluator;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
// Just testing
|
// Just testing
|
||||||
|
|
||||||
// let args: Vec<String> = std::env::args().collect();
|
// let args: Vec<String> = std::env::args().collect();
|
||||||
@ -35,12 +33,13 @@ fn main() {
|
|||||||
|
|
||||||
// dbg!(compression);
|
// dbg!(compression);
|
||||||
|
|
||||||
let matches = get_matches();
|
let matches = cli::get_matches();
|
||||||
match cli::Command::try_from(matches) {
|
match cli::Command::try_from(matches) {
|
||||||
Ok(vals) => { dbg!(vals); },
|
Ok(vals) => {
|
||||||
|
dbg!(vals);
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
print!("{}\n", err);
|
print!("{}\n", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
142
src/test.rs
142
src/test.rs
@ -1,38 +1,23 @@
|
|||||||
use std::{convert::TryFrom};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod cli {
|
mod cli {
|
||||||
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
use crate::cli::clap_app;
|
use crate::cli::clap_app;
|
||||||
use crate::cli::Command;
|
use crate::cli::Command;
|
||||||
use crate::file::File;
|
|
||||||
use crate::cli::CommandType::*;
|
use crate::cli::CommandType::*;
|
||||||
use crate::extensions::CompressionExtension::*;
|
|
||||||
use crate::error::OuchResult;
|
use crate::error::OuchResult;
|
||||||
|
use crate::extensions::CompressionExtension::*;
|
||||||
|
use crate::file::File;
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decompress_files_into_folder() -> OuchResult<()> {
|
fn decompress_files_into_folder() -> OuchResult<()> {
|
||||||
let matches = clap_app().
|
let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "-o", "folder/"]);
|
||||||
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)?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
command_from_matches,
|
command_from_matches,
|
||||||
Command {
|
Command {
|
||||||
command_type: Decompression(
|
command_type: Decompression(vec![("file.zip".into(), Zip,),],),
|
||||||
vec![
|
|
||||||
(
|
|
||||||
"file.zip".into(),
|
|
||||||
Zip,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
output: Some(File::WithoutExtension("folder".into())),
|
output: Some(File::WithoutExtension("folder".into())),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -42,27 +27,16 @@ mod cli {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn decompress_files() -> OuchResult<()> {
|
fn decompress_files() -> OuchResult<()> {
|
||||||
let matches = clap_app().
|
let matches = clap_app().get_matches_from(vec!["ouch", "-i", "file.zip", "file.tar"]);
|
||||||
get_matches_from(
|
|
||||||
vec!["ouch", "-i", "file.zip", "file.tar"]
|
|
||||||
);
|
|
||||||
let command_from_matches = Command::try_from(matches)?;
|
let command_from_matches = Command::try_from(matches)?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
command_from_matches,
|
command_from_matches,
|
||||||
Command {
|
Command {
|
||||||
command_type: Decompression(
|
command_type: Decompression(vec![
|
||||||
vec![
|
("file.zip".into(), Zip,),
|
||||||
(
|
("file.tar".into(), Tar,),
|
||||||
"file.zip".into(),
|
],),
|
||||||
Zip,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"file.tar".into(),
|
|
||||||
Tar,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
output: None,
|
output: None,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -72,15 +46,27 @@ mod cli {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compress_files() -> OuchResult<()> {
|
fn compress_files() -> OuchResult<()> {
|
||||||
let matches = clap_app().
|
let matches = clap_app().get_matches_from(vec![
|
||||||
get_matches_from(
|
"ouch",
|
||||||
vec!["ouch", "-i", "file", "file2.jpeg", "file3.ok", "-o", "file.tar"]
|
"-i",
|
||||||
);
|
"file",
|
||||||
|
"file2.jpeg",
|
||||||
|
"file3.ok",
|
||||||
|
"-o",
|
||||||
|
"file.tar",
|
||||||
|
]);
|
||||||
let command_from_matches = Command::try_from(matches)?;
|
let command_from_matches = Command::try_from(matches)?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
command_from_matches,
|
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(())
|
Ok(())
|
||||||
@ -94,19 +80,13 @@ mod cli_errors {
|
|||||||
|
|
||||||
use crate::cli::clap_app;
|
use crate::cli::clap_app;
|
||||||
use crate::cli::Command;
|
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::Error;
|
||||||
|
use crate::error::OuchResult;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compress_files() -> OuchResult<()> {
|
fn compress_files() -> OuchResult<()> {
|
||||||
let matches = clap_app().
|
let matches =
|
||||||
get_matches_from(
|
clap_app().get_matches_from(vec!["ouch", "-i", "a_file", "file2.jpeg", "file3.ok"]);
|
||||||
vec!["ouch", "-i", "a_file", "file2.jpeg", "file3.ok"]
|
|
||||||
);
|
|
||||||
let res = Command::try_from(matches);
|
let res = Command::try_from(matches);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -117,3 +97,65 @@ mod cli_errors {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user