Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter total # of checks and filenames in snapshots #867

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ similar-asserts = { version = "1.5.0", features = ["serde"] }
predicates = "3.1.0"
insta = { version = "1.39.0", features = ["ron", "filters"] }
fs-err = "2.11.0"
regex = "1.10.6"

# In dev and test profiles, compile all dependencies with optimizations enabled,
# but still checking debug assertions and overflows.
Expand Down
24 changes: 24 additions & 0 deletions src/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use std::{
cell::RefCell,
fmt,
io::{Cursor, Write},
path::{Path, PathBuf},
rc::Rc,
};

Expand Down Expand Up @@ -151,6 +152,14 @@ fn assert_integration_test(test_name: &str, invocation: &[&str]) {
settings.set_snapshot_path("../test_outputs/snapshot_tests");
// Turn dynamic time strings like [ 0.123s] into [TIME] for reproducibility.
settings.add_filter(r"\[\s*[\d\.]+s\]", "[TIME]");
// Turn total number of checks into [TOTAL] to not fail when new lints are added.
settings.add_filter(r"\d+ checks", "[TOTAL] checks");
// Similarly, turn the number of passed checks to also not fail when new lints are added.
settings.add_filter(r"\d+ pass", "[PASS] pass");
// Escape the root path (e.g., in lint spans) for deterministic results in different
// build environments.
let repo_root = get_root_path();
settings.add_filter(&regex::escape(&repo_root.to_string_lossy()), "[ROOT]");

// The `settings` are applied to the current thread as long as the returned
// drop guard `_grd` is alive, so we use a `let` binding to keep it alive
Expand Down Expand Up @@ -180,6 +189,21 @@ fn assert_integration_test(test_name: &str, invocation: &[&str]) {
insta::assert_snapshot!(format!("{test_name}-output"), result);
}

/// Helper function to get the root of the source code repository, for
/// filtering the path in snapshots.
fn get_root_path() -> PathBuf {
let canonicalized = Path::new(file!())
.canonicalize()
.expect("canonicalization failed");
// this file is in `$ROOT/src/snapshot_tests.rs`, so the repo root is two `parent`s up.
let repo_root = canonicalized
.parent()
.and_then(Path::parent)
.expect("getting repo root failed");

repo_root.to_owned()
}

/// [#163](/~https://github.com/obi1kenobi/cargo-semver-checks/issues/163)
///
/// Running `cargo semver-checks --workspace` on a workspace that doesn't
Expand Down
Loading