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
9/// Tar roundtrip with a single file
10///
11/// ``` bash
12/// cmprss tar test.txt archive.tar
13/// cmprss tar --extract archive.tar .
14/// ```
15#[test]
16fn tar_roundtrip_explicit() -> Result<(), Box<dyn std::error::Error>> {
17 let file = assert_fs::NamedTempFile::new("test.txt")?;
18 file.write_str("garbage data for testing")?;
19 let working_dir = assert_fs::TempDir::new()?;
20 let archive = working_dir.child("archive.tar");
21 archive.assert(predicate::path::missing());
22
23 let mut compress = Command::cargo_bin("cmprss")?;
24 compress.arg("tar").arg(file.path()).arg(archive.path());
25 compress.assert().success();
26 archive.assert(predicate::path::is_file());
27
28 let mut extract = Command::cargo_bin("cmprss")?;
29 extract
30 .arg("tar")
31 .arg("--extract")
32 .arg(archive.path())
33 .arg(working_dir.path());
34 extract.assert().success();
35
36 // Assert the files are identical
37 working_dir
38 .child("test.txt")
39 .assert(predicate::path::eq_file(file.path()));
40
41 Ok(())
42}
43
44/// Tar roundtrip with multiple files
45///
46/// ``` bash
47/// cmprss tar test.txt test2.txt archive.tar
48/// cmprss tar --extract archive.tar .
49/// ```
50#[test]
51fn tar_roundtrip_explicit_two() -> Result<(), Box<dyn std::error::Error>> {
52 let file = assert_fs::NamedTempFile::new("test.txt")?;
53 file.write_str("garbage data for testing")?;
54 let file2 = assert_fs::NamedTempFile::new("test2.txt")?;
55 file2.write_str("more garbage data for testing")?;
56 let working_dir = assert_fs::TempDir::new()?;
57 let archive = working_dir.child("archive.tar");
58 archive.assert(predicate::path::missing());
59
60 let mut compress = Command::cargo_bin("cmprss")?;
61 compress
62 .arg("tar")
63 .arg(file.path())
64 .arg(file2.path())
65 .arg(archive.path());
66 compress.assert().success();
67 archive.assert(predicate::path::is_file());
68
69 let mut extract = Command::cargo_bin("cmprss")?;
70 extract
71 .arg("tar")
72 .arg("--extract")
73 .arg(archive.path())
74 .arg(working_dir.path());
75 extract.assert().success();
76
77 // Assert the files are identical
78 working_dir
79 .child("test.txt")
80 .assert(predicate::path::eq_file(file.path()));
81 working_dir
82 .child("test2.txt")
83 .assert(predicate::path::eq_file(file2.path()));
84
85 Ok(())
86}
87
88/// Tar roundtrip with a single file inferring output filename
89/// Compressing: output = './test.txt.tar'
90/// Extracting: output = '.'
91///
92/// ``` bash
93/// cmprss tar test.txt
94/// cmprss tar --extract test.txt.tar
95/// ```
96#[test]
97fn tar_roundtrip_implicit() -> Result<(), Box<dyn std::error::Error>> {
98 let file = assert_fs::NamedTempFile::new("test.txt")?;
99 file.write_str("garbage data for testing")?;
100 let working_dir = assert_fs::TempDir::new()?.into_persistent();
101 let archive = working_dir.child("test.txt.tar");
102 archive.assert(predicate::path::missing());
103
104 let mut compress = Command::cargo_bin("cmprss")?;
105 compress
106 .current_dir(&working_dir)
107 .arg("tar")
108 .arg("--ignore-pipes")
109 .arg(file.path());
110 compress.assert().success();
111 archive.assert(predicate::path::is_file());
112
113 let mut extract = Command::cargo_bin("cmprss")?;
114 extract
115 .current_dir(&working_dir)
116 .arg("tar")
117 .arg("--ignore-pipes")
118 .arg("--extract")
119 .arg(archive.path());
120 extract.assert().success();
121
122 // Assert the files are identical
123 working_dir
124 .child("test.txt")
125 .assert(predicate::path::eq_file(file.path()));
126
127 Ok(())
128}
129
130/// Tar roundtrip with multiple files inferring output
131/// Uses the first file's name to generate the output filename
132/// Compressing: output = './test.txt.tar'
133/// Extracting: output = '.'
134///
135/// ``` bash
136/// cmprss tar test.txt test2.txt
137/// cmprss tar --extract test.txt.tar
138/// ```
139#[test]
140fn tar_roundtrip_implicit_two() -> Result<(), Box<dyn std::error::Error>> {
141 let file = assert_fs::NamedTempFile::new("test.txt")?;
142 file.write_str("garbage data for testing")?;
143 let file2 = assert_fs::NamedTempFile::new("test2.txt")?;
144 file2.write_str("more garbage data for testing")?;
145 let working_dir = assert_fs::TempDir::new()?.into_persistent();
146 let archive = working_dir.child("test.txt.tar");
147 archive.assert(predicate::path::missing());
148
149 let mut compress = Command::cargo_bin("cmprss")?;
150 compress
151 .current_dir(&working_dir)
152 .arg("tar")
153 .arg("--ignore-pipes")
154 .arg(file.path())
155 .arg(file2.path());
156 compress.assert().success();
157 archive.assert(predicate::path::is_file());
158
159 let mut extract = Command::cargo_bin("cmprss")?;
160 extract
161 .current_dir(&working_dir)
162 .arg("tar")
163 .arg("--ignore-pipes")
164 .arg("--extract")
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]
188fn 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]
231fn 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]
274fn 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]
316fn 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]
359fn 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]
406fn 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]
449fn 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]
493fn 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}