Make test outputs identical on all platforms

This commit is contained in:
cyqsimon 2023-12-10 18:23:45 +08:00 committed by João Marcos
parent 21007e8ee1
commit 1c30f51051
8 changed files with 27 additions and 23 deletions

2
Cargo.lock generated
View File

@ -613,6 +613,7 @@ dependencies = [
"console", "console",
"lazy_static", "lazy_static",
"linked-hash-map", "linked-hash-map",
"regex",
"similar", "similar",
"yaml-rust", "yaml-rust",
] ]
@ -842,6 +843,7 @@ dependencies = [
"proptest", "proptest",
"rand", "rand",
"rayon", "rayon",
"regex",
"same-file", "same-file",
"sevenz-rust", "sevenz-rust",
"snap", "snap",

View File

@ -47,10 +47,11 @@ clap_mangen = "0.2.15"
[dev-dependencies] [dev-dependencies]
assert_cmd = "2.0.12" assert_cmd = "2.0.12"
infer = "0.15.0" infer = "0.15.0"
insta = "1.34.0" insta = { version = "1.34.0", features = ["filters"] }
parse-display = "0.8.2" parse-display = "0.8.2"
proptest = "1.4.0" proptest = "1.4.0"
rand = { version = "0.8.5", default-features = false, features = ["small_rng", "std"] } rand = { version = "0.8.5", default-features = false, features = ["small_rng", "std"] }
regex = "1.10.2"
test-strategy = "0.3.1" test-strategy = "0.3.1"
[features] [features]

View File

@ -3,8 +3,8 @@ source: tests/ui.rs
expression: "run_ouch(\"ouch decompress a b.unknown\", dir)" expression: "run_ouch(\"ouch decompress a b.unknown\", dir)"
--- ---
[ERROR] Cannot decompress files [ERROR] Cannot decompress files
- Files with unsupported extensions: <FOLDER>/b.unknown - Files with unsupported extensions: <TMP_DIR>/b.unknown
- Files with missing extensions: <FOLDER>/a - Files with missing extensions: <TMP_DIR>/a
- Decompression formats are detected automatically from file extension - 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 extensions are: tar, zip, bz, bz2, gz, lz4, xz, lzma, sz, zst, rar

View File

@ -3,12 +3,12 @@ source: tests/ui.rs
expression: "run_ouch(\"ouch decompress b.unknown\", dir)" expression: "run_ouch(\"ouch decompress b.unknown\", dir)"
--- ---
[ERROR] Cannot decompress files [ERROR] Cannot decompress files
- Files with unsupported extensions: <FOLDER>/b.unknown - Files with unsupported extensions: <TMP_DIR>/b.unknown
- Decompression formats are detected automatically from file extension - 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 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: Supported aliases are: tgz, tbz, tlz4, txz, tzlma, tsz, tzst
hint: hint:
hint: Alternatively, you can pass an extension to the '--format' flag: hint: Alternatively, you can pass an extension to the '--format' flag:
hint: ouch decompress <FOLDER>/b.unknown --format tar.gz hint: ouch decompress <TMP_DIR>/b.unknown --format tar.gz

View File

@ -3,12 +3,12 @@ source: tests/ui.rs
expression: "run_ouch(\"ouch decompress a\", dir)" expression: "run_ouch(\"ouch decompress a\", dir)"
--- ---
[ERROR] Cannot decompress files [ERROR] Cannot decompress files
- Files with missing extensions: <FOLDER>/a - Files with missing extensions: <TMP_DIR>/a
- Decompression formats are detected automatically from file extension - 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 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: Supported aliases are: tgz, tbz, tlz4, txz, tzlma, tsz, tzst
hint: hint:
hint: Alternatively, you can pass an extension to the '--format' flag: hint: Alternatively, you can pass an extension to the '--format' flag:
hint: ouch decompress <FOLDER>/a --format tar.gz hint: ouch decompress <TMP_DIR>/a --format tar.gz

View File

@ -4,7 +4,7 @@ expression: "output_to_string(ouch!(\"-h\"))"
--- ---
A command-line utility for easily compressing and decompressing files and directories. A command-line utility for easily compressing and decompressing files and directories.
Usage: ouch [OPTIONS] <COMMAND> Usage: <OUCH_BIN> [OPTIONS] <COMMAND>
Commands: Commands:
compress Compress one or more files into one output file [aliases: c] compress Compress one or more files into one output file [aliases: c]

View File

@ -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 Repository: https://github.com/ouch-org/ouch
Usage: ouch [OPTIONS] <COMMAND> Usage: <OUCH_BIN> [OPTIONS] <COMMAND>
Commands: Commands:
compress Compress one or more files into one output file [aliases: c] compress Compress one or more files into one output file [aliases: c]

View File

@ -6,9 +6,10 @@
#[macro_use] #[macro_use]
mod utils; 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 insta::assert_display_snapshot as ui;
use regex::Regex;
use crate::utils::run_in; use crate::utils::run_in;
@ -35,18 +36,13 @@ fn run_ouch(argv: &str, dir: &Path) -> String {
redact_paths(&output_to_string(output), dir) redact_paths(&output_to_string(output), dir)
} }
// remove random tempdir paths from snapshots to make them deterministic /// Remove random tempdir paths from snapshots to make them deterministic.
fn redact_paths(text: &str, path: &Path) -> String { fn redact_paths(text: &str, dir: &Path) -> String {
let redacted = "<FOLDER>"; let dir_name = dir.file_name().and_then(OsStr::to_str).unwrap();
let path = path.display(); // this regex should be good as long as the path does not contain whitespace characters
let path = if cfg!(target_os = "macos") { let re = Regex::new(&format!(r"\S*[/\\]{dir_name}[/\\]")).unwrap();
format!(r"/private{path}") re.replace_all(text, "<TMP_DIR>/").into()
} else {
path.to_string()
};
text.replace(path.as_str(), redacted)
} }
fn output_to_string(output: Output) -> String { fn output_to_string(output: Output) -> String {
@ -107,6 +103,11 @@ fn ui_test_ok_decompress() {
#[test] #[test]
fn ui_test_usage_help_flag() { fn ui_test_usage_help_flag() {
ui!(output_to_string(ouch!("--help"))); insta::with_settings!({filters => vec![
ui!(output_to_string(ouch!("-h"))); // binary name is `ouch.exe` on Windows and `ouch` on everywhere else
(r"(Usage:.*\b)ouch(\.exe)?\b", "${1}<OUCH_BIN>"),
]}, {
ui!(output_to_string(ouch!("--help")));
ui!(output_to_string(ouch!("-h")));
});
} }