this repo has no description
1use assert_cmd::prelude::*;
2use assert_fs::prelude::*;
3use predicates::prelude::*;
4use std::{
5 fs::File,
6 process::{Command, Stdio},
7};
8
9mod common;
10use common::*;
11
12mod 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}