this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

test: move the tar comparison tests into Rust

+257 -72
+54
Cargo.lock
··· 212 212 "indicatif", 213 213 "is-terminal", 214 214 "predicates", 215 + "rand", 215 216 "tar", 216 217 "xz2", 217 218 ] ··· 346 347 checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" 347 348 dependencies = [ 348 349 "num-traits", 350 + ] 351 + 352 + [[package]] 353 + name = "getrandom" 354 + version = "0.2.14" 355 + source = "registry+https://github.com/rust-lang/crates.io-index" 356 + checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" 357 + dependencies = [ 358 + "cfg-if", 359 + "libc", 360 + "wasi", 349 361 ] 350 362 351 363 [[package]] ··· 535 547 checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" 536 548 537 549 [[package]] 550 + name = "ppv-lite86" 551 + version = "0.2.17" 552 + source = "registry+https://github.com/rust-lang/crates.io-index" 553 + checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" 554 + 555 + [[package]] 538 556 name = "predicates" 539 557 version = "3.0.4" 540 558 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 584 602 ] 585 603 586 604 [[package]] 605 + name = "rand" 606 + version = "0.8.5" 607 + source = "registry+https://github.com/rust-lang/crates.io-index" 608 + checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" 609 + dependencies = [ 610 + "libc", 611 + "rand_chacha", 612 + "rand_core", 613 + ] 614 + 615 + [[package]] 616 + name = "rand_chacha" 617 + version = "0.3.1" 618 + source = "registry+https://github.com/rust-lang/crates.io-index" 619 + checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" 620 + dependencies = [ 621 + "ppv-lite86", 622 + "rand_core", 623 + ] 624 + 625 + [[package]] 626 + name = "rand_core" 627 + version = "0.6.4" 628 + source = "registry+https://github.com/rust-lang/crates.io-index" 629 + checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" 630 + dependencies = [ 631 + "getrandom", 632 + ] 633 + 634 + [[package]] 587 635 name = "redox_syscall" 588 636 version = "0.3.5" 589 637 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 761 809 "same-file", 762 810 "winapi-util", 763 811 ] 812 + 813 + [[package]] 814 + name = "wasi" 815 + version = "0.11.0+wasi-snapshot-preview1" 816 + source = "registry+https://github.com/rust-lang/crates.io-index" 817 + checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" 764 818 765 819 [[package]] 766 820 name = "winapi"
+1
Cargo.toml
··· 23 23 assert_cmd = "2.0.11" 24 24 assert_fs = "1.0.12" 25 25 predicates = "3.0.2" 26 + rand = "0.8" 26 27 27 28 [features] 28 29 default = ["interop"]
+1 -1
Taskfile.yml
··· 48 48 aliases: [t] 49 49 sources: 50 50 - ./**/*.rs 51 - cmd: cargo nextest run 51 + cmd: cargo nextest run --no-default-features 52 52 audit: 53 53 desc: Run cargo security audit 54 54 sources:
+1 -39
bin/test.sh
··· 60 60 cargo run --release --quiet -- "$1" --ignore-pipes "${@:2}" 61 61 } 62 62 63 - test_tar() { 64 - tmpdir 65 - echo "Testing tar in $PWD" 66 - echo "Creating random data" 67 - random_dir 100 dir 68 - echo "Compressing with tar and cmprss" 69 - tar -cf tar_dir.tar dir 70 - cmprss tar dir cmprss_dir.tar 71 - # Compare the two archives 72 - # We don't care if the archives are slightly different, just that they decompress the same 73 - #compare tar_dir.tar cmprss_dir.tar 74 - # Decompress the 4 variations 75 - echo "Decompressing" 76 - mkdir -p tar_tar 77 - cd tar_tar 78 - tar -xf ../tar_dir.tar 79 - cd .. 80 - mkdir -p tar_cmprss 81 - cd tar_cmprss 82 - cmprss tar --extract ../tar_dir.tar 83 - cd .. 84 - mkdir -p cmprss_tar 85 - cd cmprss_tar 86 - tar -xf ../cmprss_dir.tar 87 - cd .. 88 - mkdir -p cmprss_cmprss 89 - cd cmprss_cmprss 90 - cmprss tar --extract ../cmprss_dir.tar 91 - cd .. 92 - 93 - echo "Comparing the decompressed files" 94 - compare dir tar_tar/dir 95 - compare dir tar_cmprss/dir 96 - compare dir cmprss_tar/dir 97 - compare dir cmprss_cmprss/dir 98 - echo "No errors detected" 99 - } 100 - 101 63 # Test gzip using the provided compression level 102 64 test_gzip_level() { 103 65 tmpdir ··· 200 162 201 163 # Run all the tests if no arguments are given 202 164 if [ $# -eq 0 ]; then 203 - set -- tar gzip xz bzip2 165 + set -- gzip xz bzip2 204 166 fi 205 167 206 168 # Run the tests given on the command line
+200 -32
tests/compare.rs
··· 1 - /// Compare the interoperability of cmprss with the official tools. 2 - 3 1 #[allow(dead_code)] 4 2 mod compare { 5 - /// Tests the tar comparisons 6 - #[test] 7 - #[cfg(feature = "interop")] 8 - fn tar() { 9 - test_with("tar"); 10 - } 3 + use assert_cmd::prelude::*; 4 + use assert_fs::fixture::PathChild; 5 + use std::path::PathBuf; 6 + use std::process::Command; 11 7 12 - /// Tests the gzip comparisons 13 - #[test] 14 - #[cfg(feature = "interop")] 15 - fn gzip() { 16 - test_with("gzip"); 17 - } 8 + mod utils { 9 + use super::*; 10 + use rand::Rng; 18 11 19 - /// Tests the bzip2 comparisons 20 - #[test] 21 - #[cfg(feature = "interop")] 22 - fn bzip2() { 23 - test_with("bzip2"); 24 - } 12 + /// Asserts that the two directories are identical 13 + pub fn assert_directory(left: PathBuf, right: PathBuf) { 14 + let left_entries = std::fs::read_dir(left).unwrap(); 15 + let right_entries = std::fs::read_dir(right).unwrap(); 25 16 26 - /// Tests the xz comparisons 27 - #[test] 28 - #[cfg(feature = "interop")] 29 - fn xz() { 30 - test_with("xz"); 17 + // Fix the sorting of entries, as it's platform dependent 18 + let mut left_entries: Vec<_> = left_entries.map(|entry| entry.unwrap()).collect(); 19 + let mut right_entries: Vec<_> = right_entries.map(|entry| entry.unwrap()).collect(); 20 + left_entries.sort_by_key(|entry| entry.file_name()); 21 + right_entries.sort_by_key(|entry| entry.file_name()); 22 + 23 + for (left_entry, right_entry) in left_entries.iter().zip(right_entries) { 24 + assert_eq!(left_entry.file_name(), right_entry.file_name()); 25 + 26 + let left_path = left_entry.path(); 27 + let right_path = right_entry.path(); 28 + 29 + if left_entry.file_type().unwrap().is_dir() { 30 + assert!(right_entry.file_type().unwrap().is_dir()); 31 + assert_directory(left_path, right_path); 32 + } else { 33 + let left_data = std::fs::read(&left_path).unwrap(); 34 + let right_data = std::fs::read(&right_path).unwrap(); 35 + 36 + assert_eq!(left_data, right_data); 37 + } 38 + } 39 + } 40 + 41 + /// Create a directory filled with random files 42 + pub fn create_random_files(dir: PathBuf, count: usize) { 43 + let mut rng = rand::thread_rng(); 44 + std::fs::create_dir_all(&dir).unwrap(); 45 + 46 + for i in 0..count { 47 + let path = format!("{}/file-{}", dir.display(), i); 48 + let data: Vec<u8> = (0..rng.gen_range(0..1000)).map(|_| rng.gen()).collect(); 49 + std::fs::write(path, data).unwrap(); 50 + } 51 + } 31 52 } 32 53 33 - /// Helper to spawn the script with any tool 34 - fn test_with(tool: &str) { 35 - let output = std::process::Command::new("bin/test.sh") 36 - .arg(tool) 37 - .output() 38 - .expect("Failed to execute command"); 39 - assert!(output.status.success()); 54 + mod tar { 55 + use super::*; 56 + 57 + fn tar_dir(dir: PathBuf, archive: PathBuf) { 58 + // The current directory matters, as tar uses the relative path for the paths inside the tar file 59 + std::process::Command::new("tar") 60 + .arg("cf") 61 + .arg(archive.to_str().unwrap()) 62 + .arg(dir.file_name().unwrap()) 63 + .current_dir(dir.parent().unwrap()) 64 + .output() 65 + .expect("Failed to execute command"); 66 + } 67 + 68 + fn untar_archive(archive: PathBuf, output_dir: PathBuf) { 69 + std::process::Command::new("tar") 70 + .arg("xf") 71 + .arg(archive.to_str().unwrap()) 72 + .arg("-C") 73 + .arg(output_dir.to_str().unwrap()) 74 + .output() 75 + .expect("Failed to execute command"); 76 + } 77 + 78 + fn cmprss_dir(dir: PathBuf, archive: PathBuf) { 79 + Command::cargo_bin("cmprss") 80 + .unwrap() 81 + .arg("tar") 82 + .arg(dir.to_str().unwrap()) 83 + .arg(archive.to_str().unwrap()) 84 + .assert() 85 + .success(); 86 + } 87 + 88 + fn dcmprss_archive(archive: PathBuf, output_dir: PathBuf) { 89 + Command::cargo_bin("cmprss") 90 + .unwrap() 91 + .arg("tar") 92 + .arg("--extract") 93 + .arg(archive.to_str().unwrap()) 94 + .arg(output_dir.to_str().unwrap()) 95 + .assert() 96 + .success(); 97 + } 98 + 99 + #[test] 100 + fn tar_tar() { 101 + let tmpdir = assert_fs::TempDir::new().unwrap(); 102 + let starting_dir = tmpdir.child("orig").to_path_buf(); 103 + let working_dir = assert_fs::TempDir::new().unwrap(); 104 + let archive = working_dir.child("tar.tar").to_path_buf(); 105 + 106 + utils::create_random_files(starting_dir.clone(), 10); 107 + 108 + // Run tar on the initial directory 109 + tar_dir(starting_dir.clone(), archive.clone()); 110 + 111 + // Untar the tar file using tar 112 + let output_dir = assert_fs::TempDir::new().unwrap(); 113 + untar_archive(archive, output_dir.to_path_buf()); 114 + 115 + // Now compare the two directories 116 + utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf()); 117 + } 118 + 119 + #[test] 120 + fn cmprss_cmprss() { 121 + let tmpdir = assert_fs::TempDir::new().unwrap(); 122 + let starting_dir = tmpdir.child("orig").to_path_buf(); 123 + let working_dir = assert_fs::TempDir::new().unwrap(); 124 + let archive = working_dir.child("tar.tar").to_path_buf(); 125 + 126 + utils::create_random_files(starting_dir.clone(), 10); 127 + 128 + // Run tar on the initial directory 129 + cmprss_dir(starting_dir.clone(), archive.clone()); 130 + 131 + // Untar the tar file using tar 132 + let output_dir = assert_fs::TempDir::new().unwrap(); 133 + dcmprss_archive(archive, output_dir.to_path_buf()); 134 + 135 + // Now compare the two directories 136 + utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf()); 137 + } 138 + 139 + #[test] 140 + fn cmprss_tar() { 141 + let tmpdir = assert_fs::TempDir::new().unwrap(); 142 + let starting_dir = tmpdir.child("orig").to_path_buf(); 143 + let working_dir = assert_fs::TempDir::new().unwrap(); 144 + let archive = working_dir.child("tar.tar").to_path_buf(); 145 + 146 + utils::create_random_files(starting_dir.clone(), 10); 147 + 148 + // Run tar on the initial directory 149 + cmprss_dir(starting_dir.clone(), archive.clone()); 150 + 151 + // Untar the tar file using tar 152 + let output_dir = assert_fs::TempDir::new().unwrap(); 153 + untar_archive(archive, output_dir.to_path_buf()); 154 + 155 + // Now compare the two directories 156 + utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf()); 157 + } 158 + 159 + #[test] 160 + fn tar_cmprss() { 161 + let tmpdir = assert_fs::TempDir::new().unwrap(); 162 + let starting_dir = tmpdir.child("orig").to_path_buf(); 163 + let working_dir = assert_fs::TempDir::new().unwrap(); 164 + let archive = working_dir.child("tar.tar").to_path_buf(); 165 + 166 + utils::create_random_files(starting_dir.clone(), 10); 167 + 168 + // Run tar on the initial directory 169 + tar_dir(starting_dir.clone(), archive.clone()); 170 + 171 + // Untar the tar file using tar 172 + let output_dir = assert_fs::TempDir::new().unwrap(); 173 + dcmprss_archive(archive, output_dir.to_path_buf()); 174 + 175 + // Now compare the two directories 176 + utils::assert_directory(output_dir.to_path_buf(), tmpdir.to_path_buf()); 177 + } 40 178 } 179 + 180 + // /// Tests the gzip comparisons 181 + // #[test] 182 + // #[cfg(feature = "interop")] 183 + // fn gzip() { 184 + // test_with("gzip"); 185 + // } 186 + 187 + // /// Tests the bzip2 comparisons 188 + // #[test] 189 + // #[cfg(feature = "interop")] 190 + // fn bzip2() { 191 + // test_with("bzip2"); 192 + // } 193 + 194 + // /// Tests the xz comparisons 195 + // #[test] 196 + // #[cfg(feature = "interop")] 197 + // fn xz() { 198 + // test_with("xz"); 199 + // } 200 + 201 + // /// Helper to spawn the script with any tool 202 + // fn test_with(tool: &str) { 203 + // let output = std::process::Command::new("bin/test.sh") 204 + // .arg(tool) 205 + // .output() 206 + // .expect("Failed to execute command"); 207 + // assert!(output.status.success()); 208 + // } 41 209 }