diff --git a/src/archiver/cab.rs b/src/archiver/cab.rs index 652ab32..55c8a05 100644 --- a/src/archiver/cab.rs +++ b/src/archiver/cab.rs @@ -55,7 +55,7 @@ fn correct_targets(targets: Vec, recursive: bool, base_dir: PathBuf) -> let path = target.as_path(); if path.is_dir() && recursive { process_dir(&mut result, path.to_path_buf(), &base_dir); - } else { + } else if path.is_file(){ process_file(&mut result, path.to_path_buf(), &base_dir); } } @@ -82,4 +82,52 @@ fn process_file(result: &mut Vec<(PathBuf, String)>, target: PathBuf, base_dir: }; let name = target_path.to_str().unwrap(); result.push((target, name.to_string())); -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::path::PathBuf; + use crate::verboser::create_verboser; + + fn run_test(f: F) + where + F: FnOnce(), + { + // setup(); // 予めやりたい処理 + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)); + teardown(); // 後片付け処理 + + if let Err(err) = result { + std::panic::resume_unwind(err); + } + } + + #[test] + fn test_format() { + let archiver = CabArchiver{}; + assert_eq!(archiver.format(), Format::Cab); + } + + #[test] + fn test_archive() { + run_test(|| { + let archiver = CabArchiver{}; + let opts = ArchiverOpts { + dest: PathBuf::from("results/test.cab"), + targets: vec![PathBuf::from("src"), PathBuf::from("Cargo.toml")], + base_dir: PathBuf::from("."), + overwrite: false, + recursive: false, + v: create_verboser(false), + }; + let r = archiver.perform(&opts); + assert!(r.is_ok()); + }); + } + + fn teardown() { + let _ = std::fs::remove_file("results/test.cab"); + } +} diff --git a/src/archiver/lha.rs b/src/archiver/lha.rs index 7da9442..92a7312 100644 --- a/src/archiver/lha.rs +++ b/src/archiver/lha.rs @@ -1,12 +1,13 @@ -use crate::archiver::{Archiver, Format, ArchiverOpts}; -use crate::cli::{ToteError, Result}; +use crate::archiver::{Archiver, ArchiverOpts, Format}; +use crate::cli::{Result, ToteError}; -pub(super) struct LhaArchiver { -} +pub(super) struct LhaArchiver {} -impl Archiver for LhaArchiver { +impl Archiver for LhaArchiver { fn perform(&self, _: &ArchiverOpts) -> Result<()> { - Err(ToteError::UnsupportedFormat("only extraction support for lha".to_string())) + Err(ToteError::UnsupportedFormat( + "only extraction support for lha".to_string(), + )) } fn format(&self) -> Format { Format::LHA @@ -17,20 +18,20 @@ impl Archiver for LhaArchiver { mod tests { use super::*; - use std::path::PathBuf; use crate::verboser::create_verboser; + use std::path::PathBuf; #[test] fn test_format() { - let archiver = LhaArchiver{}; + let archiver = LhaArchiver {}; assert_eq!(archiver.format(), Format::LHA); } #[test] fn test_archive() { - let archiver = LhaArchiver{}; + let archiver = LhaArchiver {}; let opts = ArchiverOpts { - dest: PathBuf::from("results/test.rar"), + dest: PathBuf::from("results/test.lzh"), targets: vec![], base_dir: PathBuf::from("."), overwrite: false, diff --git a/src/archiver/rar.rs b/src/archiver/rar.rs index 9c1fbf8..8012fb8 100644 --- a/src/archiver/rar.rs +++ b/src/archiver/rar.rs @@ -1,12 +1,13 @@ -use crate::archiver::{Archiver, Format, ArchiverOpts}; -use crate::cli::{ToteError, Result}; +use crate::archiver::{Archiver, ArchiverOpts, Format}; +use crate::cli::{Result, ToteError}; -pub(super) struct RarArchiver { -} +pub(super) struct RarArchiver {} -impl Archiver for RarArchiver { +impl Archiver for RarArchiver { fn perform(&self, _: &ArchiverOpts) -> Result<()> { - Err(ToteError::UnsupportedFormat("only extraction support for rar".to_string())) + Err(ToteError::UnsupportedFormat( + "only extraction support for rar".to_string(), + )) } fn format(&self) -> Format { Format::Rar @@ -17,18 +18,18 @@ impl Archiver for RarArchiver { mod tests { use super::*; - use std::path::PathBuf; use crate::verboser::create_verboser; + use std::path::PathBuf; #[test] fn test_format() { - let archiver = RarArchiver{}; + let archiver = RarArchiver {}; assert_eq!(archiver.format(), Format::Rar); } #[test] fn test_archive() { - let archiver = RarArchiver{}; + let archiver = RarArchiver {}; let opts = ArchiverOpts { dest: PathBuf::from("results/test.rar"), targets: vec![],