Skip to content

Commit fb2532a

Browse files
committed
Auto merge of #10581 - epage:snapbox, r=ehuss
Integrate snapbox in with cargo-test-support ### What does this PR try to resolve? #10472 introduced snapbox to cargo's tests in the least intrusive manner by copying some cargo-test-support code. Primarily, this PR works to de-duplicate that code. Secondarily, it makes it possible for snapbox to be used by other cargo tests that can work with its more limited functionality compared to cargo-test-support. ### How should we test and review this PR? This is broken down by commits for smaller chunks to look over with some extra details in some of the commit messages. As this is effectively refactoring existing tests, them passing is sufficient for testing. The main focus would be on any API design including if there are any practices that we used to do that this continues forward to snapbox that we shouldn't. ### Additional information The cargo contributing guide also needs to be updated but I'm leaving that off for another PR once this is merged so we have a clearer idea of what the API will look like (less churn) and so we can focus the conversation for each PR.
2 parents 78b10d4 + d6e912c commit fb2532a

File tree

5 files changed

+627
-536
lines changed

5 files changed

+627
-536
lines changed

crates/cargo-test-support/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ doctest = false
1111
anyhow = "1.0.34"
1212
cargo-test-macro = { path = "../cargo-test-macro" }
1313
cargo-util = { path = "../cargo-util" }
14+
snapbox = { version = "0.2.8", features = ["diff", "path"] }
1415
filetime = "0.2"
1516
flate2 = { version = "1.0", default-features = false, features = ["zlib"] }
1617
git2 = "0.14.2"

crates/cargo-test-support/src/compare.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,57 @@ use std::path::Path;
4141
use std::str;
4242
use url::Url;
4343

44+
/// Default `snapbox` Assertions
45+
///
46+
/// # Snapshots
47+
///
48+
/// Updating of snapshots is controlled with the `SNAPSHOTS` environment variable:
49+
///
50+
/// - `skip`: do not run the tests
51+
/// - `ignore`: run the tests but ignore their failure
52+
/// - `verify`: run the tests
53+
/// - `overwrite`: update the snapshots based on the output of the tests
54+
///
55+
/// # Patterns
56+
///
57+
/// - `[..]` is a character wildcard, stopping at line breaks
58+
/// - `\n...\n` is a multi-line wildcard
59+
/// - `[EXE]` matches the exe suffix for the current platform
60+
/// - `[ROOT]` matches [`paths::root()`][crate::paths::root]
61+
/// - `[ROOTURL]` matches [`paths::root()`][crate::paths::root] as a URL
62+
///
63+
/// # Normalization
64+
///
65+
/// In addition to the patterns described above, text is normalized
66+
/// in such a way to avoid unwanted differences. The normalizations are:
67+
///
68+
/// - Backslashes are converted to forward slashes to deal with Windows paths.
69+
/// This helps so that all tests can be written assuming forward slashes.
70+
/// Other heuristics are applied to try to ensure Windows-style paths aren't
71+
/// a problem.
72+
/// - Carriage returns are removed, which can help when running on Windows.
73+
pub fn assert() -> snapbox::Assert {
74+
let root = paths::root();
75+
// Use `from_file_path` instead of `from_dir_path` so the trailing slash is
76+
// put in the users output, rather than hidden in the variable
77+
let root_url = url::Url::from_file_path(&root).unwrap().to_string();
78+
let root = root.display().to_string();
79+
80+
let mut subs = snapbox::Substitutions::new();
81+
subs.extend([
82+
(
83+
"[EXE]",
84+
std::borrow::Cow::Borrowed(std::env::consts::EXE_SUFFIX),
85+
),
86+
("[ROOT]", std::borrow::Cow::Owned(root)),
87+
("[ROOTURL]", std::borrow::Cow::Owned(root_url)),
88+
])
89+
.unwrap();
90+
snapbox::Assert::new()
91+
.action_env(snapbox::DEFAULT_ACTION_ENV)
92+
.substitutions(subs)
93+
}
94+
4495
/// Normalizes the output so that it can be compared against the expected value.
4596
fn normalize_actual(actual: &str, cwd: Option<&Path>) -> String {
4697
// It's easier to read tabs in outputs if they don't show up as literal

0 commit comments

Comments
 (0)