1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use predicates::prelude::*;
use std::{
fs::File,
process::{Command, Stdio},
};
mod common;
use common::*;
mod gzip {
use super::*;
mod roundtrip {
use super::*;
/// Gzip roundtrip using stdin
/// Compressing: input = stdin, output = test.txt.gz
/// Extracting: input = test.txt.gz, output = test.txt
///
/// ``` bash
/// cat test.txt | cmprss gzip test.txt.gz
/// cmprss gzip --ignore-pipes --extract test.txt.gz
/// ```
#[test]
fn stdin() -> Result<(), Box<dyn std::error::Error>> {
let file = create_test_file("test.txt", "garbage data for testing")?;
let working_dir = create_working_dir()?;
let archive = working_dir.child("test.txt.gz");
archive.assert(predicate::path::missing());
// Pipe file to stdin
let mut compress = Command::cargo_bin("cmprss")?;
compress
.current_dir(&working_dir)
.arg("gzip")
.arg("test.txt.gz")
.stdin(Stdio::from(File::open(file.path())?));
compress.assert().success();
archive.assert(predicate::path::is_file());
let mut extract = Command::cargo_bin("cmprss")?;
extract
.current_dir(&working_dir)
.arg("gzip")
.arg("--ignore-pipes")
.arg("--extract")
.arg(archive.path());
extract.assert().success();
// Assert the files are identical
assert_files_equal(file.path(), &working_dir.child("test.txt"));
Ok(())
}
/// Gzip roundtrip using filename inference
/// Compressing: input = stdin, output = default filename (archive.gz)
/// Extracting: input = archive.gz, output = default filename (archive)
///
/// ``` bash
/// cat test.txt | cmprss gzip
/// cmprss gzip --ignore-pipes --extract archive.gz
/// ```
#[test]
fn inferred_output_filenames() -> Result<(), Box<dyn std::error::Error>> {
let file = create_test_file("test.txt", "garbage data for testing")?;
let working_dir = create_working_dir()?;
let archive = working_dir.child("archive.gz"); // default filename
archive.assert(predicate::path::missing());
// Pipe file to stdin
let mut compress = Command::cargo_bin("cmprss")?;
compress
.current_dir(&working_dir)
.arg("gzip")
.arg("--ignore-stdout")
.stdin(Stdio::from(File::open(file.path())?));
compress.assert().success();
archive.assert(predicate::path::is_file());
let mut extract = Command::cargo_bin("cmprss")?;
extract
.current_dir(&working_dir)
.arg("gzip")
.arg("--ignore-pipes")
.arg("--extract")
.arg(archive.path());
extract.assert().success();
// Assert the files are identical
assert_files_equal(file.path(), &working_dir.child("archive"));
Ok(())
}
}
}