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",
"lazy_static",
"linked-hash-map",
"regex",
"similar",
"yaml-rust",
]
@ -842,6 +843,7 @@ dependencies = [
"proptest",
"rand",
"rayon",
"regex",
"same-file",
"sevenz-rust",
"snap",

View File

@ -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]

View File

@ -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: <FOLDER>/b.unknown
- Files with missing extensions: <FOLDER>/a
- Files with unsupported extensions: <TMP_DIR>/b.unknown
- Files with missing extensions: <TMP_DIR>/a
- Decompression formats are detected automatically from file extension
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)"
---
[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
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 <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)"
---
[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
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 <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.
Usage: ouch [OPTIONS] <COMMAND>
Usage: <OUCH_BIN> [OPTIONS] <COMMAND>
Commands:
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
Usage: ouch [OPTIONS] <COMMAND>
Usage: <OUCH_BIN> [OPTIONS] <COMMAND>
Commands:
compress Compress one or more files into one output file [aliases: c]

View File

@ -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 = "<FOLDER>";
/// 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, "<TMP_DIR>/").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}<OUCH_BIN>"),
]}, {
ui!(output_to_string(ouch!("--help")));
ui!(output_to_string(ouch!("-h")));
});
}