···4848 aliases: [t]
4949 sources:
5050 - ./**/*.rs
5151- cmd: cargo nextest run
5151+ cmd: cargo nextest run --no-default-features
5252 audit:
5353 desc: Run cargo security audit
5454 sources:
+1-39
bin/test.sh
···6060 cargo run --release --quiet -- "$1" --ignore-pipes "${@:2}"
6161}
62626363-test_tar() {
6464- tmpdir
6565- echo "Testing tar in $PWD"
6666- echo "Creating random data"
6767- random_dir 100 dir
6868- echo "Compressing with tar and cmprss"
6969- tar -cf tar_dir.tar dir
7070- cmprss tar dir cmprss_dir.tar
7171- # Compare the two archives
7272- # We don't care if the archives are slightly different, just that they decompress the same
7373- #compare tar_dir.tar cmprss_dir.tar
7474- # Decompress the 4 variations
7575- echo "Decompressing"
7676- mkdir -p tar_tar
7777- cd tar_tar
7878- tar -xf ../tar_dir.tar
7979- cd ..
8080- mkdir -p tar_cmprss
8181- cd tar_cmprss
8282- cmprss tar --extract ../tar_dir.tar
8383- cd ..
8484- mkdir -p cmprss_tar
8585- cd cmprss_tar
8686- tar -xf ../cmprss_dir.tar
8787- cd ..
8888- mkdir -p cmprss_cmprss
8989- cd cmprss_cmprss
9090- cmprss tar --extract ../cmprss_dir.tar
9191- cd ..
9292-9393- echo "Comparing the decompressed files"
9494- compare dir tar_tar/dir
9595- compare dir tar_cmprss/dir
9696- compare dir cmprss_tar/dir
9797- compare dir cmprss_cmprss/dir
9898- echo "No errors detected"
9999-}
100100-10163# Test gzip using the provided compression level
10264test_gzip_level() {
10365 tmpdir
···200162201163# Run all the tests if no arguments are given
202164if [ $# -eq 0 ]; then
203203- set -- tar gzip xz bzip2
165165+ set -- gzip xz bzip2
204166fi
205167206168# Run the tests given on the command line
+200-32
tests/compare.rs
···11-/// Compare the interoperability of cmprss with the official tools.
22-31#[allow(dead_code)]
42mod compare {
55- /// Tests the tar comparisons
66- #[test]
77- #[cfg(feature = "interop")]
88- fn tar() {
99- test_with("tar");
1010- }
33+ use assert_cmd::prelude::*;
44+ use assert_fs::fixture::PathChild;
55+ use std::path::PathBuf;
66+ use std::process::Command;
1171212- /// Tests the gzip comparisons
1313- #[test]
1414- #[cfg(feature = "interop")]
1515- fn gzip() {
1616- test_with("gzip");
1717- }
88+ mod utils {
99+ use super::*;
1010+ use rand::Rng;
18111919- /// Tests the bzip2 comparisons
2020- #[test]
2121- #[cfg(feature = "interop")]
2222- fn bzip2() {
2323- test_with("bzip2");
2424- }
1212+ /// Asserts that the two directories are identical
1313+ pub fn assert_directory(left: PathBuf, right: PathBuf) {
1414+ let left_entries = std::fs::read_dir(left).unwrap();
1515+ let right_entries = std::fs::read_dir(right).unwrap();
25162626- /// Tests the xz comparisons
2727- #[test]
2828- #[cfg(feature = "interop")]
2929- fn xz() {
3030- test_with("xz");
1717+ // Fix the sorting of entries, as it's platform dependent
1818+ let mut left_entries: Vec<_> = left_entries.map(|entry| entry.unwrap()).collect();
1919+ let mut right_entries: Vec<_> = right_entries.map(|entry| entry.unwrap()).collect();
2020+ left_entries.sort_by_key(|entry| entry.file_name());
2121+ right_entries.sort_by_key(|entry| entry.file_name());
2222+2323+ for (left_entry, right_entry) in left_entries.iter().zip(right_entries) {
2424+ assert_eq!(left_entry.file_name(), right_entry.file_name());
2525+2626+ let left_path = left_entry.path();
2727+ let right_path = right_entry.path();
2828+2929+ if left_entry.file_type().unwrap().is_dir() {
3030+ assert!(right_entry.file_type().unwrap().is_dir());
3131+ assert_directory(left_path, right_path);
3232+ } else {
3333+ let left_data = std::fs::read(&left_path).unwrap();
3434+ let right_data = std::fs::read(&right_path).unwrap();
3535+3636+ assert_eq!(left_data, right_data);
3737+ }
3838+ }
3939+ }
4040+4141+ /// Create a directory filled with random files
4242+ pub fn create_random_files(dir: PathBuf, count: usize) {
4343+ let mut rng = rand::thread_rng();
4444+ std::fs::create_dir_all(&dir).unwrap();
4545+4646+ for i in 0..count {
4747+ let path = format!("{}/file-{}", dir.display(), i);
4848+ let data: Vec<u8> = (0..rng.gen_range(0..1000)).map(|_| rng.gen()).collect();
4949+ std::fs::write(path, data).unwrap();
5050+ }
5151+ }
3152 }
32533333- /// Helper to spawn the script with any tool
3434- fn test_with(tool: &str) {
3535- let output = std::process::Command::new("bin/test.sh")
3636- .arg(tool)
3737- .output()
3838- .expect("Failed to execute command");
3939- assert!(output.status.success());
5454+ mod tar {
5555+ use super::*;
5656+5757+ fn tar_dir(dir: PathBuf, archive: PathBuf) {
5858+ // The current directory matters, as tar uses the relative path for the paths inside the tar file
5959+ std::process::Command::new("tar")
6060+ .arg("cf")
6161+ .arg(archive.to_str().unwrap())
6262+ .arg(dir.file_name().unwrap())
6363+ .current_dir(dir.parent().unwrap())
6464+ .output()
6565+ .expect("Failed to execute command");
6666+ }
6767+6868+ fn untar_archive(archive: PathBuf, output_dir: PathBuf) {
6969+ std::process::Command::new("tar")
7070+ .arg("xf")
7171+ .arg(archive.to_str().unwrap())
7272+ .arg("-C")
7373+ .arg(output_dir.to_str().unwrap())
7474+ .output()
7575+ .expect("Failed to execute command");
7676+ }
7777+7878+ fn cmprss_dir(dir: PathBuf, archive: PathBuf) {
7979+ Command::cargo_bin("cmprss")
8080+ .unwrap()
8181+ .arg("tar")
8282+ .arg(dir.to_str().unwrap())
8383+ .arg(archive.to_str().unwrap())
8484+ .assert()
8585+ .success();
8686+ }
8787+8888+ fn dcmprss_archive(archive: PathBuf, output_dir: PathBuf) {
8989+ Command::cargo_bin("cmprss")
9090+ .unwrap()
9191+ .arg("tar")
9292+ .arg("--extract")
9393+ .arg(archive.to_str().unwrap())
9494+ .arg(output_dir.to_str().unwrap())
9595+ .assert()
9696+ .success();
9797+ }
9898+9999+ #[test]
100100+ fn tar_tar() {
101101+ let tmpdir = assert_fs::TempDir::new().unwrap();
102102+ let starting_dir = tmpdir.child("orig").to_path_buf();
103103+ let working_dir = assert_fs::TempDir::new().unwrap();
104104+ let archive = working_dir.child("tar.tar").to_path_buf();
105105+106106+ utils::create_random_files(starting_dir.clone(), 10);
107107+108108+ // Run tar on the initial directory
109109+ tar_dir(starting_dir.clone(), archive.clone());
110110+111111+ // Untar the tar file using tar
112112+ let output_dir = assert_fs::TempDir::new().unwrap();
113113+ untar_archive(archive, output_dir.to_path_buf());
114114+115115+ // Now compare the two directories
116116+ utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf());
117117+ }
118118+119119+ #[test]
120120+ fn cmprss_cmprss() {
121121+ let tmpdir = assert_fs::TempDir::new().unwrap();
122122+ let starting_dir = tmpdir.child("orig").to_path_buf();
123123+ let working_dir = assert_fs::TempDir::new().unwrap();
124124+ let archive = working_dir.child("tar.tar").to_path_buf();
125125+126126+ utils::create_random_files(starting_dir.clone(), 10);
127127+128128+ // Run tar on the initial directory
129129+ cmprss_dir(starting_dir.clone(), archive.clone());
130130+131131+ // Untar the tar file using tar
132132+ let output_dir = assert_fs::TempDir::new().unwrap();
133133+ dcmprss_archive(archive, output_dir.to_path_buf());
134134+135135+ // Now compare the two directories
136136+ utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf());
137137+ }
138138+139139+ #[test]
140140+ fn cmprss_tar() {
141141+ let tmpdir = assert_fs::TempDir::new().unwrap();
142142+ let starting_dir = tmpdir.child("orig").to_path_buf();
143143+ let working_dir = assert_fs::TempDir::new().unwrap();
144144+ let archive = working_dir.child("tar.tar").to_path_buf();
145145+146146+ utils::create_random_files(starting_dir.clone(), 10);
147147+148148+ // Run tar on the initial directory
149149+ cmprss_dir(starting_dir.clone(), archive.clone());
150150+151151+ // Untar the tar file using tar
152152+ let output_dir = assert_fs::TempDir::new().unwrap();
153153+ untar_archive(archive, output_dir.to_path_buf());
154154+155155+ // Now compare the two directories
156156+ utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf());
157157+ }
158158+159159+ #[test]
160160+ fn tar_cmprss() {
161161+ let tmpdir = assert_fs::TempDir::new().unwrap();
162162+ let starting_dir = tmpdir.child("orig").to_path_buf();
163163+ let working_dir = assert_fs::TempDir::new().unwrap();
164164+ let archive = working_dir.child("tar.tar").to_path_buf();
165165+166166+ utils::create_random_files(starting_dir.clone(), 10);
167167+168168+ // Run tar on the initial directory
169169+ tar_dir(starting_dir.clone(), archive.clone());
170170+171171+ // Untar the tar file using tar
172172+ let output_dir = assert_fs::TempDir::new().unwrap();
173173+ dcmprss_archive(archive, output_dir.to_path_buf());
174174+175175+ // Now compare the two directories
176176+ utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf());
177177+ }
40178 }
179179+180180+ // /// Tests the gzip comparisons
181181+ // #[test]
182182+ // #[cfg(feature = "interop")]
183183+ // fn gzip() {
184184+ // test_with("gzip");
185185+ // }
186186+187187+ // /// Tests the bzip2 comparisons
188188+ // #[test]
189189+ // #[cfg(feature = "interop")]
190190+ // fn bzip2() {
191191+ // test_with("bzip2");
192192+ // }
193193+194194+ // /// Tests the xz comparisons
195195+ // #[test]
196196+ // #[cfg(feature = "interop")]
197197+ // fn xz() {
198198+ // test_with("xz");
199199+ // }
200200+201201+ // /// Helper to spawn the script with any tool
202202+ // fn test_with(tool: &str) {
203203+ // let output = std::process::Command::new("bin/test.sh")
204204+ // .arg(tool)
205205+ // .output()
206206+ // .expect("Failed to execute command");
207207+ // assert!(output.status.success());
208208+ // }
41209}