From 1c30f51051af36ed4f0212016898608f2b61839b Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sun, 10 Dec 2023 18:23:45 +0800 Subject: [PATCH] Make test outputs identical on all platforms --- Cargo.lock | 2 ++ Cargo.toml | 3 +- ...st_err_decompress_missing_extension-2.snap | 4 +-- ...st_err_decompress_missing_extension-3.snap | 4 +-- ...test_err_decompress_missing_extension.snap | 4 +-- .../ui__ui_test_usage_help_flag-2.snap | 2 +- .../ui__ui_test_usage_help_flag.snap | 2 +- tests/ui.rs | 29 ++++++++++--------- 8 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b6e428f..0b83ec3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -613,6 +613,7 @@ dependencies = [ "console", "lazy_static", "linked-hash-map", + "regex", "similar", "yaml-rust", ] @@ -842,6 +843,7 @@ dependencies = [ "proptest", "rand", "rayon", + "regex", "same-file", "sevenz-rust", "snap", diff --git a/Cargo.toml b/Cargo.toml index f6edacc..a871e64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,10 +47,11 @@ clap_mangen = "0.2.15" [dev-dependencies] assert_cmd = "2.0.12" infer = "0.15.0" -insta = "1.34.0" +insta = { version = "1.34.0", features = ["filters"] } parse-display = "0.8.2" proptest = "1.4.0" rand = { version = "0.8.5", default-features = false, features = ["small_rng", "std"] } +regex = "1.10.2" test-strategy = "0.3.1" [features] diff --git a/tests/snapshots/ui__ui_test_err_decompress_missing_extension-2.snap b/tests/snapshots/ui__ui_test_err_decompress_missing_extension-2.snap index 7bb834d..cc3fa02 100644 --- a/tests/snapshots/ui__ui_test_err_decompress_missing_extension-2.snap +++ b/tests/snapshots/ui__ui_test_err_decompress_missing_extension-2.snap @@ -3,8 +3,8 @@ source: tests/ui.rs expression: "run_ouch(\"ouch decompress a b.unknown\", dir)" --- [ERROR] Cannot decompress files - - Files with unsupported extensions: /b.unknown - - Files with missing extensions: /a + - Files with unsupported extensions: /b.unknown + - Files with missing extensions: /a - Decompression formats are detected automatically from file extension hint: Supported extensions are: tar, zip, bz, bz2, gz, lz4, xz, lzma, sz, zst, rar diff --git a/tests/snapshots/ui__ui_test_err_decompress_missing_extension-3.snap b/tests/snapshots/ui__ui_test_err_decompress_missing_extension-3.snap index 0192ef9..d2eb579 100644 --- a/tests/snapshots/ui__ui_test_err_decompress_missing_extension-3.snap +++ b/tests/snapshots/ui__ui_test_err_decompress_missing_extension-3.snap @@ -3,12 +3,12 @@ source: tests/ui.rs expression: "run_ouch(\"ouch decompress b.unknown\", dir)" --- [ERROR] Cannot decompress files - - Files with unsupported extensions: /b.unknown + - Files with unsupported extensions: /b.unknown - Decompression formats are detected automatically from file extension hint: Supported extensions are: tar, zip, bz, bz2, gz, lz4, xz, lzma, sz, zst, rar hint: Supported aliases are: tgz, tbz, tlz4, txz, tzlma, tsz, tzst hint: hint: Alternatively, you can pass an extension to the '--format' flag: -hint: ouch decompress /b.unknown --format tar.gz +hint: ouch decompress /b.unknown --format tar.gz diff --git a/tests/snapshots/ui__ui_test_err_decompress_missing_extension.snap b/tests/snapshots/ui__ui_test_err_decompress_missing_extension.snap index f74a81a..ac4c03b 100644 --- a/tests/snapshots/ui__ui_test_err_decompress_missing_extension.snap +++ b/tests/snapshots/ui__ui_test_err_decompress_missing_extension.snap @@ -3,12 +3,12 @@ source: tests/ui.rs expression: "run_ouch(\"ouch decompress a\", dir)" --- [ERROR] Cannot decompress files - - Files with missing extensions: /a + - Files with missing extensions: /a - Decompression formats are detected automatically from file extension hint: Supported extensions are: tar, zip, bz, bz2, gz, lz4, xz, lzma, sz, zst, rar hint: Supported aliases are: tgz, tbz, tlz4, txz, tzlma, tsz, tzst hint: hint: Alternatively, you can pass an extension to the '--format' flag: -hint: ouch decompress /a --format tar.gz +hint: ouch decompress /a --format tar.gz diff --git a/tests/snapshots/ui__ui_test_usage_help_flag-2.snap b/tests/snapshots/ui__ui_test_usage_help_flag-2.snap index b0b60d5..354ef21 100644 --- a/tests/snapshots/ui__ui_test_usage_help_flag-2.snap +++ b/tests/snapshots/ui__ui_test_usage_help_flag-2.snap @@ -4,7 +4,7 @@ expression: "output_to_string(ouch!(\"-h\"))" --- A command-line utility for easily compressing and decompressing files and directories. -Usage: ouch [OPTIONS] +Usage: [OPTIONS] Commands: compress Compress one or more files into one output file [aliases: c] diff --git a/tests/snapshots/ui__ui_test_usage_help_flag.snap b/tests/snapshots/ui__ui_test_usage_help_flag.snap index 7cf21d2..6861d00 100644 --- a/tests/snapshots/ui__ui_test_usage_help_flag.snap +++ b/tests/snapshots/ui__ui_test_usage_help_flag.snap @@ -8,7 +8,7 @@ Supported formats: tar, zip, gz, 7z, xz/lzma, bz/bz2, lz4, sz (Snappy), zst and Repository: https://github.com/ouch-org/ouch -Usage: ouch [OPTIONS] +Usage: [OPTIONS] Commands: compress Compress one or more files into one output file [aliases: c] diff --git a/tests/ui.rs b/tests/ui.rs index 85bea38..8d6cdef 100644 --- a/tests/ui.rs +++ b/tests/ui.rs @@ -6,9 +6,10 @@ #[macro_use] mod utils; -use std::{io, path::Path, process::Output}; +use std::{ffi::OsStr, io, path::Path, process::Output}; use insta::assert_display_snapshot as ui; +use regex::Regex; use crate::utils::run_in; @@ -35,18 +36,13 @@ fn run_ouch(argv: &str, dir: &Path) -> String { redact_paths(&output_to_string(output), dir) } -// remove random tempdir paths from snapshots to make them deterministic -fn redact_paths(text: &str, path: &Path) -> String { - let redacted = ""; +/// Remove random tempdir paths from snapshots to make them deterministic. +fn redact_paths(text: &str, dir: &Path) -> String { + let dir_name = dir.file_name().and_then(OsStr::to_str).unwrap(); - let path = path.display(); - let path = if cfg!(target_os = "macos") { - format!(r"/private{path}") - } else { - path.to_string() - }; - - text.replace(path.as_str(), redacted) + // this regex should be good as long as the path does not contain whitespace characters + let re = Regex::new(&format!(r"\S*[/\\]{dir_name}[/\\]")).unwrap(); + re.replace_all(text, "/").into() } fn output_to_string(output: Output) -> String { @@ -107,6 +103,11 @@ fn ui_test_ok_decompress() { #[test] fn ui_test_usage_help_flag() { - ui!(output_to_string(ouch!("--help"))); - ui!(output_to_string(ouch!("-h"))); + insta::with_settings!({filters => vec![ + // binary name is `ouch.exe` on Windows and `ouch` on everywhere else + (r"(Usage:.*\b)ouch(\.exe)?\b", "${1}"), + ]}, { + ui!(output_to_string(ouch!("--help"))); + ui!(output_to_string(ouch!("-h"))); + }); }