this repo has no description
1mod 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}