1 /// Snapshot tests for Ouch's output.
3 /// See CONTRIBUTING.md for a brief guide on how to use [`insta`] for these tests.
4 /// [`insta`]: https://docs.rs/insta
9 use std::{io, path::Path, process::Output};
12 use insta::assert_display_snapshot as ui;
14 // Don't run these on Windows
16 use self::ignore as ui;
17 use crate::utils::run_in;
19 fn testdir() -> io::Result<(tempfile::TempDir, &'static Path)> {
20 let dir = tempfile::tempdir()?;
21 let path = dir.path().to_path_buf().into_boxed_path();
22 Ok((dir, Box::leak(path)))
25 fn run_ouch(argv: &str, dir: &Path) -> String {
26 let output = utils::cargo_bin()
27 .args(argv.split_whitespace().skip(1))
30 .unwrap_or_else(|err| {
32 "Failed to run command\n\
39 redact_paths(&output_to_string(output), dir)
42 // remove random tempdir paths from snapshots to make them deterministic
43 fn redact_paths(text: &str, path: &Path) -> String {
44 let redacted = "<FOLDER>";
46 let path = path.display();
47 let path = if cfg!(target_os = "macos") {
48 format!(r"/private{path}")
53 text.replace(path.as_str(), redacted)
56 fn output_to_string(output: Output) -> String {
57 String::from_utf8(output.stdout).unwrap() + std::str::from_utf8(&output.stderr).unwrap()
61 fn ui_test_err_compress_missing_extension() {
62 let (_dropper, dir) = testdir().unwrap();
65 run_in(dir, "touch", "input").unwrap();
67 ui!(run_ouch("ouch compress input output", dir));
71 fn ui_test_err_decompress_missing_extension() {
72 let (_dropper, dir) = testdir().unwrap();
74 run_in(dir, "touch", "a b.unknown").unwrap();
76 ui!(run_ouch("ouch decompress a", dir));
77 ui!(run_ouch("ouch decompress a b.unknown", dir));
78 ui!(run_ouch("ouch decompress b.unknown", dir));
82 fn ui_test_err_missing_files() {
83 let (_dropper, dir) = testdir().unwrap();
85 ui!(run_ouch("ouch compress a b", dir));
86 ui!(run_ouch("ouch decompress a b", dir));
87 ui!(run_ouch("ouch list a b", dir));
91 fn ui_test_ok_compress() {
92 let (_dropper, dir) = testdir().unwrap();
95 run_in(dir, "touch", "input").unwrap();
97 ui!(run_ouch("ouch compress input output.zip", dir));
98 ui!(run_ouch("ouch compress input output.gz", dir));
102 fn ui_test_ok_decompress() {
103 let (_dropper, dir) = testdir().unwrap();
106 run_in(dir, "touch", "input").unwrap();
107 run_ouch("ouch compress input output.zst", dir);
109 ui!(run_ouch("ouch decompress output.zst", dir));
113 fn ui_test_usage_help_flag() {
114 ui!(output_to_string(ouch!("--help")));
115 ui!(output_to_string(ouch!("-h")));
120 macro_rules! ignore {