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::{ffi::OsStr, io, path::Path, process::Output};
11 use insta::assert_snapshot as ui;
14 use crate::utils::create_files_in;
16 fn testdir() -> io::Result<(tempfile::TempDir, &'static Path)> {
17 let dir = tempfile::tempdir()?;
18 let path = dir.path().to_path_buf().into_boxed_path();
19 Ok((dir, Box::leak(path)))
22 fn run_ouch(argv: &str, dir: &Path) -> String {
23 let output = utils::cargo_bin()
24 .args(argv.split_whitespace().skip(1))
27 .unwrap_or_else(|err| {
29 "Failed to run command\n\
36 redact_paths(&output_to_string(output), dir)
39 /// Remove random tempdir paths from snapshots to make them deterministic.
40 fn redact_paths(text: &str, dir: &Path) -> String {
41 let dir_name = dir.file_name().and_then(OsStr::to_str).unwrap();
43 // this regex should be good as long as the path does not contain whitespace characters
44 let re = Regex::new(&format!(r"\S*[/\\]{dir_name}[/\\]")).unwrap();
45 re.replace_all(text, "<TMP_DIR>/").into()
48 fn output_to_string(output: Output) -> String {
49 String::from_utf8(output.stdout).unwrap() + std::str::from_utf8(&output.stderr).unwrap()
53 fn ui_test_err_compress_missing_extension() {
54 let (_dropper, dir) = testdir().unwrap();
57 create_files_in(dir, &["input"]);
59 ui!(run_ouch("ouch compress input output", dir));
63 fn ui_test_err_decompress_missing_extension() {
64 let (_dropper, dir) = testdir().unwrap();
66 create_files_in(dir, &["a", "b.unknown"]);
68 let snapshot = concat_snapshot_filename_rar_feature("ui_test_err_decompress_missing_extension");
69 ui!(format!("{snapshot}-1"), run_ouch("ouch decompress a", dir));
70 ui!(format!("{snapshot}-2"), run_ouch("ouch decompress a b.unknown", dir));
71 ui!(format!("{snapshot}-3"), run_ouch("ouch decompress b.unknown", dir));
75 fn ui_test_err_missing_files() {
76 let (_dropper, dir) = testdir().unwrap();
78 ui!(run_ouch("ouch compress a b", dir));
79 ui!(run_ouch("ouch decompress a b", dir));
80 ui!(run_ouch("ouch list a b", dir));
84 fn ui_test_err_format_flag() {
85 let (_dropper, dir) = testdir().unwrap();
88 create_files_in(dir, &["input"]);
90 let snapshot = concat_snapshot_filename_rar_feature("ui_test_err_format_flag");
92 format!("{snapshot}-1"),
93 run_ouch("ouch compress input output --format tar.gz.unknown", dir),
96 format!("{snapshot}-2"),
97 run_ouch("ouch compress input output --format targz", dir),
100 format!("{snapshot}-3"),
101 run_ouch("ouch compress input output --format .tar.$#!@.rest", dir),
106 fn ui_test_ok_format_flag() {
107 let (_dropper, dir) = testdir().unwrap();
110 create_files_in(dir, &["input"]);
112 let snapshot = concat_snapshot_filename_rar_feature("ui_test_ok_format_flag");
114 format!("{snapshot}-1"),
115 run_ouch("ouch compress input output1 --format tar.gz", dir),
118 format!("{snapshot}-2"),
119 run_ouch("ouch compress input output2 --format .tar.gz", dir),
124 fn ui_test_ok_compress() {
125 let (_dropper, dir) = testdir().unwrap();
128 create_files_in(dir, &["input"]);
130 ui!(run_ouch("ouch compress input output.zip", dir));
131 ui!(run_ouch("ouch compress input output.gz", dir));
135 fn ui_test_ok_decompress() {
136 let (_dropper, dir) = testdir().unwrap();
139 create_files_in(dir, &["input"]);
140 run_ouch("ouch compress input output.zst", dir);
142 ui!(run_ouch("ouch decompress output.zst", dir));
146 fn ui_test_usage_help_flag() {
147 insta::with_settings!({filters => vec![
148 // binary name is `ouch.exe` on Windows and `ouch` on everywhere else
149 (r"(Usage:.*\b)ouch(\.exe)?\b", "${1}<OUCH_BIN>"),
151 ui!(output_to_string(ouch!("--help")));
152 ui!(output_to_string(ouch!("-h")));
156 /// Concatenates `with_rar` or `without_rar` if the feature is toggled or not.
157 fn concat_snapshot_filename_rar_feature(name: &str) -> String {
158 let suffix = if cfg!(feature = "unrar") {
164 format!("{name}_{suffix}")