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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use predicates::prelude::*;
use std::{
fs::File,
process::{Command, Stdio},
};
mod common;
use common::*;
mod lz4 {
use super::*;
mod roundtrip {
use super::*;
/// LZ4 roundtrip using explicit filenames
/// Compressing: input = test.txt, output = test.txt.lz4
/// Extracting: input = test.txt.lz4, output = test.txt
///
/// ``` bash
/// cmprss lz4 test.txt test.txt.lz4
/// cmprss lz4 --extract test.txt.lz4 test.txt
/// ```
#[test]
fn explicit() -> Result<(), Box<dyn std::error::Error>> {
let file = create_test_file("test.txt", "This is a test file for LZ4 compression.")?;
let working_dir = create_working_dir()?;
let archive = working_dir.child("test.txt.lz4");
let extracted_file = working_dir.child("test_extracted.txt");
// Compress the file
let mut compress = Command::cargo_bin("cmprss")?;
compress
.arg("lz4")
.arg("--compress")
.arg(file.path())
.arg(archive.path())
.arg("--progress=off");
compress.assert().success();
archive.assert(predicate::path::exists());
// Extract the file
let mut extract = Command::cargo_bin("cmprss")?;
extract
.arg("lz4")
.arg("--extract")
.arg(archive.path())
.arg(extracted_file.path())
.arg("--progress=off");
extract.assert().success();
extracted_file.assert(predicate::path::exists());
// Verify the contents
assert_files_equal(file.path(), extracted_file.path());
Ok(())
}
/// LZ4 roundtrip using stdin
/// Compressing: input = stdin, output = test.txt.lz4
/// Extracting: input = test.txt.lz4, output = test.txt
///
/// ``` bash
/// cat test.txt | cmprss lz4 test.txt.lz4
/// cmprss lz4 --extract test.txt.lz4 test.txt
/// ```
#[test]
fn stdin() -> Result<(), Box<dyn std::error::Error>> {
let file = create_test_file(
"test.txt",
"This is a test file for LZ4 compression via stdin.",
)?;
let working_dir = create_working_dir()?;
let archive = working_dir.child("test.txt.lz4");
let extracted_file = working_dir.child("test_extracted.txt");
// Compress the file via stdin
let mut compress = Command::cargo_bin("cmprss")?;
compress
.arg("lz4")
.arg("--compress")
.arg("--output")
.arg(archive.path())
.arg("--progress=off")
.stdin(Stdio::from(File::open(file.path())?));
compress.assert().success();
archive.assert(predicate::path::exists());
// Extract the file
let mut extract = Command::cargo_bin("cmprss")?;
extract
.arg("lz4")
.arg("--extract")
.arg(archive.path())
.arg(extracted_file.path())
.arg("--progress=off");
extract.assert().success();
extracted_file.assert(predicate::path::exists());
// Verify the contents
assert_files_equal(file.path(), extracted_file.path());
Ok(())
}
/// LZ4 roundtrip using stdout
/// Compressing: input = test.txt, output = stdout
/// Extracting: input = test.txt.lz4, output = stdout
///
/// ``` bash
/// cmprss lz4 test.txt > test.txt.lz4
/// cmprss lz4 --extract test.txt.lz4 > test.txt
/// ```
#[test]
fn stdout() -> Result<(), Box<dyn std::error::Error>> {
let file = create_test_file(
"test.txt",
"This is a test file for LZ4 compression to stdout.",
)?;
let working_dir = create_working_dir()?;
let archive = working_dir.child("test.txt.lz4");
let extracted_file = working_dir.child("test_extracted.txt");
// Compress the file
let mut compress = Command::cargo_bin("cmprss")?;
compress
.arg("lz4")
.arg("--compress")
.arg(file.path())
.arg("--progress=off")
.stdout(Stdio::from(File::create(archive.path())?));
compress.assert().success();
archive.assert(predicate::path::exists());
// Extract the file to stdout
let mut extract = Command::cargo_bin("cmprss")?;
extract
.arg("lz4")
.arg("--extract")
.arg(archive.path())
.arg("--progress=off")
.stdout(Stdio::from(File::create(extracted_file.path())?));
extract.assert().success();
extracted_file.assert(predicate::path::exists());
// Verify the contents
assert_files_equal(file.path(), extracted_file.path());
Ok(())
}
}
}