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();
66 run_in(dir, "touch", "input").unwrap();
69 run_in(dir, "cmd", "/C copy nul input").unwrap();
71 ui!(run_ouch("ouch compress input output", dir));
75 fn ui_test_err_decompress_missing_extension() {
76 let (_dropper, dir) = testdir().unwrap();
79 run_in(dir, "touch", "a b.unknown").unwrap();
82 run_in(dir, "cmd", "/C copy nul a").unwrap();
85 run_in(dir, "cmd", "/C copy nul b.unknown").unwrap();
88 ui!(run_ouch("ouch decompress a", dir));
89 ui!(run_ouch("ouch decompress a b.unknown", dir));
90 ui!(run_ouch("ouch decompress b.unknown", dir));
94 fn ui_test_err_missing_files() {
95 let (_dropper, dir) = testdir().unwrap();
97 ui!(run_ouch("ouch compress a b", dir));
98 ui!(run_ouch("ouch decompress a b", dir));
99 ui!(run_ouch("ouch list a b", dir));
103 fn ui_test_ok_compress() {
104 let (_dropper, dir) = testdir().unwrap();
108 run_in(dir, "touch", "input").unwrap();
111 run_in(dir, "cmd", "/C copy nul input").unwrap();
113 ui!(run_ouch("ouch compress input output.zip", dir));
114 ui!(run_ouch("ouch compress input output.gz", dir));
118 fn ui_test_ok_decompress() {
119 let (_dropper, dir) = testdir().unwrap();
123 run_in(dir, "touch", "input").unwrap();
124 run_ouch("ouch compress input output.zst", dir);
126 ui!(run_ouch("ouch decompress output.zst", dir));
130 fn ui_test_usage_help_flag() {
131 ui!(output_to_string(ouch!("--help")));
132 ui!(output_to_string(ouch!("-h")));
137 macro_rules! ignore {