Skip to content

Commit

Permalink
add cab archiver/extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Aug 16, 2024
1 parent 54f109b commit 4bd316f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
52 changes: 50 additions & 2 deletions src/archiver/cab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn correct_targets(targets: Vec<PathBuf>, 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);
}
}
Expand All @@ -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()));
}
}

#[cfg(test)]
mod tests {
use super::*;

use std::path::PathBuf;
use crate::verboser::create_verboser;

fn run_test<F>(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");
}
}
21 changes: 11 additions & 10 deletions src/archiver/lha.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions src/archiver/rar.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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![],
Expand Down

0 comments on commit 4bd316f

Please sign in to comment.