Skip to content

Commit

Permalink
Merge pull request #17 from tamada/release/v0.2.0
Browse files Browse the repository at this point in the history
Release/v0.2.0
  • Loading branch information
tamada authored May 3, 2024
2 parents 440b8a6 + 80d9404 commit 2f4e2e5
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "A tool for archiving files and directories and extracting several
repository = "https://github.com/tamada/totebag"
readme = "README.md"
authors = [
"Haruaki Tamada <tamada_f@ke_cafebabe.jp"
"Haruaki Tamada <tamada_f@ke_cafebabe.jp>"
]
license = "MIT"
categories = ["command-line-utilities", "compression"]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# totebag

[![Version](https://shields.io/badge/Version-0.1.18-blue)](https://github.com/tamada/totebag/releases/tag/v0.1.18)
[![Version](https://shields.io/badge/Version-0.2.0-blue)](https://github.com/tamada/totebag/releases/tag/v0.2.0)
[![MIT License](https://shields.io/badge/License-MIT-blue)](https://github.com/tamada/totebag/blob/main/LICENSE)

[![build](https://github.com/tamada/totebag/actions/workflows/build.yaml/badge.svg)](https://github.com/tamada/totebag/actions/workflows/build.yaml)
Expand All @@ -9,13 +9,13 @@

A tool for archiving files and directories and extracting several archive formats.

## :speaking_head: Description
## Description

There are many archive formats and their tools. The one problem with using each tool is that its interfaces are slightly different.
Then, The `totebag` treats the archive files as the same interface.
The tool can extract archive files and archive files and directories.

## :runner: Usage
## Usage

```sh
totebag [OPTIONS] <ARGUMENTS...>
Expand All @@ -37,7 +37,7 @@ ARGUMENTS
Otherwise, it will archive the files.
```
## :anchor: Install
## Install
```sh
brew install tamada/tap/totebag
Expand Down
32 changes: 30 additions & 2 deletions src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::archiver::tar::{TarArchiver, TarGzArchiver, TarBz2Archiver};
use crate::verboser::{create_verboser, Verboser};
use crate::CliOpts;

mod optscreator;
mod os;
mod zip;
mod rar;
mod tar;
Expand All @@ -19,7 +19,7 @@ pub trait Archiver {
fn format(&self) -> Format;
}

pub fn create_archiver(dest: PathBuf) -> Result<Box<dyn Archiver>> {
pub fn create_archiver(dest: &PathBuf) -> Result<Box<dyn Archiver>> {
let format = find_format(dest.file_name());
match format {
Ok(format) => {
Expand Down Expand Up @@ -89,3 +89,31 @@ impl ArchiverOpts {
}
}
}

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

#[test]
fn test_archiver() {
let a1 = create_archiver(&PathBuf::from("results/test.tar"));
assert!(a1.is_ok());
assert_eq!(a1.unwrap().format(), Format::Tar);

let a2 = create_archiver(&PathBuf::from("results/test.tar.gz"));
assert!(a2.is_ok());
assert_eq!(a2.unwrap().format(), Format::TarGz);

let a3 = create_archiver(&PathBuf::from("results/test.tar.bz2"));
assert!(a3.is_ok());
assert_eq!(a3.unwrap().format(), Format::TarBz2);

let a4 = create_archiver(&PathBuf::from("results/test.zip"));
assert!(a4.is_ok());
assert_eq!(a4.unwrap().format(), Format::Zip);

let a5 = create_archiver(&PathBuf::from("results/test.rar"));
assert!(a5.is_ok());
assert_eq!(a5.unwrap().format(), Format::Rar);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions src/archiver/rar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,31 @@ impl Archiver for RarArchiver {
Format::Rar
}
}

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

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

#[test]
fn test_format() {
let archiver = RarArchiver{};
assert_eq!(archiver.format(), Format::Rar);
}

#[test]
fn test_archive() {
let archiver = RarArchiver{};
let opts = ArchiverOpts {
dest: PathBuf::from("results/test.rar"),
targets: vec![],
overwrite: false,
recursive: false,
v: create_verboser(false),
};
let r = archiver.perform(opts);
assert!(r.is_err());
}
}
71 changes: 70 additions & 1 deletion src/archiver/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,73 @@ fn write_to_tar<W: Write>(file: W, targets: Vec<PathBuf>, recursive: bool) -> Re
return Err(ToatError::ArchiverError(e.to_string()))
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use crate::archiver::Archiver;
use crate::archiver::tar::{TarArchiver, TarGzArchiver, TarBz2Archiver};
use crate::archiver::ArchiverOpts;
use crate::format::Format;

fn run_test<F>(f: F)
where
F: FnOnce() -> PathBuf,
{
// setup(); // 予めやりたい処理
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
match result {
Ok(path) => teardown(path),
Err(err) => std::panic::resume_unwind(err),
}
}

#[test]
fn test_tar() {
run_test(|| {
let archiver = TarArchiver{};
let inout = ArchiverOpts::create(PathBuf::from("results/test.tar"), vec![PathBuf::from("src"), PathBuf::from("Cargo.toml")], true, true, false);
let result = archiver.perform(inout);
let path = PathBuf::from("results/test.tar");
assert!(result.is_ok());
assert!(path.exists());
assert_eq!(archiver.format(), Format::Tar);
path
});
}


#[test]
fn test_targz() {
run_test(|| {
let archiver = TarGzArchiver{};
let inout = ArchiverOpts::create(PathBuf::from("results/test.tar.gz"), vec![PathBuf::from("src"), PathBuf::from("Cargo.toml")], true, true, false);
let result = archiver.perform(inout);
let path = PathBuf::from("results/test.tar.gz");
assert!(result.is_ok());
assert!(path.exists());
assert_eq!(archiver.format(), Format::TarGz);
path
});
}

#[test]
fn test_tarbz2() {
run_test(|| {
let archiver = TarBz2Archiver{};
let inout = ArchiverOpts::create(PathBuf::from("results/test.tar.bz2"), vec![PathBuf::from("src"), PathBuf::from("Cargo.toml")], true, true, false);
let result = archiver.perform(inout);
let path = PathBuf::from("results/test.tar.bz2");
assert!(result.is_ok());
assert!(path.exists());
assert_eq!(archiver.format(), Format::TarBz2);
path
});
}

fn teardown(path: PathBuf) {
let _ = std::fs::remove_file(path);
}
}
10 changes: 5 additions & 5 deletions src/archiver/zip.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#[cfg(target_os = "windows")]
use optscreator::windows::*;
use os::windows::*;

#[cfg(any(target_os = "linux", target_os = "macos"))]
use optscreator::linux::*;
use os::linux::*;

use std::fs::File;
use std::path::PathBuf;
use std::io::{BufReader, Write, Seek};
use zip::ZipWriter;

use crate::archiver::{Archiver, Format, ArchiverOpts};
use crate::archiver::optscreator;
use crate::archiver::os;
use crate::cli::{ToatError, Result};

pub(super) struct ZipArchiver {
Expand Down Expand Up @@ -95,14 +95,14 @@ mod tests {
fn test_zip() {
run_test(|| {
let archiver = ZipArchiver{};
let inout = ArchiverOpts::create(PathBuf::from("test.zip"), vec![PathBuf::from("src"), PathBuf::from("Cargo.toml")], true, true, false);
let inout = ArchiverOpts::create(PathBuf::from("results/test.zip"), vec![PathBuf::from("src"), PathBuf::from("Cargo.toml")], true, true, false);
let result = archiver.perform(inout);
assert!(result.is_ok());
assert_eq!(archiver.format(), Format::Zip);
});
}

fn teardown() {
let _ = std::fs::remove_file("test.zip");
let _ = std::fs::remove_file("results/test.zip");
}
}
28 changes: 26 additions & 2 deletions src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ pub trait Extractor {
fn format(&self) -> Format;
}

pub fn create_extract_opts(opts: CliOpts) -> ExtractorOpts {
pub fn create_extract_opts(opts: &CliOpts) -> ExtractorOpts {
let d = opts.dest.clone();
ExtractorOpts {
dest: opts.dest.unwrap_or_else(|| {
dest: d.unwrap_or_else(|| {
PathBuf::from(".")
}),
use_archive_name_dir: opts.to_archive_name_dir,
Expand Down Expand Up @@ -97,4 +98,27 @@ mod tests {
let target = PathBuf::from("/tmp/archive.zip");
assert_eq!(opts2.destination(&target), PathBuf::from("."));
}

#[test]
fn test_create_extractor() {
let e1 = create_extractor(&PathBuf::from("results/test.zip"));
assert!(e1.is_ok());
assert_eq!(e1.unwrap().format(), Format::Zip);

let e2 = create_extractor(&PathBuf::from("results/test.tar"));
assert!(e2.is_ok());
assert_eq!(e2.unwrap().format(), Format::Tar);

let e3 = create_extractor(&PathBuf::from("results/test.tgz"));
assert!(e3.is_ok());
assert_eq!(e3.unwrap().format(), Format::TarGz);

let e4 = create_extractor(&PathBuf::from("results/test.tbz2"));
assert!(e4.is_ok());
assert_eq!(e4.unwrap().format(), Format::TarBz2);

let e5 = create_extractor(&PathBuf::from("results/test.rar"));
assert!(e5.is_ok());
assert_eq!(e5.unwrap().format(), Format::Rar);
}
}
27 changes: 27 additions & 0 deletions src/extractor/rar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Extractor for RarExtractor {
#[cfg(test)]
mod tests {
use super::*;
use crate::verboser::create_verboser;

#[test]
fn test_list_archives() {
Expand All @@ -60,4 +61,30 @@ mod tests {
Err(_) => assert!(false),
}
}

#[test]
fn test_extract_archive() {
let e = RarExtractor{};
let file = PathBuf::from("testdata/test.rar");
let opts = ExtractorOpts {
dest: PathBuf::from("results/rar"),
use_archive_name_dir: true,
overwrite: true,
v: create_verboser(false),
};
match e.perform(file, &opts) {
Ok(_) => {
assert!(true);
assert!(PathBuf::from("results/rar/test/Cargo.toml").exists());
std::fs::remove_dir_all(PathBuf::from("results/rar")).unwrap();
},
Err(_) => assert!(false),
};
}

#[test]
fn test_format() {
let extractor = RarExtractor{};
assert_eq!(extractor.format(), Format::Rar);
}
}
34 changes: 34 additions & 0 deletions src/extractor/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn list_tar<R: Read>(archive: &mut tar::Archive<R>) -> Result<Vec<String>> {
#[cfg(test)]
mod tests {
use super::*;
use crate::verboser::create_verboser;

#[test]
fn test_list_tar_file() {
Expand All @@ -120,6 +121,27 @@ mod tests {
}
}

#[test]
fn test_extract_archive() {
let e = TarExtractor{};
let file = PathBuf::from("testdata/test.tar");
let opts = ExtractorOpts {
dest: PathBuf::from("results/tar"),
use_archive_name_dir: false,
overwrite: true,
v: create_verboser(false),
};
match e.perform(file, &opts) {
Ok(_) => {
assert!(true);
assert!(PathBuf::from("results/tar/Cargo.toml").exists());
std::fs::remove_dir_all(PathBuf::from("results/tar")).unwrap();
},
Err(_) => assert!(false),
};
}


#[test]
fn test_list_tarbz2_file() {
let extractor = TarBz2Extractor{};
Expand Down Expand Up @@ -151,4 +173,16 @@ mod tests {
Err(_) => assert!(false),
}
}

#[test]
fn test_format() {
let e1 = TarExtractor{};
assert_eq!(e1.format(), Format::Tar);

let e2 = TarGzExtractor{};
assert_eq!(e2.format(), Format::TarGz);

let e3 = TarBz2Extractor{};
assert_eq!(e3.format(), Format::TarBz2);
}
}
Loading

0 comments on commit 2f4e2e5

Please sign in to comment.