extern crate assert_cmd;
extern crate assert_fs;
extern crate predicates;
use assert_cmd::prelude::*;
use assert_fs::TempDir;
use assert_fs::prelude::*;
use predicates::prelude::*;
use std::process::Command;
#[test]
fn test_tar_gz_manual_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let file = temp_dir.child("test.txt");
file.write_str("test content")?;
let tar_file = temp_dir.child("test.tar");
Command::cargo_bin("cmprss")?
.arg("tar")
.arg(file.path())
.arg(tar_file.path())
.assert()
.success();
let tar_gz_file = temp_dir.child("test.tar.gz");
Command::cargo_bin("cmprss")?
.arg("gzip")
.arg(tar_file.path())
.arg(tar_gz_file.path())
.assert()
.success();
let extract_tar = temp_dir.child("extracted.tar");
Command::cargo_bin("cmprss")?
.arg("gzip")
.arg("--extract")
.arg(tar_gz_file.path())
.arg(extract_tar.path())
.assert()
.success();
let extract_dir = temp_dir.child("extracted");
extract_dir.create_dir_all()?;
Command::cargo_bin("cmprss")?
.arg("tar")
.arg("--extract")
.arg(extract_tar.path())
.arg(extract_dir.path())
.assert()
.success();
let extracted_file = extract_dir.child("test.txt");
extracted_file.assert(predicate::path::exists());
extracted_file.assert(predicate::str::contains("test content"));
Ok(())
}
#[test]
fn test_tar_gz_compress() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let source_dir = temp_dir.child("source");
source_dir.create_dir_all()?;
let test_file = source_dir.child("test_file.txt");
test_file.write_str("test content for tar.gz compression")?;
let archive = temp_dir.child("direct.tar.gz");
Command::cargo_bin("cmprss")?
.arg("--compress") .arg(source_dir.path())
.arg(archive.path())
.assert()
.success();
archive.assert(predicate::path::exists());
Ok(())
}
#[test]
fn test_tar_gz_extract() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let source_dir = temp_dir.child("source");
source_dir.create_dir_all()?;
let test_file = source_dir.child("test_file.txt");
test_file.write_str("test content for tar.gz extraction")?;
let tar_file = temp_dir.child("archive.tar");
Command::cargo_bin("cmprss")?
.arg("tar")
.arg(source_dir.path())
.arg(tar_file.path())
.assert()
.success();
let tar_gz_file = temp_dir.child("archive.tar.gz");
Command::cargo_bin("cmprss")?
.arg("gzip")
.arg(tar_file.path())
.arg(tar_gz_file.path())
.assert()
.success();
let extract_dir = temp_dir.child("extract");
extract_dir.create_dir_all()?;
Command::cargo_bin("cmprss")?
.arg("--extract")
.arg(tar_gz_file.path())
.arg(extract_dir.path())
.assert()
.success();
let extracted_file = extract_dir.child("source").child("test_file.txt");
extracted_file.assert(predicate::path::exists());
extracted_file.assert(predicate::str::contains(
"test content for tar.gz extraction",
));
Ok(())
}
fn tar_pipeline_roundtrip(ext: &str) -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let source_dir = temp_dir.child("source");
source_dir.create_dir_all()?;
let test_file = source_dir.child("data.txt");
let content = format!("{ext} roundtrip content");
test_file.write_str(&content)?;
let archive = temp_dir.child(format!("archive.{ext}"));
Command::cargo_bin("cmprss")?
.arg("--compress")
.arg(source_dir.path())
.arg(archive.path())
.assert()
.success();
let extract_dir = temp_dir.child("extract");
extract_dir.create_dir_all()?;
Command::cargo_bin("cmprss")?
.arg("--extract")
.arg(archive.path())
.arg(extract_dir.path())
.assert()
.success();
let extracted_file = extract_dir.child("source").child("data.txt");
extracted_file.assert(predicate::path::exists());
extracted_file.assert(predicate::str::contains(content));
Ok(())
}
#[test]
fn test_tar_xz_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
tar_pipeline_roundtrip("tar.xz")
}
#[test]
fn test_tar_bz2_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
tar_pipeline_roundtrip("tar.bz2")
}
#[test]
fn test_tar_zst_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
tar_pipeline_roundtrip("tar.zst")
}
#[test]
fn test_tar_lzma_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
tar_pipeline_roundtrip("tar.lzma")
}
#[test]
fn test_tar_br_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
tar_pipeline_roundtrip("tar.br")
}
#[test]
fn test_tar_lz4_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
tar_pipeline_roundtrip("tar.lz4")
}
#[test]
fn test_tar_sz_roundtrip() -> Result<(), Box<dyn std::error::Error>> {
tar_pipeline_roundtrip("tar.sz")
}
#[test]
fn test_tar_gz_explicit_then_extract() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = TempDir::new()?;
let test_file = temp_dir.child("test.txt");
test_file.write_str("test content for tar.gz")?;
let tar_file = temp_dir.child("test.tar");
Command::cargo_bin("cmprss")?
.arg("tar")
.arg(test_file.path())
.arg(tar_file.path())
.assert()
.success();
let tar_gz_file = temp_dir.child("test.tar.gz");
Command::cargo_bin("cmprss")?
.arg("gzip")
.arg(tar_file.path())
.arg(tar_gz_file.path())
.assert()
.success();
let extract_dir = temp_dir.child("extract");
extract_dir.create_dir_all()?;
Command::cargo_bin("cmprss")?
.arg("-e") .arg(tar_gz_file.path())
.arg(extract_dir.path())
.assert()
.success();
let extracted_file = extract_dir.child("test.txt");
extracted_file.assert(predicate::path::exists());
extracted_file.assert(predicate::str::contains("test content for tar.gz"));
Ok(())
}