this repo has no description
0
fork

Configure Feed

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

refactor(tests): reorganize test files by backend and type

+1431 -1386
+139
tests/bzip2.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::{ 5 + fs::File, 6 + process::{Command, Stdio}, 7 + }; 8 + 9 + mod common; 10 + use common::*; 11 + 12 + mod bzip2 { 13 + use super::*; 14 + 15 + mod roundtrip { 16 + use super::*; 17 + 18 + /// Bzip2 roundtrip using files 19 + /// Compressing: input = test.txt, output = test.txt.bz2 20 + /// Extracting: input = test.txt.bz2, output = test.txt 21 + /// 22 + /// ``` bash 23 + /// cmprss bzip2 test.txt test.txt.bz2 24 + /// cmprss bzip2 --extract --ignore-pipes test.txt.bz2 25 + /// ``` 26 + #[test] 27 + fn explicit() -> Result<(), Box<dyn std::error::Error>> { 28 + let file = create_test_file("test.txt", "garbage data for testing")?; 29 + let working_dir = create_working_dir()?; 30 + let archive = working_dir.child("test.txt.bz2"); 31 + archive.assert(predicate::path::missing()); 32 + 33 + let mut compress = Command::cargo_bin("cmprss")?; 34 + compress 35 + .current_dir(&working_dir) 36 + .arg("bzip2") 37 + .arg(file.path()) 38 + .arg(archive.path()); 39 + compress.assert().success(); 40 + archive.assert(predicate::path::is_file()); 41 + 42 + let mut extract = Command::cargo_bin("cmprss")?; 43 + extract 44 + .current_dir(&working_dir) 45 + .arg("bzip2") 46 + .arg("--ignore-pipes") 47 + .arg("--extract") 48 + .arg(archive.path()); 49 + extract.assert().success(); 50 + 51 + // Assert the files are identical 52 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 53 + 54 + Ok(()) 55 + } 56 + 57 + /// Bzip2 roundtrip using stdin 58 + /// Compressing: input = stdin, output = test.txt.bz2 59 + /// Extracting: input = stdin(test.txt.bz2), output = test.txt 60 + /// 61 + /// ``` bash 62 + /// cat test.txt | cmprss bzip2 test.txt.bz2 63 + /// cat test.txt.bz2 | cmprss bzip2 --extract out.txt 64 + /// ``` 65 + #[test] 66 + fn stdin() -> Result<(), Box<dyn std::error::Error>> { 67 + let file = create_test_file("test.txt", "garbage data for testing")?; 68 + let working_dir = create_working_dir()?; 69 + let archive = working_dir.child("test.txt.bz2"); 70 + archive.assert(predicate::path::missing()); 71 + 72 + // Pipe file to stdin 73 + let mut compress = Command::cargo_bin("cmprss")?; 74 + compress 75 + .current_dir(&working_dir) 76 + .arg("bzip2") 77 + .arg("test.txt.bz2") 78 + .stdin(Stdio::from(File::open(file.path())?)); 79 + compress.assert().success(); 80 + archive.assert(predicate::path::is_file()); 81 + 82 + let mut extract = Command::cargo_bin("cmprss")?; 83 + extract 84 + .current_dir(&working_dir) 85 + .arg("bzip2") 86 + .stdin(Stdio::from(File::open(archive.path())?)) 87 + .arg("--extract") 88 + .arg("out.txt"); 89 + extract.assert().success(); 90 + 91 + // Assert the files are identical 92 + assert_files_equal(file.path(), &working_dir.child("out.txt")); 93 + 94 + Ok(()) 95 + } 96 + 97 + /// Bzip2 roundtrip using stdout 98 + /// Compressing: input = test.txt, output = stdout 99 + /// Extracting: input = test.txt.bz2, output = stdout 100 + /// 101 + /// ``` bash 102 + /// cmprss bzip2 test.txt > test.txt.bz2 103 + /// cmprss bzip2 --extract test.txt.bz2 > out.txt 104 + /// ``` 105 + #[test] 106 + fn stdout() -> Result<(), Box<dyn std::error::Error>> { 107 + let file = create_test_file("test.txt", "garbage data for testing")?; 108 + let working_dir = create_working_dir()?; 109 + let archive = working_dir.child("test.txt.bz2"); 110 + archive.assert(predicate::path::missing()); 111 + 112 + // Compress file to stdout 113 + let mut compress = Command::cargo_bin("cmprss")?; 114 + compress 115 + .current_dir(&working_dir) 116 + .arg("bzip2") 117 + .arg(file.path()) 118 + .stdout(Stdio::from(File::create(archive.path())?)); 119 + compress.assert().success(); 120 + archive.assert(predicate::path::is_file()); 121 + 122 + // Extract file to stdout 123 + let mut extract = Command::cargo_bin("cmprss")?; 124 + extract 125 + .current_dir(&working_dir) 126 + .arg("bzip2") 127 + .arg("--ignore-stdin") 128 + .arg("--extract") 129 + .arg(archive.path()) 130 + .arg("out.txt"); 131 + extract.assert().success(); 132 + 133 + // Assert the files are identical 134 + assert_files_equal(file.path(), &working_dir.child("out.txt")); 135 + 136 + Ok(()) 137 + } 138 + } 139 + }
-1386
tests/cli.rs
··· 1 - mod cli { 2 - use assert_cmd::prelude::*; 3 - use assert_fs::prelude::*; 4 - use predicates::prelude::*; 5 - use std::{ 6 - fs::File, 7 - path::PathBuf, 8 - process::{Command, Stdio}, 9 - }; 10 - 11 - /// Tar roundtrip with a single file 12 - /// 13 - /// ``` bash 14 - /// cmprss tar test.txt archive.tar 15 - /// cmprss tar --extract archive.tar . 16 - /// ``` 17 - #[test] 18 - fn tar_roundtrip_explicit() -> Result<(), Box<dyn std::error::Error>> { 19 - let file = assert_fs::NamedTempFile::new("test.txt")?; 20 - file.write_str("garbage data for testing")?; 21 - let working_dir = assert_fs::TempDir::new()?; 22 - let archive = working_dir.child("archive.tar"); 23 - archive.assert(predicate::path::missing()); 24 - 25 - let mut compress = Command::cargo_bin("cmprss")?; 26 - compress.arg("tar").arg(file.path()).arg(archive.path()); 27 - compress.assert().success(); 28 - archive.assert(predicate::path::is_file()); 29 - 30 - let mut extract = Command::cargo_bin("cmprss")?; 31 - extract 32 - .arg("tar") 33 - .arg("--extract") 34 - .arg(archive.path()) 35 - .arg(working_dir.path()); 36 - extract.assert().success(); 37 - 38 - // Assert the files are identical 39 - working_dir 40 - .child("test.txt") 41 - .assert(predicate::path::eq_file(file.path())); 42 - 43 - Ok(()) 44 - } 45 - 46 - /// Tar roundtrip with multiple files 47 - /// 48 - /// ``` bash 49 - /// cmprss tar test.txt test2.txt archive.tar 50 - /// cmprss tar --extract archive.tar . 51 - /// ``` 52 - #[test] 53 - fn tar_roundtrip_explicit_two() -> Result<(), Box<dyn std::error::Error>> { 54 - let file = assert_fs::NamedTempFile::new("test.txt")?; 55 - file.write_str("garbage data for testing")?; 56 - let file2 = assert_fs::NamedTempFile::new("test2.txt")?; 57 - file2.write_str("more garbage data for testing")?; 58 - let working_dir = assert_fs::TempDir::new()?; 59 - let archive = working_dir.child("archive.tar"); 60 - archive.assert(predicate::path::missing()); 61 - 62 - let mut compress = Command::cargo_bin("cmprss")?; 63 - compress 64 - .arg("tar") 65 - .arg(file.path()) 66 - .arg(file2.path()) 67 - .arg(archive.path()); 68 - compress.assert().success(); 69 - archive.assert(predicate::path::is_file()); 70 - 71 - let mut extract = Command::cargo_bin("cmprss")?; 72 - extract 73 - .arg("tar") 74 - .arg("--extract") 75 - .arg(archive.path()) 76 - .arg(working_dir.path()); 77 - extract.assert().success(); 78 - 79 - // Assert the files are identical 80 - working_dir 81 - .child("test.txt") 82 - .assert(predicate::path::eq_file(file.path())); 83 - working_dir 84 - .child("test2.txt") 85 - .assert(predicate::path::eq_file(file2.path())); 86 - 87 - Ok(()) 88 - } 89 - 90 - /// Tar roundtrip with a single file inferring output filename 91 - /// Compressing: output = './test.txt.tar' 92 - /// Extracting: output = '.' 93 - /// 94 - /// ``` bash 95 - /// cmprss tar test.txt 96 - /// cmprss tar --extract test.txt.tar 97 - /// ``` 98 - #[test] 99 - fn tar_roundtrip_implicit() -> Result<(), Box<dyn std::error::Error>> { 100 - let file = assert_fs::NamedTempFile::new("test.txt")?; 101 - file.write_str("garbage data for testing")?; 102 - let working_dir = assert_fs::TempDir::new()?.into_persistent(); 103 - let archive = working_dir.child("test.txt.tar"); 104 - archive.assert(predicate::path::missing()); 105 - 106 - let mut compress = Command::cargo_bin("cmprss")?; 107 - compress 108 - .current_dir(&working_dir) 109 - .arg("tar") 110 - .arg("--ignore-pipes") 111 - .arg(file.path()); 112 - compress.assert().success(); 113 - archive.assert(predicate::path::is_file()); 114 - 115 - let mut extract = Command::cargo_bin("cmprss")?; 116 - extract 117 - .current_dir(&working_dir) 118 - .arg("tar") 119 - .arg("--ignore-pipes") 120 - .arg("--extract") 121 - .arg(archive.path()); 122 - extract.assert().success(); 123 - 124 - // Assert the files are identical 125 - working_dir 126 - .child("test.txt") 127 - .assert(predicate::path::eq_file(file.path())); 128 - 129 - Ok(()) 130 - } 131 - 132 - /// Tar roundtrip with multiple files inferring output 133 - /// Uses the first file's name to generate the output filename 134 - /// Compressing: output = './test.txt.tar' 135 - /// Extracting: output = '.' 136 - /// 137 - /// ``` bash 138 - /// cmprss tar test.txt test2.txt 139 - /// cmprss tar test.txt.tar 140 - /// ``` 141 - #[test] 142 - fn tar_roundtrip_implicit_two() -> Result<(), Box<dyn std::error::Error>> { 143 - let file = assert_fs::NamedTempFile::new("test.txt")?; 144 - file.write_str("garbage data for testing")?; 145 - let file2 = assert_fs::NamedTempFile::new("test2.txt")?; 146 - file2.write_str("more garbage data for testing")?; 147 - let working_dir = assert_fs::TempDir::new()?.into_persistent(); 148 - let archive = working_dir.child("test.txt.tar"); 149 - archive.assert(predicate::path::missing()); 150 - 151 - let mut compress = Command::cargo_bin("cmprss")?; 152 - compress 153 - .current_dir(&working_dir) 154 - .arg("tar") 155 - .arg("--ignore-pipes") 156 - .arg(file.path()) 157 - .arg(file2.path()); 158 - compress.assert().success(); 159 - archive.assert(predicate::path::is_file()); 160 - 161 - let mut extract = Command::cargo_bin("cmprss")?; 162 - extract 163 - .current_dir(&working_dir) 164 - .arg("--ignore-pipes") 165 - .arg(archive.path()); 166 - extract.assert().success(); 167 - 168 - // Assert the files are identical 169 - working_dir 170 - .child("test.txt") 171 - .assert(predicate::path::eq_file(file.path())); 172 - working_dir 173 - .child("test2.txt") 174 - .assert(predicate::path::eq_file(file2.path())); 175 - 176 - Ok(()) 177 - } 178 - 179 - /// Gzip roundtrip using stdin 180 - /// Compressing: input = stdin, output = test.txt.gz 181 - /// Extracting: input = test.txt.gz, output = test.txt 182 - /// 183 - /// ``` bash 184 - /// cat test.txt | cmprss gzip test.txt.gz 185 - /// cmprss gzip --ignore-pipes --extract test.txt.gz 186 - /// ``` 187 - #[test] 188 - fn gzip_roundtrip_stdin() -> Result<(), Box<dyn std::error::Error>> { 189 - let file = assert_fs::NamedTempFile::new("test.txt")?; 190 - file.write_str("garbage data for testing")?; 191 - let working_dir = assert_fs::TempDir::new()?; 192 - let archive = working_dir.child("test.txt.gz"); 193 - archive.assert(predicate::path::missing()); 194 - 195 - // Pipe file to stdin 196 - let mut compress = Command::cargo_bin("cmprss")?; 197 - compress 198 - .current_dir(&working_dir) 199 - .arg("gzip") 200 - .arg("test.txt.gz") 201 - .stdin(Stdio::from(File::open(file.path())?)); 202 - compress.assert().success(); 203 - archive.assert(predicate::path::is_file()); 204 - 205 - let mut extract = Command::cargo_bin("cmprss")?; 206 - extract 207 - .current_dir(&working_dir) 208 - .arg("gzip") 209 - .arg("--ignore-pipes") 210 - .arg("--extract") 211 - .arg(archive.path()); 212 - extract.assert().success(); 213 - 214 - // Assert the files are identical 215 - working_dir 216 - .child("test.txt") 217 - .assert(predicate::path::eq_file(file.path())); 218 - 219 - Ok(()) 220 - } 221 - 222 - /// Gzip roundtrip using filename inference 223 - /// Compressing: input = stdin, output = default filename (archive.gz) 224 - /// Extracting: input = archive.gz, output = default filename (archive) 225 - /// 226 - /// ``` bash 227 - /// cat test.txt | cmprss gzip 228 - /// cmprss gzip --ignore-pipes --extract archive.gz 229 - /// ``` 230 - #[test] 231 - fn gzip_roundtrip_inferred_output_filenames() -> Result<(), Box<dyn std::error::Error>> { 232 - let file = assert_fs::NamedTempFile::new("test.txt")?; 233 - file.write_str("garbage data for testing")?; 234 - let working_dir = assert_fs::TempDir::new()?; 235 - let archive = working_dir.child("archive.gz"); // default filename 236 - archive.assert(predicate::path::missing()); 237 - 238 - // Pipe file to stdin 239 - let mut compress = Command::cargo_bin("cmprss")?; 240 - compress 241 - .current_dir(&working_dir) 242 - .arg("gzip") 243 - .arg("--ignore-stdout") 244 - .stdin(Stdio::from(File::open(file.path())?)); 245 - compress.assert().success(); 246 - archive.assert(predicate::path::is_file()); 247 - 248 - let mut extract = Command::cargo_bin("cmprss")?; 249 - extract 250 - .current_dir(&working_dir) 251 - .arg("gzip") 252 - .arg("--ignore-pipes") 253 - .arg("--extract") 254 - .arg(archive.path()); 255 - extract.assert().success(); 256 - 257 - // Assert the files are identical 258 - working_dir 259 - .child("archive") 260 - .assert(predicate::path::eq_file(file.path())); 261 - 262 - Ok(()) 263 - } 264 - 265 - /// Xz roundtrip using files 266 - /// Compressing: input = test.txt, output = test.txt.xz 267 - /// Extracting: input = test.txt.xz, output = test.txt 268 - /// 269 - /// ``` bash 270 - /// cmprss xz test.txt test.txt.xz 271 - /// cmprss xz --extract --ignore-pipes test.txt.xz 272 - /// ``` 273 - #[test] 274 - fn xz_roundtrip_explicit() -> Result<(), Box<dyn std::error::Error>> { 275 - let file = assert_fs::NamedTempFile::new("test.txt")?; 276 - file.write_str("garbage data for testing")?; 277 - let working_dir = assert_fs::TempDir::new()?; 278 - let archive = working_dir.child("test.txt.xz"); 279 - archive.assert(predicate::path::missing()); 280 - 281 - let mut compress = Command::cargo_bin("cmprss")?; 282 - compress 283 - .current_dir(&working_dir) 284 - .arg("xz") 285 - .arg(file.path()) 286 - .arg(archive.path()); 287 - compress.assert().success(); 288 - archive.assert(predicate::path::is_file()); 289 - 290 - let mut extract = Command::cargo_bin("cmprss")?; 291 - extract 292 - .current_dir(&working_dir) 293 - .arg("xz") 294 - .arg("--ignore-pipes") 295 - .arg("--extract") 296 - .arg(archive.path()); 297 - extract.assert().success(); 298 - 299 - // Assert the files are identical 300 - working_dir 301 - .child("test.txt") 302 - .assert(predicate::path::eq_file(file.path())); 303 - 304 - Ok(()) 305 - } 306 - 307 - /// Xz roundtrip using stdin 308 - /// Compressing: input = stdin, output = test.txt.xz 309 - /// Extracting: input = stdin(test.txt.xz), output = test.txt 310 - /// 311 - /// ``` bash 312 - /// cat test.txt | cmprss xz test.txt.xz 313 - /// cat test.txt.xz | cmprss xz --extract out.txt 314 - /// ``` 315 - #[test] 316 - fn xz_roundtrip_stdin() -> Result<(), Box<dyn std::error::Error>> { 317 - let file = assert_fs::NamedTempFile::new("test.txt")?; 318 - file.write_str("garbage data for testing")?; 319 - let working_dir = assert_fs::TempDir::new()?; 320 - let archive = working_dir.child("test.txt.xz"); 321 - archive.assert(predicate::path::missing()); 322 - 323 - // Pipe file to stdin 324 - let mut compress = Command::cargo_bin("cmprss")?; 325 - compress 326 - .current_dir(&working_dir) 327 - .arg("xz") 328 - .arg("test.txt.xz") 329 - .stdin(Stdio::from(File::open(file.path())?)); 330 - compress.assert().success(); 331 - archive.assert(predicate::path::is_file()); 332 - 333 - let mut extract = Command::cargo_bin("cmprss")?; 334 - extract 335 - .current_dir(&working_dir) 336 - .arg("xz") 337 - .stdin(Stdio::from(File::open(archive.path())?)) 338 - .arg("--extract") 339 - .arg("out.txt"); 340 - extract.assert().success(); 341 - 342 - // Assert the files are identical 343 - working_dir 344 - .child("out.txt") 345 - .assert(predicate::path::eq_file(file.path())); 346 - 347 - Ok(()) 348 - } 349 - 350 - /// Xz roundtrip using stdout 351 - /// Compressing: input = test.txt, output = stdout 352 - /// Extracting: input = test.txt.xz, output = stdout 353 - /// 354 - /// ``` bash 355 - /// cmprss xz test.txt > test.txt.xz 356 - /// cmprss xz --extract test.txt.xz > out.txt 357 - /// ``` 358 - #[test] 359 - fn xz_roundtrip_stdout() -> Result<(), Box<dyn std::error::Error>> { 360 - let file = assert_fs::NamedTempFile::new("test.txt")?; 361 - file.write_str("garbage data for testing")?; 362 - let working_dir = assert_fs::TempDir::new()?; 363 - let archive = working_dir.child("test.txt.xz"); 364 - archive.assert(predicate::path::missing()); 365 - 366 - // Compress file to stdout 367 - let mut compress = Command::cargo_bin("cmprss")?; 368 - compress 369 - .current_dir(&working_dir) 370 - .arg("xz") 371 - .arg(file.path()) 372 - .stdout(Stdio::from(File::create(archive.path())?)); 373 - compress.assert().success(); 374 - archive.assert(predicate::path::is_file()); 375 - 376 - // Extract file to stdout 377 - let mut extract = Command::cargo_bin("cmprss")?; 378 - extract 379 - .current_dir(&working_dir) 380 - .arg("xz") 381 - .arg("--ignore-stdin") 382 - .arg("--extract") 383 - .arg(archive.path()) 384 - .arg("out.txt"); 385 - // TODO: This fails, but manual testing shows it works fine 386 - //.stdout(Stdio::from(File::create("out.txt")?)); 387 - extract.assert().success(); 388 - 389 - // Assert the files are identical 390 - working_dir 391 - .child("out.txt") 392 - .assert(predicate::path::eq_file(file.path())); 393 - 394 - Ok(()) 395 - } 396 - 397 - /// Bzip2 roundtrip using files 398 - /// Compressing: input = test.txt, output = test.txt.bz2 399 - /// Extracting: input = test.txt.bz2, output = test.txt 400 - /// 401 - /// ``` bash 402 - /// cmprss bzip2 test.txt test.txt.bz2 403 - /// cmprss bzip2 --extract --ignore-pipes test.txt.bz2 404 - /// ``` 405 - #[test] 406 - fn bzip2_roundtrip_explicit() -> Result<(), Box<dyn std::error::Error>> { 407 - let file = assert_fs::NamedTempFile::new("test.txt")?; 408 - file.write_str("garbage data for testing")?; 409 - 410 - let working_dir = assert_fs::TempDir::new()?; 411 - let archive = working_dir.child("test.txt.bz2"); 412 - archive.assert(predicate::path::missing()); 413 - 414 - let mut compress = Command::cargo_bin("cmprss")?; 415 - compress 416 - .current_dir(&working_dir) 417 - .arg("bzip2") 418 - .arg(file.path()) 419 - .arg(archive.path()); 420 - compress.assert().success(); 421 - archive.assert(predicate::path::is_file()); 422 - 423 - let mut extract = Command::cargo_bin("cmprss")?; 424 - extract 425 - .current_dir(&working_dir) 426 - .arg("bzip2") 427 - .arg("--ignore-pipes") 428 - .arg("--extract") 429 - .arg(archive.path()); 430 - extract.assert().success(); 431 - 432 - // Assert the files are identical 433 - working_dir 434 - .child("test.txt") 435 - .assert(predicate::path::eq_file(file.path())); 436 - 437 - Ok(()) 438 - } 439 - 440 - /// Bzip2 roundtrip using stdin 441 - /// Compressing: input = stdin, output = test.txt.bz2 442 - /// Extracting: input = stdin(test.txt.bz2), output = test.txt 443 - /// 444 - /// ``` bash 445 - /// cat test.txt | cmprss bzip2 test.txt.bz2 446 - /// cat test.txt.bz2 | cmprss bzip2 --extract out.txt 447 - /// ``` 448 - #[test] 449 - fn bzip2_roundtrip_stdin() -> Result<(), Box<dyn std::error::Error>> { 450 - let file = assert_fs::NamedTempFile::new("test.txt")?; 451 - file.write_str("garbage data for testing")?; 452 - 453 - let working_dir = assert_fs::TempDir::new()?; 454 - let archive = working_dir.child("test.txt.bz2"); 455 - archive.assert(predicate::path::missing()); 456 - 457 - // Pipe file to stdin 458 - let mut compress = Command::cargo_bin("cmprss")?; 459 - compress 460 - .current_dir(&working_dir) 461 - .arg("bzip2") 462 - .arg("test.txt.bz2") 463 - .stdin(Stdio::from(File::open(file.path())?)); 464 - compress.assert().success(); 465 - archive.assert(predicate::path::is_file()); 466 - 467 - let mut extract = Command::cargo_bin("cmprss")?; 468 - extract 469 - .current_dir(&working_dir) 470 - .arg("bzip2") 471 - .stdin(Stdio::from(File::open(archive.path())?)) 472 - .arg("--extract") 473 - .arg("out.txt"); 474 - extract.assert().success(); 475 - 476 - // Assert the files are identical 477 - working_dir 478 - .child("out.txt") 479 - .assert(predicate::path::eq_file(file.path())); 480 - 481 - Ok(()) 482 - } 483 - 484 - /// Bzip2 roundtrip using stdout 485 - /// Compressing: input = test.txt, output = stdout 486 - /// Extracting: input = test.txt.bz2, output = stdout 487 - /// 488 - /// ``` bash 489 - /// cmprss bzip2 test.txt > test.txt.bz2 490 - /// cmprss bzip2 --extract test.txt.bz2 > out.txt 491 - /// ``` 492 - #[test] 493 - fn bzip2_roundtrip_stdout() -> Result<(), Box<dyn std::error::Error>> { 494 - let file = assert_fs::NamedTempFile::new("test.txt")?; 495 - file.write_str("garbage data for testing")?; 496 - 497 - let working_dir = assert_fs::TempDir::new()?; 498 - let archive = working_dir.child("test.txt.bz2"); 499 - archive.assert(predicate::path::missing()); 500 - 501 - // Compress file to stdout 502 - let mut compress = Command::cargo_bin("cmprss")?; 503 - compress 504 - .current_dir(&working_dir) 505 - .arg("bzip2") 506 - .arg(file.path()) 507 - .stdout(Stdio::from(File::create(archive.path())?)); 508 - compress.assert().success(); 509 - archive.assert(predicate::path::is_file()); 510 - 511 - // Extract file to stdout 512 - let mut extract = Command::cargo_bin("cmprss")?; 513 - extract 514 - .current_dir(&working_dir) 515 - .arg("bzip2") 516 - .arg("--ignore-stdin") 517 - .arg("--extract") 518 - .arg(archive.path()) 519 - .arg("out.txt"); 520 - // TODO: This fails, but manual testing shows it works fine 521 - //.stdout(Stdio::from(File::create("out.txt")?)); 522 - extract.assert().success(); 523 - 524 - // Assert the files are identical 525 - working_dir 526 - .child("out.txt") 527 - .assert(predicate::path::eq_file(file.path())); 528 - 529 - Ok(()) 530 - } 531 - 532 - /// Zip roundtrip with a single file 533 - /// 534 - /// ``` bash 535 - /// cmprss zip test.txt archive.zip 536 - /// cmprss zip --extract archive.zip . 537 - /// ``` 538 - #[test] 539 - fn zip_roundtrip_explicit() -> Result<(), Box<dyn std::error::Error>> { 540 - let file = assert_fs::NamedTempFile::new("test.txt")?; 541 - file.write_str("garbage data for testing")?; 542 - let working_dir = assert_fs::TempDir::new()?; 543 - let archive = working_dir.child("archive.zip"); 544 - archive.assert(predicate::path::missing()); 545 - 546 - let mut compress = Command::cargo_bin("cmprss")?; 547 - compress.arg("zip").arg(file.path()).arg(archive.path()); 548 - compress.assert().success(); 549 - archive.assert(predicate::path::is_file()); 550 - 551 - let mut extract = Command::cargo_bin("cmprss")?; 552 - extract 553 - .arg("zip") 554 - .arg("--extract") 555 - .arg(archive.path()) 556 - .arg(working_dir.path()); 557 - extract.assert().success(); 558 - 559 - // Assert the files are identical 560 - working_dir 561 - .child("test.txt") 562 - .assert(predicate::path::eq_file(file.path())); 563 - 564 - Ok(()) 565 - } 566 - 567 - /// Zip roundtrip with multiple files 568 - /// 569 - /// ``` bash 570 - /// cmprss zip test.txt test2.txt archive.zip 571 - /// cmprss zip --extract archive.zip . 572 - /// ``` 573 - #[test] 574 - fn zip_roundtrip_explicit_two() -> Result<(), Box<dyn std::error::Error>> { 575 - let file = assert_fs::NamedTempFile::new("test.txt")?; 576 - file.write_str("garbage data for testing")?; 577 - let file2 = assert_fs::NamedTempFile::new("test2.txt")?; 578 - file2.write_str("more garbage data for testing")?; 579 - let working_dir = assert_fs::TempDir::new()?; 580 - let archive = working_dir.child("archive.zip"); 581 - archive.assert(predicate::path::missing()); 582 - 583 - let mut compress = Command::cargo_bin("cmprss")?; 584 - compress 585 - .arg("zip") 586 - .arg(file.path()) 587 - .arg(file2.path()) 588 - .arg(archive.path()); 589 - compress.assert().success(); 590 - archive.assert(predicate::path::is_file()); 591 - 592 - let mut extract = Command::cargo_bin("cmprss")?; 593 - extract 594 - .arg("zip") 595 - .arg("--extract") 596 - .arg(archive.path()) 597 - .arg(working_dir.path()); 598 - extract.assert().success(); 599 - 600 - // Assert the files are identical 601 - working_dir 602 - .child("test.txt") 603 - .assert(predicate::path::eq_file(file.path())); 604 - working_dir 605 - .child("test2.txt") 606 - .assert(predicate::path::eq_file(file2.path())); 607 - 608 - Ok(()) 609 - } 610 - 611 - /// Zip roundtrip with a directory 612 - /// 613 - /// ``` bash 614 - /// cmprss zip directory archive.zip 615 - /// cmprss zip --extract archive.zip output_dir 616 - /// ``` 617 - #[test] 618 - fn zip_roundtrip_directory() -> Result<(), Box<dyn std::error::Error>> { 619 - let dir = assert_fs::TempDir::new()?; 620 - let file = dir.child("test.txt"); 621 - file.write_str("garbage data for testing")?; 622 - let file2 = dir.child("test2.txt"); 623 - file2.write_str("more garbage data for testing")?; 624 - 625 - let working_dir = assert_fs::TempDir::new()?; 626 - let archive = working_dir.child("archive.zip"); 627 - archive.assert(predicate::path::missing()); 628 - 629 - let mut compress = Command::cargo_bin("cmprss")?; 630 - compress.arg("zip").arg(dir.path()).arg(archive.path()); 631 - compress.assert().success(); 632 - archive.assert(predicate::path::is_file()); 633 - 634 - let extract_dir = working_dir.child("output"); 635 - std::fs::create_dir_all(extract_dir.path())?; 636 - 637 - let mut extract = Command::cargo_bin("cmprss")?; 638 - extract 639 - .arg("zip") 640 - .arg("--extract") 641 - .arg(archive.path()) 642 - .arg(extract_dir.path()); 643 - extract.assert().success(); 644 - 645 - // Assert the files are identical 646 - // Since the archive stores the entire directory, the extracted file is contained in the directory 647 - let dir_name: PathBuf = dir.path().file_name().unwrap().into(); 648 - extract_dir 649 - .child(&dir_name) 650 - .child("test.txt") 651 - .assert(predicate::path::eq_file(file.path())); 652 - extract_dir 653 - .child(&dir_name) 654 - .child("test2.txt") 655 - .assert(predicate::path::eq_file(file2.path())); 656 - 657 - Ok(()) 658 - } 659 - 660 - /// Magic roundtrip using stdin 661 - /// Compressing: input = stdin, output = test.txt.gz 662 - /// Extracting: input = test.txt.gz, output = test.txt 663 - /// 664 - /// ``` bash 665 - /// cat test.txt | cmprss test.txt.gz 666 - /// cmprss gz --extract test.txt.gz out.txt 667 - /// ``` 668 - #[test] 669 - fn magic_roundtrip_stdin() -> Result<(), Box<dyn std::error::Error>> { 670 - let file = assert_fs::NamedTempFile::new("test.txt")?; 671 - file.write_str("garbage data for testing")?; 672 - 673 - let working_dir = assert_fs::TempDir::new()?; 674 - let archive = working_dir.child("test.txt.gz"); 675 - archive.assert(predicate::path::missing()); 676 - 677 - // Pipe file to stdin 678 - let mut compress = Command::cargo_bin("cmprss")?; 679 - compress 680 - .current_dir(&working_dir) 681 - .arg("--ignore-stdout") 682 - .arg("test.txt.gz") 683 - .stdin(Stdio::from(File::open(file.path())?)); 684 - compress.assert().success(); 685 - archive.assert(predicate::path::is_file()); 686 - 687 - let mut extract = Command::cargo_bin("cmprss")?; 688 - extract 689 - .current_dir(&working_dir) 690 - .arg("gz") 691 - .arg("--ignore-pipes") 692 - .arg("--extract") 693 - .arg("test.txt.gz"); 694 - extract.assert().success(); 695 - 696 - // Assert the files are identical 697 - working_dir 698 - .child("test.txt") 699 - .assert(predicate::path::eq_file(file.path())); 700 - 701 - Ok(()) 702 - } 703 - 704 - /// Magic roundtrip using files 705 - /// Compressing: input = test.txt, output = test.txt.gz 706 - /// Extracting: input = test.txt.gz, output = stdout 707 - /// 708 - /// ``` bash 709 - /// cmprss test.txt test.txt.gz 710 - /// cmprss test.txt.gz out.txt 711 - /// ``` 712 - #[test] 713 - fn magic_roundtrip_files() -> Result<(), Box<dyn std::error::Error>> { 714 - let file = assert_fs::NamedTempFile::new("test.txt")?; 715 - file.write_str("garbage data for testing")?; 716 - 717 - let working_dir = assert_fs::TempDir::new()?; 718 - let archive = working_dir.child("test.txt.gz"); 719 - archive.assert(predicate::path::missing()); 720 - 721 - // Compress file to an archive 722 - let mut compress = Command::cargo_bin("cmprss")?; 723 - compress 724 - .current_dir(&working_dir) 725 - .arg("--ignore-pipes") 726 - .arg(file.path()) 727 - .arg("test.txt.gz"); 728 - compress.assert().success(); 729 - archive.assert(predicate::path::is_file()); 730 - 731 - // Extract file to given file 732 - let mut extract = Command::cargo_bin("cmprss")?; 733 - extract 734 - .current_dir(&working_dir) 735 - .arg("--ignore-pipes") 736 - .arg("test.txt.gz") 737 - .arg("out.txt"); 738 - extract.assert().success(); 739 - 740 - // Assert the files are identical 741 - working_dir 742 - .child("out.txt") 743 - .assert(predicate::path::eq_file(file.path())); 744 - 745 - Ok(()) 746 - } 747 - 748 - /// Magic roundtrip using stdout decompression 749 - /// Compressing: input = test.txt, output = test.txt.gz 750 - /// Extracting: input = test.txt.gz, output = stdout 751 - /// 752 - /// ``` bash 753 - /// cmprss test.txt test.txt.gz 754 - /// cmprss test.txt.gz > out.txt 755 - /// ``` 756 - #[test] 757 - fn magic_roundtrip_stdout_decompression() -> Result<(), Box<dyn std::error::Error>> { 758 - let file = assert_fs::NamedTempFile::new("test.txt")?; 759 - file.write_str("garbage data for testing")?; 760 - 761 - let working_dir = assert_fs::TempDir::new()?; 762 - let archive = working_dir.child("test.txt.gz"); 763 - archive.assert(predicate::path::missing()); 764 - 765 - let out_file = working_dir.child("out.txt"); 766 - 767 - // Compress file to an archive 768 - let mut compress = Command::cargo_bin("cmprss")?; 769 - compress 770 - .current_dir(&working_dir) 771 - .arg("--ignore-pipes") 772 - .arg(file.path()) 773 - .arg("test.txt.gz"); 774 - compress.assert().success(); 775 - archive.assert(predicate::path::is_file()); 776 - 777 - // Extract file to stdout 778 - let mut extract = Command::cargo_bin("cmprss")?; 779 - extract 780 - .current_dir(&working_dir) 781 - .arg("--ignore-stdin") 782 - .arg("test.txt.gz") 783 - .stdout(Stdio::from(File::create(&out_file)?)); 784 - extract.assert().success(); 785 - 786 - // Assert the files are identical 787 - out_file.assert(predicate::path::eq_file(file.path())); 788 - Ok(()) 789 - } 790 - 791 - /// Magic roundtrip using stdin compression 792 - /// Compressing: input = stdin, output = test.txt.gz 793 - /// Extracting: input = test.txt.gz, output = test.txt 794 - /// 795 - /// ``` bash 796 - /// cat test.txt | cmprss test.txt.gz 797 - /// cmprss test.txt.gz out.txt 798 - /// ``` 799 - #[test] 800 - fn magic_roundtrip_stdin_compression() -> Result<(), Box<dyn std::error::Error>> { 801 - let file = assert_fs::NamedTempFile::new("test.txt")?; 802 - file.write_str("garbage data for testing")?; 803 - 804 - let working_dir = assert_fs::TempDir::new()?; 805 - let archive = working_dir.child("test.txt.gz"); 806 - archive.assert(predicate::path::missing()); 807 - 808 - let out_file = working_dir.child("out.txt"); 809 - 810 - // Compress stdin to an archive 811 - let mut compress = Command::cargo_bin("cmprss")?; 812 - compress 813 - .current_dir(&working_dir) 814 - .arg("--ignore-stdout") 815 - .arg("test.txt.gz") 816 - .stdin(Stdio::from(File::open(file.path())?)); 817 - compress.assert().success(); 818 - archive.assert(predicate::path::is_file()); 819 - 820 - // Extract file to given file 821 - let mut extract = Command::cargo_bin("cmprss")?; 822 - extract 823 - .current_dir(&working_dir) 824 - .arg("--ignore-pipes") 825 - .arg("test.txt.gz") 826 - .arg(out_file.path()); 827 - extract.assert().success(); 828 - 829 - // Assert the files are identical 830 - out_file.assert(predicate::path::eq_file(file.path())); 831 - 832 - Ok(()) 833 - } 834 - 835 - /// Magic roundtrip using default filenames 836 - /// Compressing: input = test.txt, output = test.txt.gz 837 - /// Extracting: input = test.txt.gz, output = <default> 838 - /// 839 - /// ``` bash 840 - /// cmprss test.txt test.txt.gz 841 - /// cmprss test.txt.gz 842 - /// ``` 843 - #[test] 844 - fn magic_roundtrip_default_filenames() -> Result<(), Box<dyn std::error::Error>> { 845 - let file = assert_fs::NamedTempFile::new("test.txt")?; 846 - file.write_str("garbage data for testing")?; 847 - 848 - let working_dir = assert_fs::TempDir::new()?; 849 - let archive = working_dir.child("test.txt.gz"); 850 - archive.assert(predicate::path::missing()); 851 - 852 - // Compress file to an archive 853 - let mut compress = Command::cargo_bin("cmprss")?; 854 - compress 855 - .current_dir(&working_dir) 856 - .arg("--ignore-pipes") 857 - .arg(file.path()) 858 - .arg("test.txt.gz"); 859 - compress.assert().success(); 860 - archive.assert(predicate::path::is_file()); 861 - 862 - // Extract file to default filename 863 - let mut extract = Command::cargo_bin("cmprss")?; 864 - extract 865 - .current_dir(&working_dir) 866 - .arg("--ignore-pipes") 867 - .arg("test.txt.gz"); 868 - extract.assert().success(); 869 - 870 - // Assert the files are identical 871 - working_dir 872 - .child("test.txt") 873 - .assert(predicate::path::eq_file(file.path())); 874 - 875 - Ok(()) 876 - } 877 - 878 - /// Magic roundtrip using multiple files with tar 879 - /// Compressing: input = test.txt/test2.txt, output = archive.tar 880 - /// Extracting: input = archive.tar, output = <default> 881 - /// 882 - /// ``` bash 883 - /// cmprss test.txt test2.txt archive.tar 884 - /// cmprss archive.tar 885 - /// ``` 886 - #[test] 887 - fn magic_roundtrip_multiple_files_tar() -> Result<(), Box<dyn std::error::Error>> { 888 - let file = assert_fs::NamedTempFile::new("test.txt")?; 889 - file.write_str("garbage data for testing")?; 890 - let file2 = assert_fs::NamedTempFile::new("test2.txt")?; 891 - file2.write_str("more garbage data for testing")?; 892 - 893 - let working_dir = assert_fs::TempDir::new()?; 894 - let archive = working_dir.child("archive.tar"); 895 - archive.assert(predicate::path::missing()); 896 - 897 - // Compress files to an archive 898 - let mut compress = Command::cargo_bin("cmprss")?; 899 - compress 900 - .current_dir(&working_dir) 901 - .arg("--ignore-pipes") 902 - .arg(file.path()) 903 - .arg(file2.path()) 904 - .arg("archive.tar"); 905 - compress.assert().success(); 906 - archive.assert(predicate::path::is_file()); 907 - 908 - // Extract file to default filename 909 - let mut extract = Command::cargo_bin("cmprss")?; 910 - extract 911 - .current_dir(&working_dir) 912 - .arg("--ignore-pipes") 913 - .arg("archive.tar"); 914 - extract.assert().success(); 915 - 916 - // Assert the files are identical 917 - working_dir 918 - .child("test.txt") 919 - .assert(predicate::path::eq_file(file.path())); 920 - working_dir 921 - .child("test2.txt") 922 - .assert(predicate::path::eq_file(file2.path())); 923 - 924 - Ok(()) 925 - } 926 - 927 - /// Magic roundtrip with tar.gz 928 - /// Infer things as much as possible 929 - /// Compressing: input = test.txt + test2.txt, output = test.tar.gz 930 - /// Extracting: input = test.tar.gz, output = test.txt + test2.txt 931 - /// 932 - /// ``` bash 933 - /// cmprss test.txt test2.txt archive.tar 934 - /// cmprss archive.tar archive.tar.gz 935 - /// cmprss archive.tar.gz archive.tar 936 - /// cmprss archive.tar 937 - /// ``` 938 - #[test] 939 - fn magic_roundtrip_tar_gz() -> Result<(), Box<dyn std::error::Error>> { 940 - let file = assert_fs::NamedTempFile::new("test.txt")?; 941 - file.write_str("garbage data for testing")?; 942 - let file2 = assert_fs::NamedTempFile::new("test2.txt")?; 943 - file2.write_str("more garbage data for testing")?; 944 - 945 - let working_dir = assert_fs::TempDir::new()?; 946 - let archive = working_dir.child("archive.tar"); 947 - archive.assert(predicate::path::missing()); 948 - let archive2 = working_dir.child("archive.tar.gz"); 949 - archive2.assert(predicate::path::missing()); 950 - 951 - let extract_dir = assert_fs::TempDir::new()?; 952 - 953 - // Compress files to an archive 954 - let mut compress = Command::cargo_bin("cmprss")?; 955 - compress 956 - .current_dir(&working_dir) 957 - .arg("--ignore-pipes") 958 - .arg(file.path()) 959 - .arg(file2.path()) 960 - .arg("archive.tar"); 961 - compress.assert().success(); 962 - archive.assert(predicate::path::is_file()); 963 - 964 - // Compress tar to an archive 965 - let mut compress2 = Command::cargo_bin("cmprss")?; 966 - compress2 967 - .current_dir(&working_dir) 968 - .arg("--ignore-pipes") 969 - .arg("archive.tar") 970 - .arg("archive.tar.gz"); 971 - compress2.assert().success(); 972 - archive2.assert(predicate::path::is_file()); 973 - 974 - // Extract file to default filename 975 - let mut extract = Command::cargo_bin("cmprss")?; 976 - extract 977 - .current_dir(&extract_dir) 978 - .arg("--ignore-pipes") 979 - .arg(archive2.path()) 980 - .arg("archive.tar"); 981 - extract.assert().success(); 982 - 983 - // Extract file to default filename 984 - let mut extract2 = Command::cargo_bin("cmprss")?; 985 - extract2 986 - .current_dir(&extract_dir) 987 - .arg("--ignore-pipes") 988 - .arg("archive.tar"); 989 - extract2.assert().success(); 990 - 991 - // Assert the files are identical 992 - extract_dir 993 - .child("test.txt") 994 - .assert(predicate::path::eq_file(file.path())); 995 - extract_dir 996 - .child("test2.txt") 997 - .assert(predicate::path::eq_file(file2.path())); 998 - 999 - Ok(()) 1000 - } 1001 - 1002 - /// Magic roundtrip with tar.gz using pipes 1003 - /// Infer things as much as possible 1004 - /// Compressing: input = test.txt + test2.txt, output = test.tar.gz 1005 - /// Extracting: input = test.tar.gz, output = test.txt + test2.txt 1006 - /// 1007 - /// ``` bash 1008 - /// cmprss tar test.txt test2.txt | cmprss gzip | cmprss gzip --extract | cmprss tar --extract 1009 - /// ``` 1010 - #[test] 1011 - fn magic_roundtrip_tar_gz_pipes() -> Result<(), Box<dyn std::error::Error>> { 1012 - let file = assert_fs::NamedTempFile::new("test.txt")?; 1013 - file.write_str("garbage data for testing")?; 1014 - let file2 = assert_fs::NamedTempFile::new("test2.txt")?; 1015 - file2.write_str("more garbage data for testing")?; 1016 - 1017 - let working_dir = assert_fs::TempDir::new()?; 1018 - let tee1 = working_dir.child("tee1"); 1019 - let tee2 = working_dir.child("tee2"); 1020 - let tee3 = working_dir.child("tee3"); 1021 - 1022 - let extract_dir = assert_fs::TempDir::new()?; 1023 - 1024 - let mut compress = Command::cargo_bin("cmprss")?; 1025 - compress 1026 - .current_dir(&working_dir) 1027 - .arg("tar") 1028 - .arg("--ignore-stdin") 1029 - .arg(file.path()) 1030 - .arg(file2.path()) 1031 - .stdout(Stdio::from(File::create(tee1.path())?)); 1032 - compress.assert().success(); 1033 - tee1.assert(predicate::path::is_file()); 1034 - 1035 - let mut compress2 = Command::cargo_bin("cmprss")?; 1036 - compress2 1037 - .current_dir(&working_dir) 1038 - .arg("gzip") 1039 - .stdin(Stdio::from(File::open(tee1.path())?)) 1040 - .stdout(Stdio::from(File::create(tee2.path())?)); 1041 - compress2.assert().success(); 1042 - tee2.assert(predicate::path::is_file()); 1043 - 1044 - let mut extract = Command::cargo_bin("cmprss")?; 1045 - extract 1046 - .current_dir(&working_dir) 1047 - .arg("gzip") 1048 - .arg("--extract") 1049 - .stdin(Stdio::from(File::open(tee2.path())?)) 1050 - .stdout(Stdio::from(File::create(tee3.path())?)); 1051 - extract.assert().success(); 1052 - tee3.assert(predicate::path::is_file()); 1053 - 1054 - // Extract file to default filename 1055 - let mut extract2 = Command::cargo_bin("cmprss")?; 1056 - extract2 1057 - .current_dir(&extract_dir) 1058 - .arg("tar") 1059 - .arg("--ignore-stdout") 1060 - .arg("--extract") 1061 - .stdin(Stdio::from(File::open(tee3.path())?)); 1062 - extract2.assert().success(); 1063 - 1064 - // Assert the files are identical 1065 - extract_dir 1066 - .child("test.txt") 1067 - .assert(predicate::path::eq_file(file.path())); 1068 - extract_dir 1069 - .child("test2.txt") 1070 - .assert(predicate::path::eq_file(file2.path())); 1071 - 1072 - Ok(()) 1073 - } 1074 - 1075 - /// Zstd roundtrip using explicit filenames 1076 - /// Compressing: input = test.txt, output = test.txt.zst 1077 - /// Extracting: input = test.txt.zst, output = test.txt 1078 - /// 1079 - /// ``` bash 1080 - /// cmprss zstd test.txt test.txt.zst 1081 - /// cmprss zstd --extract --ignore-pipes test.txt.zst 1082 - /// ``` 1083 - #[test] 1084 - fn zstd_roundtrip_explicit() -> Result<(), Box<dyn std::error::Error>> { 1085 - let file = assert_fs::NamedTempFile::new("test.txt")?; 1086 - file.write_str("garbage data for testing")?; 1087 - 1088 - let working_dir = assert_fs::TempDir::new()?; 1089 - let archive = working_dir.child("test.txt.zst"); 1090 - archive.assert(predicate::path::missing()); 1091 - 1092 - let mut compress = Command::cargo_bin("cmprss")?; 1093 - compress 1094 - .current_dir(&working_dir) 1095 - .arg("zstd") 1096 - .arg(file.path()) 1097 - .arg(archive.path()); 1098 - compress.assert().success(); 1099 - archive.assert(predicate::path::is_file()); 1100 - 1101 - let mut extract = Command::cargo_bin("cmprss")?; 1102 - extract 1103 - .current_dir(&working_dir) 1104 - .arg("zstd") 1105 - .arg("--ignore-pipes") 1106 - .arg("--extract") 1107 - .arg(archive.path()); 1108 - extract.assert().success(); 1109 - 1110 - // Assert the files are identical 1111 - working_dir 1112 - .child("test.txt") 1113 - .assert(predicate::path::eq_file(file.path())); 1114 - 1115 - Ok(()) 1116 - } 1117 - 1118 - /// Zstd roundtrip using stdin 1119 - /// Compressing: input = stdin, output = test.txt.zst 1120 - /// Extracting: input = stdin(test.txt.zst), output = test.txt 1121 - /// 1122 - /// ``` bash 1123 - /// cat test.txt | cmprss zstd test.txt.zst 1124 - /// cat test.txt.zst | cmprss zstd --extract out.txt 1125 - /// ``` 1126 - #[test] 1127 - fn zstd_roundtrip_stdin() -> Result<(), Box<dyn std::error::Error>> { 1128 - let file = assert_fs::NamedTempFile::new("test.txt")?; 1129 - file.write_str("garbage data for testing")?; 1130 - 1131 - let working_dir = assert_fs::TempDir::new()?; 1132 - let archive = working_dir.child("test.txt.zst"); 1133 - archive.assert(predicate::path::missing()); 1134 - 1135 - // Pipe file to stdin 1136 - let mut compress = Command::cargo_bin("cmprss")?; 1137 - compress 1138 - .current_dir(&working_dir) 1139 - .arg("zstd") 1140 - .arg("test.txt.zst") 1141 - .stdin(Stdio::from(File::open(file.path())?)); 1142 - compress.assert().success(); 1143 - archive.assert(predicate::path::is_file()); 1144 - 1145 - let mut extract = Command::cargo_bin("cmprss")?; 1146 - extract 1147 - .current_dir(&working_dir) 1148 - .arg("zstd") 1149 - .stdin(Stdio::from(File::open(archive.path())?)) 1150 - .arg("--extract") 1151 - .arg("out.txt"); 1152 - extract.assert().success(); 1153 - 1154 - // Assert the files are identical 1155 - working_dir 1156 - .child("out.txt") 1157 - .assert(predicate::path::eq_file(file.path())); 1158 - 1159 - Ok(()) 1160 - } 1161 - 1162 - /// Zstd roundtrip using stdout 1163 - /// Compressing: input = test.txt, output = stdout 1164 - /// Extracting: input = test.txt.zst, output = stdout 1165 - /// 1166 - /// ``` bash 1167 - /// cmprss zstd test.txt > test.txt.zst 1168 - /// cmprss zstd --extract test.txt.zst > out.txt 1169 - /// ``` 1170 - #[test] 1171 - fn zstd_roundtrip_stdout() -> Result<(), Box<dyn std::error::Error>> { 1172 - let file = assert_fs::NamedTempFile::new("test.txt")?; 1173 - file.write_str("garbage data for testing")?; 1174 - 1175 - let working_dir = assert_fs::TempDir::new()?; 1176 - let archive = working_dir.child("test.txt.zst"); 1177 - archive.assert(predicate::path::missing()); 1178 - 1179 - // Redirect stdout to file 1180 - let mut compress = Command::cargo_bin("cmprss")?; 1181 - compress 1182 - .current_dir(&working_dir) 1183 - .arg("zstd") 1184 - .arg(file.path()) 1185 - .stdout(Stdio::from(File::create(archive.path())?)); 1186 - compress.assert().success(); 1187 - archive.assert(predicate::path::is_file()); 1188 - 1189 - let output = working_dir.child("out.txt"); 1190 - output.assert(predicate::path::missing()); 1191 - 1192 - let mut extract = Command::cargo_bin("cmprss")?; 1193 - extract 1194 - .current_dir(&working_dir) 1195 - .arg("zstd") 1196 - .arg("--extract") 1197 - .arg(archive.path()) 1198 - .stdout(Stdio::from(File::create(output.path())?)); 1199 - extract.assert().success(); 1200 - output.assert(predicate::path::is_file()); 1201 - 1202 - // Assert the files are identical 1203 - output.assert(predicate::path::eq_file(file.path())); 1204 - 1205 - Ok(()) 1206 - } 1207 - 1208 - /// Zstd roundtrip with compression level 1209 - /// Compressing: input = test.txt, output = test.txt.zst, level = 9 1210 - /// Extracting: input = test.txt.zst, output = test.txt 1211 - /// 1212 - /// ``` bash 1213 - /// cmprss zstd --level 9 test.txt test.txt.zst 1214 - /// cmprss zstd --extract test.txt.zst test.txt 1215 - /// ``` 1216 - #[test] 1217 - fn zstd_roundtrip_with_level() -> Result<(), Box<dyn std::error::Error>> { 1218 - let file = assert_fs::NamedTempFile::new("test.txt")?; 1219 - file.write_str("garbage data for testing")?; 1220 - 1221 - let working_dir = assert_fs::TempDir::new()?; 1222 - let archive = working_dir.child("test.txt.zst"); 1223 - archive.assert(predicate::path::missing()); 1224 - 1225 - let mut compress = Command::cargo_bin("cmprss")?; 1226 - compress 1227 - .current_dir(&working_dir) 1228 - .arg("zstd") 1229 - .arg("--level") 1230 - .arg("9") 1231 - .arg(file.path()) 1232 - .arg(archive.path()); 1233 - compress.assert().success(); 1234 - archive.assert(predicate::path::is_file()); 1235 - 1236 - let output = working_dir.child("test.txt"); 1237 - 1238 - let mut extract = Command::cargo_bin("cmprss")?; 1239 - extract 1240 - .current_dir(&working_dir) 1241 - .arg("zstd") 1242 - .arg("--extract") 1243 - .arg(archive.path()) 1244 - .arg(output.path()); 1245 - extract.assert().success(); 1246 - 1247 - // Assert the files are identical 1248 - output.assert(predicate::path::eq_file(file.path())); 1249 - 1250 - Ok(()) 1251 - } 1252 - 1253 - #[test] 1254 - fn lz4_roundtrip_explicit() -> Result<(), Box<dyn std::error::Error>> { 1255 - let mut cmd = Command::cargo_bin("cmprss")?; 1256 - let dir = assert_fs::TempDir::new()?; 1257 - 1258 - // Create a test file 1259 - let test_file = dir.child("test.txt"); 1260 - test_file.write_str("This is a test file for LZ4 compression.")?; 1261 - 1262 - // Create output paths 1263 - let compressed_file = dir.child("test.txt.lz4"); 1264 - let extracted_file = dir.child("test_extracted.txt"); 1265 - 1266 - // Compress the file 1267 - cmd.arg("lz4") 1268 - .arg("--compress") 1269 - .arg(test_file.path()) 1270 - .arg(compressed_file.path()) 1271 - .arg("--progress=off"); 1272 - 1273 - cmd.assert().success(); 1274 - compressed_file.assert(predicates::path::exists()); 1275 - 1276 - // Extract the file 1277 - let mut cmd = Command::cargo_bin("cmprss")?; 1278 - cmd.arg("lz4") 1279 - .arg("--extract") 1280 - .arg(compressed_file.path()) 1281 - .arg(extracted_file.path()) 1282 - .arg("--progress=off"); 1283 - 1284 - cmd.assert().success(); 1285 - extracted_file.assert(predicates::path::exists()); 1286 - 1287 - // Verify the contents 1288 - let original_content = std::fs::read_to_string(test_file.path())?; 1289 - let extracted_content = std::fs::read_to_string(extracted_file.path())?; 1290 - assert_eq!(original_content, extracted_content); 1291 - 1292 - Ok(()) 1293 - } 1294 - 1295 - #[test] 1296 - fn lz4_roundtrip_stdin() -> Result<(), Box<dyn std::error::Error>> { 1297 - let mut cmd = Command::cargo_bin("cmprss")?; 1298 - let dir = assert_fs::TempDir::new()?; 1299 - 1300 - // Create a test file 1301 - let test_file = dir.child("test.txt"); 1302 - test_file.write_str("This is a test file for LZ4 compression via stdin.")?; 1303 - 1304 - // Create output paths 1305 - let compressed_file = dir.child("test.txt.lz4"); 1306 - let extracted_file = dir.child("test_extracted.txt"); 1307 - 1308 - // Compress the file via stdin 1309 - let test_content = std::fs::read_to_string(test_file.path())?; 1310 - cmd.arg("lz4") 1311 - .arg("--compress") 1312 - .arg("--output") 1313 - .arg(compressed_file.path()) 1314 - .arg("--progress=off") 1315 - .stdin(Stdio::piped()); 1316 - 1317 - let mut child = cmd.spawn()?; 1318 - if let Some(stdin) = child.stdin.as_mut() { 1319 - use std::io::Write; 1320 - stdin.write_all(test_content.as_bytes())?; 1321 - } 1322 - let output = child.wait_with_output()?; 1323 - assert!(output.status.success()); 1324 - compressed_file.assert(predicate::path::exists()); 1325 - 1326 - // Extract the file 1327 - let mut cmd = Command::cargo_bin("cmprss")?; 1328 - cmd.arg("lz4") 1329 - .arg("--extract") 1330 - .arg(compressed_file.path()) 1331 - .arg(extracted_file.path()) 1332 - .arg("--progress=off"); 1333 - 1334 - cmd.assert().success(); 1335 - extracted_file.assert(predicate::path::exists()); 1336 - 1337 - // Verify the contents 1338 - let original_content = std::fs::read_to_string(test_file.path())?; 1339 - let extracted_content = std::fs::read_to_string(extracted_file.path())?; 1340 - assert_eq!(original_content, extracted_content); 1341 - 1342 - Ok(()) 1343 - } 1344 - 1345 - #[test] 1346 - fn lz4_roundtrip_stdout() -> Result<(), Box<dyn std::error::Error>> { 1347 - let mut cmd = Command::cargo_bin("cmprss")?; 1348 - let dir = assert_fs::TempDir::new()?; 1349 - 1350 - // Create a test file 1351 - let test_file = dir.child("test.txt"); 1352 - test_file.write_str("This is a test file for LZ4 compression to stdout.")?; 1353 - 1354 - // Create output paths 1355 - let compressed_file = dir.child("test.txt.lz4"); 1356 - let extracted_file = dir.child("test_extracted.txt"); 1357 - 1358 - // Compress the file 1359 - cmd.arg("lz4") 1360 - .arg("--compress") 1361 - .arg(test_file.path()) 1362 - .arg("--progress=off"); 1363 - 1364 - let output = cmd.output()?; 1365 - assert!(output.status.success()); 1366 - std::fs::write(compressed_file.path(), output.stdout)?; 1367 - 1368 - // Extract the file to stdout 1369 - let mut cmd = Command::cargo_bin("cmprss")?; 1370 - cmd.arg("lz4") 1371 - .arg("--extract") 1372 - .arg(compressed_file.path()) 1373 - .arg("--progress=off"); 1374 - 1375 - let output = cmd.output()?; 1376 - assert!(output.status.success()); 1377 - std::fs::write(extracted_file.path(), output.stdout)?; 1378 - 1379 - // Verify the contents 1380 - let original_content = std::fs::read_to_string(test_file.path())?; 1381 - let extracted_content = std::fs::read_to_string(extracted_file.path())?; 1382 - assert_eq!(original_content, extracted_content); 1383 - 1384 - Ok(()) 1385 - } 1386 - }
+24
tests/common/mod.rs
··· 1 + use assert_fs::prelude::*; 2 + use predicates::prelude::*; 3 + 4 + pub fn create_test_file( 5 + name: &str, 6 + content: &str, 7 + ) -> Result<assert_fs::NamedTempFile, Box<dyn std::error::Error>> { 8 + let file = assert_fs::NamedTempFile::new(name)?; 9 + file.write_str(content)?; 10 + Ok(file) 11 + } 12 + 13 + pub fn create_working_dir() -> Result<assert_fs::TempDir, Box<dyn std::error::Error>> { 14 + Ok(assert_fs::TempDir::new()?) 15 + } 16 + 17 + #[allow(dead_code)] 18 + pub fn create_persistent_working_dir() -> Result<assert_fs::TempDir, Box<dyn std::error::Error>> { 19 + Ok(assert_fs::TempDir::new()?.into_persistent()) 20 + } 21 + 22 + pub fn assert_files_equal(file1: &std::path::Path, file2: &std::path::Path) { 23 + assert!(predicate::path::eq_file(file1).eval(file2)); 24 + }
+98
tests/gzip.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::{ 5 + fs::File, 6 + process::{Command, Stdio}, 7 + }; 8 + 9 + mod common; 10 + use common::*; 11 + 12 + mod gzip { 13 + use super::*; 14 + 15 + mod roundtrip { 16 + use super::*; 17 + 18 + /// Gzip roundtrip using stdin 19 + /// Compressing: input = stdin, output = test.txt.gz 20 + /// Extracting: input = test.txt.gz, output = test.txt 21 + /// 22 + /// ``` bash 23 + /// cat test.txt | cmprss gzip test.txt.gz 24 + /// cmprss gzip --ignore-pipes --extract test.txt.gz 25 + /// ``` 26 + #[test] 27 + fn stdin() -> Result<(), Box<dyn std::error::Error>> { 28 + let file = create_test_file("test.txt", "garbage data for testing")?; 29 + let working_dir = create_working_dir()?; 30 + let archive = working_dir.child("test.txt.gz"); 31 + archive.assert(predicate::path::missing()); 32 + 33 + // Pipe file to stdin 34 + let mut compress = Command::cargo_bin("cmprss")?; 35 + compress 36 + .current_dir(&working_dir) 37 + .arg("gzip") 38 + .arg("test.txt.gz") 39 + .stdin(Stdio::from(File::open(file.path())?)); 40 + compress.assert().success(); 41 + archive.assert(predicate::path::is_file()); 42 + 43 + let mut extract = Command::cargo_bin("cmprss")?; 44 + extract 45 + .current_dir(&working_dir) 46 + .arg("gzip") 47 + .arg("--ignore-pipes") 48 + .arg("--extract") 49 + .arg(archive.path()); 50 + extract.assert().success(); 51 + 52 + // Assert the files are identical 53 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 54 + 55 + Ok(()) 56 + } 57 + 58 + /// Gzip roundtrip using filename inference 59 + /// Compressing: input = stdin, output = default filename (archive.gz) 60 + /// Extracting: input = archive.gz, output = default filename (archive) 61 + /// 62 + /// ``` bash 63 + /// cat test.txt | cmprss gzip 64 + /// cmprss gzip --ignore-pipes --extract archive.gz 65 + /// ``` 66 + #[test] 67 + fn inferred_output_filenames() -> Result<(), Box<dyn std::error::Error>> { 68 + let file = create_test_file("test.txt", "garbage data for testing")?; 69 + let working_dir = create_working_dir()?; 70 + let archive = working_dir.child("archive.gz"); // default filename 71 + archive.assert(predicate::path::missing()); 72 + 73 + // Pipe file to stdin 74 + let mut compress = Command::cargo_bin("cmprss")?; 75 + compress 76 + .current_dir(&working_dir) 77 + .arg("gzip") 78 + .arg("--ignore-stdout") 79 + .stdin(Stdio::from(File::open(file.path())?)); 80 + compress.assert().success(); 81 + archive.assert(predicate::path::is_file()); 82 + 83 + let mut extract = Command::cargo_bin("cmprss")?; 84 + extract 85 + .current_dir(&working_dir) 86 + .arg("gzip") 87 + .arg("--ignore-pipes") 88 + .arg("--extract") 89 + .arg(archive.path()); 90 + extract.assert().success(); 91 + 92 + // Assert the files are identical 93 + assert_files_equal(file.path(), &working_dir.child("archive")); 94 + 95 + Ok(()) 96 + } 97 + } 98 + }
+154
tests/lz4.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::{ 5 + fs::File, 6 + process::{Command, Stdio}, 7 + }; 8 + 9 + mod common; 10 + use common::*; 11 + 12 + mod lz4 { 13 + use super::*; 14 + 15 + mod roundtrip { 16 + use super::*; 17 + 18 + /// LZ4 roundtrip using explicit filenames 19 + /// Compressing: input = test.txt, output = test.txt.lz4 20 + /// Extracting: input = test.txt.lz4, output = test.txt 21 + /// 22 + /// ``` bash 23 + /// cmprss lz4 test.txt test.txt.lz4 24 + /// cmprss lz4 --extract test.txt.lz4 test.txt 25 + /// ``` 26 + #[test] 27 + fn explicit() -> Result<(), Box<dyn std::error::Error>> { 28 + let file = create_test_file("test.txt", "This is a test file for LZ4 compression.")?; 29 + let working_dir = create_working_dir()?; 30 + let archive = working_dir.child("test.txt.lz4"); 31 + let extracted_file = working_dir.child("test_extracted.txt"); 32 + 33 + // Compress the file 34 + let mut compress = Command::cargo_bin("cmprss")?; 35 + compress 36 + .arg("lz4") 37 + .arg("--compress") 38 + .arg(file.path()) 39 + .arg(archive.path()) 40 + .arg("--progress=off"); 41 + compress.assert().success(); 42 + archive.assert(predicate::path::exists()); 43 + 44 + // Extract the file 45 + let mut extract = Command::cargo_bin("cmprss")?; 46 + extract 47 + .arg("lz4") 48 + .arg("--extract") 49 + .arg(archive.path()) 50 + .arg(extracted_file.path()) 51 + .arg("--progress=off"); 52 + extract.assert().success(); 53 + extracted_file.assert(predicate::path::exists()); 54 + 55 + // Verify the contents 56 + assert_files_equal(file.path(), extracted_file.path()); 57 + 58 + Ok(()) 59 + } 60 + 61 + /// LZ4 roundtrip using stdin 62 + /// Compressing: input = stdin, output = test.txt.lz4 63 + /// Extracting: input = test.txt.lz4, output = test.txt 64 + /// 65 + /// ``` bash 66 + /// cat test.txt | cmprss lz4 test.txt.lz4 67 + /// cmprss lz4 --extract test.txt.lz4 test.txt 68 + /// ``` 69 + #[test] 70 + fn stdin() -> Result<(), Box<dyn std::error::Error>> { 71 + let file = create_test_file( 72 + "test.txt", 73 + "This is a test file for LZ4 compression via stdin.", 74 + )?; 75 + let working_dir = create_working_dir()?; 76 + let archive = working_dir.child("test.txt.lz4"); 77 + let extracted_file = working_dir.child("test_extracted.txt"); 78 + 79 + // Compress the file via stdin 80 + let mut compress = Command::cargo_bin("cmprss")?; 81 + compress 82 + .arg("lz4") 83 + .arg("--compress") 84 + .arg("--output") 85 + .arg(archive.path()) 86 + .arg("--progress=off") 87 + .stdin(Stdio::from(File::open(file.path())?)); 88 + compress.assert().success(); 89 + archive.assert(predicate::path::exists()); 90 + 91 + // Extract the file 92 + let mut extract = Command::cargo_bin("cmprss")?; 93 + extract 94 + .arg("lz4") 95 + .arg("--extract") 96 + .arg(archive.path()) 97 + .arg(extracted_file.path()) 98 + .arg("--progress=off"); 99 + extract.assert().success(); 100 + extracted_file.assert(predicate::path::exists()); 101 + 102 + // Verify the contents 103 + assert_files_equal(file.path(), extracted_file.path()); 104 + 105 + Ok(()) 106 + } 107 + 108 + /// LZ4 roundtrip using stdout 109 + /// Compressing: input = test.txt, output = stdout 110 + /// Extracting: input = test.txt.lz4, output = stdout 111 + /// 112 + /// ``` bash 113 + /// cmprss lz4 test.txt > test.txt.lz4 114 + /// cmprss lz4 --extract test.txt.lz4 > test.txt 115 + /// ``` 116 + #[test] 117 + fn stdout() -> Result<(), Box<dyn std::error::Error>> { 118 + let file = create_test_file( 119 + "test.txt", 120 + "This is a test file for LZ4 compression to stdout.", 121 + )?; 122 + let working_dir = create_working_dir()?; 123 + let archive = working_dir.child("test.txt.lz4"); 124 + let extracted_file = working_dir.child("test_extracted.txt"); 125 + 126 + // Compress the file 127 + let mut compress = Command::cargo_bin("cmprss")?; 128 + compress 129 + .arg("lz4") 130 + .arg("--compress") 131 + .arg(file.path()) 132 + .arg("--progress=off") 133 + .stdout(Stdio::from(File::create(archive.path())?)); 134 + compress.assert().success(); 135 + archive.assert(predicate::path::exists()); 136 + 137 + // Extract the file to stdout 138 + let mut extract = Command::cargo_bin("cmprss")?; 139 + extract 140 + .arg("lz4") 141 + .arg("--extract") 142 + .arg(archive.path()) 143 + .arg("--progress=off") 144 + .stdout(Stdio::from(File::create(extracted_file.path())?)); 145 + extract.assert().success(); 146 + extracted_file.assert(predicate::path::exists()); 147 + 148 + // Verify the contents 149 + assert_files_equal(file.path(), extracted_file.path()); 150 + 151 + Ok(()) 152 + } 153 + } 154 + }
+397
tests/magic.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::{ 5 + fs::File, 6 + process::{Command, Stdio}, 7 + }; 8 + 9 + mod common; 10 + use common::*; 11 + 12 + mod magic { 13 + use super::*; 14 + 15 + mod roundtrip { 16 + use super::*; 17 + 18 + /// Magic roundtrip using stdin 19 + /// Compressing: input = stdin, output = test.txt.gz 20 + /// Extracting: input = test.txt.gz, output = test.txt 21 + /// 22 + /// ``` bash 23 + /// cat test.txt | cmprss test.txt.gz 24 + /// cmprss gz --extract test.txt.gz out.txt 25 + /// ``` 26 + #[test] 27 + fn stdin() -> Result<(), Box<dyn std::error::Error>> { 28 + let file = create_test_file("test.txt", "garbage data for testing")?; 29 + let working_dir = create_working_dir()?; 30 + let archive = working_dir.child("test.txt.gz"); 31 + archive.assert(predicate::path::missing()); 32 + 33 + // Pipe file to stdin 34 + let mut compress = Command::cargo_bin("cmprss")?; 35 + compress 36 + .current_dir(&working_dir) 37 + .arg("--ignore-stdout") 38 + .arg("test.txt.gz") 39 + .stdin(Stdio::from(File::open(file.path())?)); 40 + compress.assert().success(); 41 + archive.assert(predicate::path::is_file()); 42 + 43 + let mut extract = Command::cargo_bin("cmprss")?; 44 + extract 45 + .current_dir(&working_dir) 46 + .arg("gz") 47 + .arg("--ignore-pipes") 48 + .arg("--extract") 49 + .arg("test.txt.gz"); 50 + extract.assert().success(); 51 + 52 + // Assert the files are identical 53 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 54 + 55 + Ok(()) 56 + } 57 + 58 + /// Magic roundtrip using files 59 + /// Compressing: input = test.txt, output = test.txt.gz 60 + /// Extracting: input = test.txt.gz, output = stdout 61 + /// 62 + /// ``` bash 63 + /// cmprss test.txt test.txt.gz 64 + /// cmprss test.txt.gz out.txt 65 + /// ``` 66 + #[test] 67 + fn files() -> Result<(), Box<dyn std::error::Error>> { 68 + let file = create_test_file("test.txt", "garbage data for testing")?; 69 + let working_dir = create_working_dir()?; 70 + let archive = working_dir.child("test.txt.gz"); 71 + archive.assert(predicate::path::missing()); 72 + 73 + // Compress file to an archive 74 + let mut compress = Command::cargo_bin("cmprss")?; 75 + compress 76 + .current_dir(&working_dir) 77 + .arg("--ignore-pipes") 78 + .arg(file.path()) 79 + .arg("test.txt.gz"); 80 + compress.assert().success(); 81 + archive.assert(predicate::path::is_file()); 82 + 83 + // Extract file to given file 84 + let mut extract = Command::cargo_bin("cmprss")?; 85 + extract 86 + .current_dir(&working_dir) 87 + .arg("--ignore-pipes") 88 + .arg("test.txt.gz") 89 + .arg("out.txt"); 90 + extract.assert().success(); 91 + 92 + // Assert the files are identical 93 + assert_files_equal(file.path(), &working_dir.child("out.txt")); 94 + 95 + Ok(()) 96 + } 97 + 98 + /// Magic roundtrip using stdout decompression 99 + /// Compressing: input = test.txt, output = test.txt.gz 100 + /// Extracting: input = test.txt.gz, output = stdout 101 + /// 102 + /// ``` bash 103 + /// cmprss test.txt test.txt.gz 104 + /// cmprss test.txt.gz > out.txt 105 + /// ``` 106 + #[test] 107 + fn stdout_decompression() -> Result<(), Box<dyn std::error::Error>> { 108 + let file = create_test_file("test.txt", "garbage data for testing")?; 109 + let working_dir = create_working_dir()?; 110 + let archive = working_dir.child("test.txt.gz"); 111 + archive.assert(predicate::path::missing()); 112 + 113 + let out_file = working_dir.child("out.txt"); 114 + 115 + // Compress file to an archive 116 + let mut compress = Command::cargo_bin("cmprss")?; 117 + compress 118 + .current_dir(&working_dir) 119 + .arg("--ignore-pipes") 120 + .arg(file.path()) 121 + .arg("test.txt.gz"); 122 + compress.assert().success(); 123 + archive.assert(predicate::path::is_file()); 124 + 125 + // Extract file to stdout 126 + let mut extract = Command::cargo_bin("cmprss")?; 127 + extract 128 + .current_dir(&working_dir) 129 + .arg("--ignore-stdin") 130 + .arg("test.txt.gz") 131 + .stdout(Stdio::from(File::create(&out_file)?)); 132 + extract.assert().success(); 133 + 134 + // Assert the files are identical 135 + assert_files_equal(file.path(), out_file.path()); 136 + 137 + Ok(()) 138 + } 139 + 140 + /// Magic roundtrip using stdin compression 141 + /// Compressing: input = stdin, output = test.txt.gz 142 + /// Extracting: input = test.txt.gz, output = test.txt 143 + /// 144 + /// ``` bash 145 + /// cat test.txt | cmprss test.txt.gz 146 + /// cmprss test.txt.gz out.txt 147 + /// ``` 148 + #[test] 149 + fn stdin_compression() -> Result<(), Box<dyn std::error::Error>> { 150 + let file = create_test_file("test.txt", "garbage data for testing")?; 151 + let working_dir = create_working_dir()?; 152 + let archive = working_dir.child("test.txt.gz"); 153 + archive.assert(predicate::path::missing()); 154 + 155 + let out_file = working_dir.child("out.txt"); 156 + 157 + // Compress stdin to an archive 158 + let mut compress = Command::cargo_bin("cmprss")?; 159 + compress 160 + .current_dir(&working_dir) 161 + .arg("--ignore-stdout") 162 + .arg("test.txt.gz") 163 + .stdin(Stdio::from(File::open(file.path())?)); 164 + compress.assert().success(); 165 + archive.assert(predicate::path::is_file()); 166 + 167 + // Extract file to given file 168 + let mut extract = Command::cargo_bin("cmprss")?; 169 + extract 170 + .current_dir(&working_dir) 171 + .arg("--ignore-pipes") 172 + .arg("test.txt.gz") 173 + .arg(out_file.path()); 174 + extract.assert().success(); 175 + 176 + // Assert the files are identical 177 + assert_files_equal(file.path(), out_file.path()); 178 + 179 + Ok(()) 180 + } 181 + 182 + /// Magic roundtrip using default filenames 183 + /// Compressing: input = test.txt, output = test.txt.gz 184 + /// Extracting: input = test.txt.gz, output = <default> 185 + /// 186 + /// ``` bash 187 + /// cmprss test.txt test.txt.gz 188 + /// cmprss test.txt.gz 189 + /// ``` 190 + #[test] 191 + fn default_filenames() -> Result<(), Box<dyn std::error::Error>> { 192 + let file = create_test_file("test.txt", "garbage data for testing")?; 193 + let working_dir = create_working_dir()?; 194 + let archive = working_dir.child("test.txt.gz"); 195 + archive.assert(predicate::path::missing()); 196 + 197 + // Compress file to an archive 198 + let mut compress = Command::cargo_bin("cmprss")?; 199 + compress 200 + .current_dir(&working_dir) 201 + .arg("--ignore-pipes") 202 + .arg(file.path()) 203 + .arg("test.txt.gz"); 204 + compress.assert().success(); 205 + archive.assert(predicate::path::is_file()); 206 + 207 + // Extract file to default filename 208 + let mut extract = Command::cargo_bin("cmprss")?; 209 + extract 210 + .current_dir(&working_dir) 211 + .arg("--ignore-pipes") 212 + .arg("test.txt.gz"); 213 + extract.assert().success(); 214 + 215 + // Assert the files are identical 216 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 217 + 218 + Ok(()) 219 + } 220 + 221 + /// Magic roundtrip using multiple files with tar 222 + /// Compressing: input = test.txt/test2.txt, output = archive.tar 223 + /// Extracting: input = archive.tar, output = <default> 224 + /// 225 + /// ``` bash 226 + /// cmprss test.txt test2.txt archive.tar 227 + /// cmprss archive.tar 228 + /// ``` 229 + #[test] 230 + fn multiple_files_tar() -> Result<(), Box<dyn std::error::Error>> { 231 + let file = create_test_file("test.txt", "garbage data for testing")?; 232 + let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 233 + let working_dir = create_working_dir()?; 234 + let archive = working_dir.child("archive.tar"); 235 + archive.assert(predicate::path::missing()); 236 + 237 + // Compress files to an archive 238 + let mut compress = Command::cargo_bin("cmprss")?; 239 + compress 240 + .current_dir(&working_dir) 241 + .arg("--ignore-pipes") 242 + .arg(file.path()) 243 + .arg(file2.path()) 244 + .arg("archive.tar"); 245 + compress.assert().success(); 246 + archive.assert(predicate::path::is_file()); 247 + 248 + // Extract file to default filename 249 + let mut extract = Command::cargo_bin("cmprss")?; 250 + extract 251 + .current_dir(&working_dir) 252 + .arg("--ignore-pipes") 253 + .arg("archive.tar"); 254 + extract.assert().success(); 255 + 256 + // Assert the files are identical 257 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 258 + assert_files_equal(file2.path(), &working_dir.child("test2.txt")); 259 + 260 + Ok(()) 261 + } 262 + 263 + /// Magic roundtrip with tar.gz 264 + /// Infer things as much as possible 265 + /// Compressing: input = test.txt + test2.txt, output = test.tar.gz 266 + /// Extracting: input = test.tar.gz, output = test.txt + test2.txt 267 + /// 268 + /// ``` bash 269 + /// cmprss test.txt test2.txt archive.tar 270 + /// cmprss archive.tar archive.tar.gz 271 + /// cmprss archive.tar.gz archive.tar 272 + /// cmprss archive.tar 273 + /// ``` 274 + #[test] 275 + fn tar_gz() -> Result<(), Box<dyn std::error::Error>> { 276 + let file = create_test_file("test.txt", "garbage data for testing")?; 277 + let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 278 + let working_dir = create_working_dir()?; 279 + let archive = working_dir.child("archive.tar"); 280 + archive.assert(predicate::path::missing()); 281 + let archive2 = working_dir.child("archive.tar.gz"); 282 + archive2.assert(predicate::path::missing()); 283 + 284 + let extract_dir = create_working_dir()?; 285 + 286 + // Compress files to an archive 287 + let mut compress = Command::cargo_bin("cmprss")?; 288 + compress 289 + .current_dir(&working_dir) 290 + .arg("--ignore-pipes") 291 + .arg(file.path()) 292 + .arg(file2.path()) 293 + .arg("archive.tar"); 294 + compress.assert().success(); 295 + archive.assert(predicate::path::is_file()); 296 + 297 + // Compress tar to an archive 298 + let mut compress2 = Command::cargo_bin("cmprss")?; 299 + compress2 300 + .current_dir(&working_dir) 301 + .arg("--ignore-pipes") 302 + .arg("archive.tar") 303 + .arg("archive.tar.gz"); 304 + compress2.assert().success(); 305 + archive2.assert(predicate::path::is_file()); 306 + 307 + // Extract file to default filename 308 + let mut extract = Command::cargo_bin("cmprss")?; 309 + extract 310 + .current_dir(&extract_dir) 311 + .arg("--ignore-pipes") 312 + .arg(archive2.path()) 313 + .arg("archive.tar"); 314 + extract.assert().success(); 315 + 316 + // Extract file to default filename 317 + let mut extract2 = Command::cargo_bin("cmprss")?; 318 + extract2 319 + .current_dir(&extract_dir) 320 + .arg("--ignore-pipes") 321 + .arg("archive.tar"); 322 + extract2.assert().success(); 323 + 324 + // Assert the files are identical 325 + assert_files_equal(file.path(), &extract_dir.child("test.txt")); 326 + assert_files_equal(file2.path(), &extract_dir.child("test2.txt")); 327 + 328 + Ok(()) 329 + } 330 + 331 + /// Magic roundtrip with tar.gz using pipes 332 + /// Infer things as much as possible 333 + /// Compressing: input = test.txt + test2.txt, output = test.tar.gz 334 + /// Extracting: input = test.tar.gz, output = test.txt + test2.txt 335 + /// 336 + /// ``` bash 337 + /// cmprss tar test.txt test2.txt | cmprss gzip | cmprss gzip --extract | cmprss tar --extract 338 + /// ``` 339 + #[test] 340 + fn tar_gz_pipes() -> Result<(), Box<dyn std::error::Error>> { 341 + let file = create_test_file("test.txt", "garbage data for testing")?; 342 + let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 343 + let working_dir = create_working_dir()?; 344 + let tee1 = working_dir.child("tee1"); 345 + let tee2 = working_dir.child("tee2"); 346 + let tee3 = working_dir.child("tee3"); 347 + 348 + let extract_dir = create_working_dir()?; 349 + 350 + let mut compress = Command::cargo_bin("cmprss")?; 351 + compress 352 + .current_dir(&working_dir) 353 + .arg("tar") 354 + .arg("--ignore-stdin") 355 + .arg(file.path()) 356 + .arg(file2.path()) 357 + .stdout(Stdio::from(File::create(tee1.path())?)); 358 + compress.assert().success(); 359 + tee1.assert(predicate::path::is_file()); 360 + 361 + let mut compress2 = Command::cargo_bin("cmprss")?; 362 + compress2 363 + .current_dir(&working_dir) 364 + .arg("gzip") 365 + .stdin(Stdio::from(File::open(tee1.path())?)) 366 + .stdout(Stdio::from(File::create(tee2.path())?)); 367 + compress2.assert().success(); 368 + tee2.assert(predicate::path::is_file()); 369 + 370 + let mut extract = Command::cargo_bin("cmprss")?; 371 + extract 372 + .current_dir(&working_dir) 373 + .arg("gzip") 374 + .arg("--extract") 375 + .stdin(Stdio::from(File::open(tee2.path())?)) 376 + .stdout(Stdio::from(File::create(tee3.path())?)); 377 + extract.assert().success(); 378 + tee3.assert(predicate::path::is_file()); 379 + 380 + // Extract file to default filename 381 + let mut extract2 = Command::cargo_bin("cmprss")?; 382 + extract2 383 + .current_dir(&extract_dir) 384 + .arg("tar") 385 + .arg("--ignore-stdout") 386 + .arg("--extract") 387 + .stdin(Stdio::from(File::open(tee3.path())?)); 388 + extract2.assert().success(); 389 + 390 + // Assert the files are identical 391 + assert_files_equal(file.path(), &extract_dir.child("test.txt")); 392 + assert_files_equal(file2.path(), &extract_dir.child("test2.txt")); 393 + 394 + Ok(()) 395 + } 396 + } 397 + }
+165
tests/tar.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::process::Command; 5 + 6 + mod common; 7 + use common::*; 8 + 9 + mod tar { 10 + use super::*; 11 + 12 + mod roundtrip { 13 + use super::*; 14 + 15 + /// Tar roundtrip with a single file 16 + /// 17 + /// ``` bash 18 + /// cmprss tar test.txt archive.tar 19 + /// cmprss tar --extract archive.tar . 20 + /// ``` 21 + #[test] 22 + fn explicit() -> Result<(), Box<dyn std::error::Error>> { 23 + let file = create_test_file("test.txt", "garbage data for testing")?; 24 + let working_dir = create_working_dir()?; 25 + let archive = working_dir.child("archive.tar"); 26 + archive.assert(predicate::path::missing()); 27 + 28 + let mut compress = Command::cargo_bin("cmprss")?; 29 + compress.arg("tar").arg(file.path()).arg(archive.path()); 30 + compress.assert().success(); 31 + archive.assert(predicate::path::is_file()); 32 + 33 + let mut extract = Command::cargo_bin("cmprss")?; 34 + extract 35 + .arg("tar") 36 + .arg("--extract") 37 + .arg(archive.path()) 38 + .arg(working_dir.path()); 39 + extract.assert().success(); 40 + 41 + // Assert the files are identical 42 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 43 + 44 + Ok(()) 45 + } 46 + 47 + /// Tar roundtrip with multiple files 48 + /// 49 + /// ``` bash 50 + /// cmprss tar test.txt test2.txt archive.tar 51 + /// cmprss tar --extract archive.tar . 52 + /// ``` 53 + #[test] 54 + fn explicit_two() -> Result<(), Box<dyn std::error::Error>> { 55 + let file = create_test_file("test.txt", "garbage data for testing")?; 56 + let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 57 + let working_dir = create_working_dir()?; 58 + let archive = working_dir.child("archive.tar"); 59 + archive.assert(predicate::path::missing()); 60 + 61 + let mut compress = Command::cargo_bin("cmprss")?; 62 + compress 63 + .arg("tar") 64 + .arg(file.path()) 65 + .arg(file2.path()) 66 + .arg(archive.path()); 67 + compress.assert().success(); 68 + archive.assert(predicate::path::is_file()); 69 + 70 + let mut extract = Command::cargo_bin("cmprss")?; 71 + extract 72 + .arg("tar") 73 + .arg("--extract") 74 + .arg(archive.path()) 75 + .arg(working_dir.path()); 76 + extract.assert().success(); 77 + 78 + // Assert the files are identical 79 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 80 + assert_files_equal(file2.path(), &working_dir.child("test2.txt")); 81 + 82 + Ok(()) 83 + } 84 + 85 + /// Tar roundtrip with a single file inferring output filename 86 + /// Compressing: output = './test.txt.tar' 87 + /// Extracting: output = '.' 88 + /// 89 + /// ``` bash 90 + /// cmprss tar test.txt 91 + /// cmprss tar --extract test.txt.tar 92 + /// ``` 93 + #[test] 94 + fn implicit() -> Result<(), Box<dyn std::error::Error>> { 95 + let file = create_test_file("test.txt", "garbage data for testing")?; 96 + let working_dir = create_persistent_working_dir()?; 97 + let archive = working_dir.child("test.txt.tar"); 98 + archive.assert(predicate::path::missing()); 99 + 100 + let mut compress = Command::cargo_bin("cmprss")?; 101 + compress 102 + .current_dir(&working_dir) 103 + .arg("tar") 104 + .arg("--ignore-pipes") 105 + .arg(file.path()); 106 + compress.assert().success(); 107 + archive.assert(predicate::path::is_file()); 108 + 109 + let mut extract = Command::cargo_bin("cmprss")?; 110 + extract 111 + .current_dir(&working_dir) 112 + .arg("tar") 113 + .arg("--ignore-pipes") 114 + .arg("--extract") 115 + .arg(archive.path()); 116 + extract.assert().success(); 117 + 118 + // Assert the files are identical 119 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 120 + 121 + Ok(()) 122 + } 123 + 124 + /// Tar roundtrip with multiple files inferring output 125 + /// Uses the first file's name to generate the output filename 126 + /// Compressing: output = './test.txt.tar' 127 + /// Extracting: output = '.' 128 + /// 129 + /// ``` bash 130 + /// cmprss tar test.txt test2.txt 131 + /// cmprss tar test.txt.tar 132 + /// ``` 133 + #[test] 134 + fn implicit_two() -> Result<(), Box<dyn std::error::Error>> { 135 + let file = create_test_file("test.txt", "garbage data for testing")?; 136 + let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 137 + let working_dir = create_persistent_working_dir()?; 138 + let archive = working_dir.child("test.txt.tar"); 139 + archive.assert(predicate::path::missing()); 140 + 141 + let mut compress = Command::cargo_bin("cmprss")?; 142 + compress 143 + .current_dir(&working_dir) 144 + .arg("tar") 145 + .arg("--ignore-pipes") 146 + .arg(file.path()) 147 + .arg(file2.path()); 148 + compress.assert().success(); 149 + archive.assert(predicate::path::is_file()); 150 + 151 + let mut extract = Command::cargo_bin("cmprss")?; 152 + extract 153 + .current_dir(&working_dir) 154 + .arg("--ignore-pipes") 155 + .arg(archive.path()); 156 + extract.assert().success(); 157 + 158 + // Assert the files are identical 159 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 160 + assert_files_equal(file2.path(), &working_dir.child("test2.txt")); 161 + 162 + Ok(()) 163 + } 164 + } 165 + }
+139
tests/xz.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::{ 5 + fs::File, 6 + process::{Command, Stdio}, 7 + }; 8 + 9 + mod common; 10 + use common::*; 11 + 12 + mod xz { 13 + use super::*; 14 + 15 + mod roundtrip { 16 + use super::*; 17 + 18 + /// Xz roundtrip using files 19 + /// Compressing: input = test.txt, output = test.txt.xz 20 + /// Extracting: input = test.txt.xz, output = test.txt 21 + /// 22 + /// ``` bash 23 + /// cmprss xz test.txt test.txt.xz 24 + /// cmprss xz --extract --ignore-pipes test.txt.xz 25 + /// ``` 26 + #[test] 27 + fn explicit() -> Result<(), Box<dyn std::error::Error>> { 28 + let file = create_test_file("test.txt", "garbage data for testing")?; 29 + let working_dir = create_working_dir()?; 30 + let archive = working_dir.child("test.txt.xz"); 31 + archive.assert(predicate::path::missing()); 32 + 33 + let mut compress = Command::cargo_bin("cmprss")?; 34 + compress 35 + .current_dir(&working_dir) 36 + .arg("xz") 37 + .arg(file.path()) 38 + .arg(archive.path()); 39 + compress.assert().success(); 40 + archive.assert(predicate::path::is_file()); 41 + 42 + let mut extract = Command::cargo_bin("cmprss")?; 43 + extract 44 + .current_dir(&working_dir) 45 + .arg("xz") 46 + .arg("--ignore-pipes") 47 + .arg("--extract") 48 + .arg(archive.path()); 49 + extract.assert().success(); 50 + 51 + // Assert the files are identical 52 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 53 + 54 + Ok(()) 55 + } 56 + 57 + /// Xz roundtrip using stdin 58 + /// Compressing: input = stdin, output = test.txt.xz 59 + /// Extracting: input = stdin(test.txt.xz), output = test.txt 60 + /// 61 + /// ``` bash 62 + /// cat test.txt | cmprss xz test.txt.xz 63 + /// cat test.txt.xz | cmprss xz --extract out.txt 64 + /// ``` 65 + #[test] 66 + fn stdin() -> Result<(), Box<dyn std::error::Error>> { 67 + let file = create_test_file("test.txt", "garbage data for testing")?; 68 + let working_dir = create_working_dir()?; 69 + let archive = working_dir.child("test.txt.xz"); 70 + archive.assert(predicate::path::missing()); 71 + 72 + // Pipe file to stdin 73 + let mut compress = Command::cargo_bin("cmprss")?; 74 + compress 75 + .current_dir(&working_dir) 76 + .arg("xz") 77 + .arg("test.txt.xz") 78 + .stdin(Stdio::from(File::open(file.path())?)); 79 + compress.assert().success(); 80 + archive.assert(predicate::path::is_file()); 81 + 82 + let mut extract = Command::cargo_bin("cmprss")?; 83 + extract 84 + .current_dir(&working_dir) 85 + .arg("xz") 86 + .stdin(Stdio::from(File::open(archive.path())?)) 87 + .arg("--extract") 88 + .arg("out.txt"); 89 + extract.assert().success(); 90 + 91 + // Assert the files are identical 92 + assert_files_equal(file.path(), &working_dir.child("out.txt")); 93 + 94 + Ok(()) 95 + } 96 + 97 + /// Xz roundtrip using stdout 98 + /// Compressing: input = test.txt, output = stdout 99 + /// Extracting: input = test.txt.xz, output = stdout 100 + /// 101 + /// ``` bash 102 + /// cmprss xz test.txt > test.txt.xz 103 + /// cmprss xz --extract test.txt.xz > out.txt 104 + /// ``` 105 + #[test] 106 + fn stdout() -> Result<(), Box<dyn std::error::Error>> { 107 + let file = create_test_file("test.txt", "garbage data for testing")?; 108 + let working_dir = create_working_dir()?; 109 + let archive = working_dir.child("test.txt.xz"); 110 + archive.assert(predicate::path::missing()); 111 + 112 + // Compress file to stdout 113 + let mut compress = Command::cargo_bin("cmprss")?; 114 + compress 115 + .current_dir(&working_dir) 116 + .arg("xz") 117 + .arg(file.path()) 118 + .stdout(Stdio::from(File::create(archive.path())?)); 119 + compress.assert().success(); 120 + archive.assert(predicate::path::is_file()); 121 + 122 + // Extract file to stdout 123 + let mut extract = Command::cargo_bin("cmprss")?; 124 + extract 125 + .current_dir(&working_dir) 126 + .arg("xz") 127 + .arg("--ignore-stdin") 128 + .arg("--extract") 129 + .arg(archive.path()) 130 + .arg("out.txt"); 131 + extract.assert().success(); 132 + 133 + // Assert the files are identical 134 + assert_files_equal(file.path(), &working_dir.child("out.txt")); 135 + 136 + Ok(()) 137 + } 138 + } 139 + }
+131
tests/zip.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::{path::PathBuf, process::Command}; 5 + 6 + mod common; 7 + use common::*; 8 + 9 + mod zip { 10 + use super::*; 11 + 12 + mod roundtrip { 13 + use super::*; 14 + 15 + /// Zip roundtrip with a single file 16 + /// 17 + /// ``` bash 18 + /// cmprss zip test.txt archive.zip 19 + /// cmprss zip --extract archive.zip . 20 + /// ``` 21 + #[test] 22 + fn explicit() -> Result<(), Box<dyn std::error::Error>> { 23 + let file = create_test_file("test.txt", "garbage data for testing")?; 24 + let working_dir = create_working_dir()?; 25 + let archive = working_dir.child("archive.zip"); 26 + archive.assert(predicate::path::missing()); 27 + 28 + let mut compress = Command::cargo_bin("cmprss")?; 29 + compress.arg("zip").arg(file.path()).arg(archive.path()); 30 + compress.assert().success(); 31 + archive.assert(predicate::path::is_file()); 32 + 33 + let mut extract = Command::cargo_bin("cmprss")?; 34 + extract 35 + .arg("zip") 36 + .arg("--extract") 37 + .arg(archive.path()) 38 + .arg(working_dir.path()); 39 + extract.assert().success(); 40 + 41 + // Assert the files are identical 42 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 43 + 44 + Ok(()) 45 + } 46 + 47 + /// Zip roundtrip with multiple files 48 + /// 49 + /// ``` bash 50 + /// cmprss zip test.txt test2.txt archive.zip 51 + /// cmprss zip --extract archive.zip . 52 + /// ``` 53 + #[test] 54 + fn explicit_two() -> Result<(), Box<dyn std::error::Error>> { 55 + let file = create_test_file("test.txt", "garbage data for testing")?; 56 + let file2 = create_test_file("test2.txt", "more garbage data for testing")?; 57 + let working_dir = create_working_dir()?; 58 + let archive = working_dir.child("archive.zip"); 59 + archive.assert(predicate::path::missing()); 60 + 61 + let mut compress = Command::cargo_bin("cmprss")?; 62 + compress 63 + .arg("zip") 64 + .arg(file.path()) 65 + .arg(file2.path()) 66 + .arg(archive.path()); 67 + compress.assert().success(); 68 + archive.assert(predicate::path::is_file()); 69 + 70 + let mut extract = Command::cargo_bin("cmprss")?; 71 + extract 72 + .arg("zip") 73 + .arg("--extract") 74 + .arg(archive.path()) 75 + .arg(working_dir.path()); 76 + extract.assert().success(); 77 + 78 + // Assert the files are identical 79 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 80 + assert_files_equal(file2.path(), &working_dir.child("test2.txt")); 81 + 82 + Ok(()) 83 + } 84 + 85 + /// Zip roundtrip with a directory 86 + /// 87 + /// ``` bash 88 + /// cmprss zip directory archive.zip 89 + /// cmprss zip --extract archive.zip output_dir 90 + /// ``` 91 + #[test] 92 + fn directory() -> Result<(), Box<dyn std::error::Error>> { 93 + let dir = create_working_dir()?; 94 + let file = dir.child("test.txt"); 95 + file.write_str("garbage data for testing")?; 96 + let file2 = dir.child("test2.txt"); 97 + file2.write_str("more garbage data for testing")?; 98 + 99 + let working_dir = create_working_dir()?; 100 + let archive = working_dir.child("archive.zip"); 101 + archive.assert(predicate::path::missing()); 102 + 103 + let mut compress = Command::cargo_bin("cmprss")?; 104 + compress.arg("zip").arg(dir.path()).arg(archive.path()); 105 + compress.assert().success(); 106 + archive.assert(predicate::path::is_file()); 107 + 108 + let extract_dir = working_dir.child("output"); 109 + std::fs::create_dir_all(extract_dir.path())?; 110 + 111 + let mut extract = Command::cargo_bin("cmprss")?; 112 + extract 113 + .arg("zip") 114 + .arg("--extract") 115 + .arg(archive.path()) 116 + .arg(extract_dir.path()); 117 + extract.assert().success(); 118 + 119 + // Assert the files are identical 120 + // Since the archive stores the entire directory, the extracted file is contained in the directory 121 + let dir_name: PathBuf = dir.path().file_name().unwrap().into(); 122 + assert_files_equal(file.path(), &extract_dir.child(&dir_name).child("test.txt")); 123 + assert_files_equal( 124 + file2.path(), 125 + &extract_dir.child(&dir_name).child("test2.txt"), 126 + ); 127 + 128 + Ok(()) 129 + } 130 + } 131 + }
+184
tests/zstd.rs
··· 1 + use assert_cmd::prelude::*; 2 + use assert_fs::prelude::*; 3 + use predicates::prelude::*; 4 + use std::{ 5 + fs::File, 6 + process::{Command, Stdio}, 7 + }; 8 + 9 + mod common; 10 + use common::*; 11 + 12 + mod zstd { 13 + use super::*; 14 + 15 + mod roundtrip { 16 + use super::*; 17 + 18 + /// Zstd roundtrip using explicit filenames 19 + /// Compressing: input = test.txt, output = test.txt.zst 20 + /// Extracting: input = test.txt.zst, output = test.txt 21 + /// 22 + /// ``` bash 23 + /// cmprss zstd test.txt test.txt.zst 24 + /// cmprss zstd --extract --ignore-pipes test.txt.zst 25 + /// ``` 26 + #[test] 27 + fn explicit() -> Result<(), Box<dyn std::error::Error>> { 28 + let file = create_test_file("test.txt", "garbage data for testing")?; 29 + let working_dir = create_working_dir()?; 30 + let archive = working_dir.child("test.txt.zst"); 31 + archive.assert(predicate::path::missing()); 32 + 33 + let mut compress = Command::cargo_bin("cmprss")?; 34 + compress 35 + .current_dir(&working_dir) 36 + .arg("zstd") 37 + .arg(file.path()) 38 + .arg(archive.path()); 39 + compress.assert().success(); 40 + archive.assert(predicate::path::is_file()); 41 + 42 + let mut extract = Command::cargo_bin("cmprss")?; 43 + extract 44 + .current_dir(&working_dir) 45 + .arg("zstd") 46 + .arg("--ignore-pipes") 47 + .arg("--extract") 48 + .arg(archive.path()); 49 + extract.assert().success(); 50 + 51 + // Assert the files are identical 52 + assert_files_equal(file.path(), &working_dir.child("test.txt")); 53 + 54 + Ok(()) 55 + } 56 + 57 + /// Zstd roundtrip using stdin 58 + /// Compressing: input = stdin, output = test.txt.zst 59 + /// Extracting: input = stdin(test.txt.zst), output = test.txt 60 + /// 61 + /// ``` bash 62 + /// cat test.txt | cmprss zstd test.txt.zst 63 + /// cat test.txt.zst | cmprss zstd --extract out.txt 64 + /// ``` 65 + #[test] 66 + fn stdin() -> Result<(), Box<dyn std::error::Error>> { 67 + let file = create_test_file("test.txt", "garbage data for testing")?; 68 + let working_dir = create_working_dir()?; 69 + let archive = working_dir.child("test.txt.zst"); 70 + archive.assert(predicate::path::missing()); 71 + 72 + // Pipe file to stdin 73 + let mut compress = Command::cargo_bin("cmprss")?; 74 + compress 75 + .current_dir(&working_dir) 76 + .arg("zstd") 77 + .arg("test.txt.zst") 78 + .stdin(Stdio::from(File::open(file.path())?)); 79 + compress.assert().success(); 80 + archive.assert(predicate::path::is_file()); 81 + 82 + let mut extract = Command::cargo_bin("cmprss")?; 83 + extract 84 + .current_dir(&working_dir) 85 + .arg("zstd") 86 + .stdin(Stdio::from(File::open(archive.path())?)) 87 + .arg("--extract") 88 + .arg("out.txt"); 89 + extract.assert().success(); 90 + 91 + // Assert the files are identical 92 + assert_files_equal(file.path(), &working_dir.child("out.txt")); 93 + 94 + Ok(()) 95 + } 96 + 97 + /// Zstd roundtrip using stdout 98 + /// Compressing: input = test.txt, output = stdout 99 + /// Extracting: input = test.txt.zst, output = stdout 100 + /// 101 + /// ``` bash 102 + /// cmprss zstd test.txt > test.txt.zst 103 + /// cmprss zstd --extract test.txt.zst > out.txt 104 + /// ``` 105 + #[test] 106 + fn stdout() -> Result<(), Box<dyn std::error::Error>> { 107 + let file = create_test_file("test.txt", "garbage data for testing")?; 108 + let working_dir = create_working_dir()?; 109 + let archive = working_dir.child("test.txt.zst"); 110 + archive.assert(predicate::path::missing()); 111 + 112 + // Compress file to stdout 113 + let mut compress = Command::cargo_bin("cmprss")?; 114 + compress 115 + .current_dir(&working_dir) 116 + .arg("zstd") 117 + .arg(file.path()) 118 + .stdout(Stdio::from(File::create(archive.path())?)); 119 + compress.assert().success(); 120 + archive.assert(predicate::path::is_file()); 121 + 122 + let output = working_dir.child("out.txt"); 123 + output.assert(predicate::path::missing()); 124 + 125 + let mut extract = Command::cargo_bin("cmprss")?; 126 + extract 127 + .current_dir(&working_dir) 128 + .arg("zstd") 129 + .arg("--extract") 130 + .arg(archive.path()) 131 + .stdout(Stdio::from(File::create(output.path())?)); 132 + extract.assert().success(); 133 + output.assert(predicate::path::is_file()); 134 + 135 + // Assert the files are identical 136 + assert_files_equal(file.path(), output.path()); 137 + 138 + Ok(()) 139 + } 140 + 141 + /// Zstd roundtrip with compression level 142 + /// Compressing: input = test.txt, output = test.txt.zst, level = 9 143 + /// Extracting: input = test.txt.zst, output = test.txt 144 + /// 145 + /// ``` bash 146 + /// cmprss zstd --level 9 test.txt test.txt.zst 147 + /// cmprss zstd --extract test.txt.zst test.txt 148 + /// ``` 149 + #[test] 150 + fn with_level() -> Result<(), Box<dyn std::error::Error>> { 151 + let file = create_test_file("test.txt", "garbage data for testing")?; 152 + let working_dir = create_working_dir()?; 153 + let archive = working_dir.child("test.txt.zst"); 154 + archive.assert(predicate::path::missing()); 155 + 156 + let mut compress = Command::cargo_bin("cmprss")?; 157 + compress 158 + .current_dir(&working_dir) 159 + .arg("zstd") 160 + .arg("--level") 161 + .arg("9") 162 + .arg(file.path()) 163 + .arg(archive.path()); 164 + compress.assert().success(); 165 + archive.assert(predicate::path::is_file()); 166 + 167 + let output = working_dir.child("test.txt"); 168 + 169 + let mut extract = Command::cargo_bin("cmprss")?; 170 + extract 171 + .current_dir(&working_dir) 172 + .arg("zstd") 173 + .arg("--extract") 174 + .arg(archive.path()) 175 + .arg(output.path()); 176 + extract.assert().success(); 177 + 178 + // Assert the files are identical 179 + assert_files_equal(file.path(), output.path()); 180 + 181 + Ok(()) 182 + } 183 + } 184 + }