Skip to content

Commit 3054936

Browse files
committed
refactor: Port from assert_matches_exact to assert_e2e
This leaves off `validate_crate_contents` as that would be an effort on its own
1 parent fe5c2d3 commit 3054936

File tree

14 files changed

+253
-140
lines changed

14 files changed

+253
-140
lines changed

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,60 @@ pub fn assert_ui() -> snapbox::Assert {
9797
.redact_with(subs)
9898
}
9999

100+
/// Assertion policy for functional end-to-end tests
101+
///
102+
/// This emphasizes showing as much content as possible at the cost of more brittleness
103+
///
104+
/// # Snapshots
105+
///
106+
/// Updating of snapshots is controlled with the `SNAPSHOTS` environment variable:
107+
///
108+
/// - `skip`: do not run the tests
109+
/// - `ignore`: run the tests but ignore their failure
110+
/// - `verify`: run the tests
111+
/// - `overwrite`: update the snapshots based on the output of the tests
112+
///
113+
/// # Patterns
114+
///
115+
/// - `[..]` is a character wildcard, stopping at line breaks
116+
/// - `\n...\n` is a multi-line wildcard
117+
/// - `[EXE]` matches the exe suffix for the current platform
118+
/// - `[ROOT]` matches [`paths::root()`][crate::paths::root]
119+
/// - `[ROOTURL]` matches [`paths::root()`][crate::paths::root] as a URL
120+
///
121+
/// # Normalization
122+
///
123+
/// In addition to the patterns described above, text is normalized
124+
/// in such a way to avoid unwanted differences. The normalizations are:
125+
///
126+
/// - Backslashes are converted to forward slashes to deal with Windows paths.
127+
/// This helps so that all tests can be written assuming forward slashes.
128+
/// Other heuristics are applied to try to ensure Windows-style paths aren't
129+
/// a problem.
130+
/// - Carriage returns are removed, which can help when running on Windows.
131+
pub fn assert_e2e() -> snapbox::Assert {
132+
let root = paths::root();
133+
// Use `from_file_path` instead of `from_dir_path` so the trailing slash is
134+
// put in the users output, rather than hidden in the variable
135+
let root_url = url::Url::from_file_path(&root).unwrap().to_string();
136+
137+
let mut subs = snapbox::Redactions::new();
138+
subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned())
139+
.unwrap();
140+
subs.extend(E2E_LITERAL_REDACTIONS.into_iter().cloned())
141+
.unwrap();
142+
subs.insert("[ROOT]", root).unwrap();
143+
subs.insert("[ROOTURL]", root_url).unwrap();
144+
subs.insert(
145+
"[ELAPSED]",
146+
regex::Regex::new("[FINISHED].*in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
147+
)
148+
.unwrap();
149+
snapbox::Assert::new()
150+
.action_env(snapbox::assert::DEFAULT_ACTION_ENV)
151+
.redact_with(subs)
152+
}
153+
100154
static MIN_LITERAL_REDACTIONS: &[(&str, &str)] = &[("[EXE]", std::env::consts::EXE_SUFFIX)];
101155
static E2E_LITERAL_REDACTIONS: &[(&str, &str)] = &[
102156
("[RUNNING]", " Running"),
@@ -287,7 +341,7 @@ pub(crate) fn match_exact(
287341

288342
/// Convenience wrapper around [`match_exact`] which will panic on error.
289343
#[track_caller]
290-
pub fn assert_match_exact(expected: &str, actual: &str) {
344+
pub(crate) fn assert_match_exact(expected: &str, actual: &str) {
291345
if let Err(e) = match_exact(expected, actual, "", "", None) {
292346
crate::panic_error("", e);
293347
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod prelude {
7676
pub use crate::CargoCommand;
7777
pub use crate::ChannelChanger;
7878
pub use crate::TestEnv;
79+
pub use snapbox::IntoData;
7980
}
8081

8182
/*

tests/testsuite/alt_registry.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Tests for alternative registries.
22
3-
use cargo_test_support::compare::assert_match_exact;
3+
use cargo_test_support::compare::assert_e2e;
44
use cargo_test_support::publish::validate_alt_upload;
55
use cargo_test_support::registry::{self, Package, RegistryBuilder};
6+
use cargo_test_support::str;
67
use cargo_test_support::{basic_manifest, paths, project};
78
use std::fs;
89

@@ -1476,9 +1477,10 @@ fn sparse_lockfile() {
14761477
.build();
14771478

14781479
p.cargo("generate-lockfile").run();
1479-
assert_match_exact(
1480+
assert_e2e().eq(
14801481
&p.read_lockfile(),
1481-
r#"# This file is automatically @generated by Cargo.
1482+
str![[r##"
1483+
# This file is automatically @generated by Cargo.
14821484
# It is not intended for manual editing.
14831485
version = 3
14841486
@@ -1492,8 +1494,10 @@ dependencies = [
14921494
[[package]]
14931495
name = "foo"
14941496
version = "0.1.0"
1495-
source = "sparse+http://[..]/"
1496-
checksum = "458c1addb23fde7dfbca0410afdbcc0086f96197281ec304d9e0e10def3cb899""#,
1497+
source = "sparse+http://127.0.0.1:[..]/index/"
1498+
checksum = "458c1addb23fde7dfbca0410afdbcc0086f96197281ec304d9e0e10def3cb899"
1499+
1500+
"##]],
14971501
);
14981502
}
14991503

tests/testsuite/artifact_dep.rs

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Tests specific to artifact dependencies, designated using
22
//! the new `dep = { artifact = "bin", … }` syntax in manifests.
33
4-
use cargo_test_support::compare;
4+
use cargo_test_support::compare::assert_e2e;
55
use cargo_test_support::registry::{Package, RegistryBuilder};
6+
use cargo_test_support::str;
67
use cargo_test_support::{
78
basic_bin_manifest, basic_manifest, cross_compile, project, publish, registry, rustc_host,
89
Project,
@@ -595,25 +596,31 @@ fn build_script_with_bin_artifacts() {
595596
let build_script_output = build_script_output_string(&p, "foo");
596597
// we need the binary directory for this artifact along with all binary paths
597598
if cfg!(target_env = "msvc") {
598-
compare::assert_match_exact(
599-
"[..]/artifact/bar-[..]/bin/baz.exe\n\
600-
[..]/artifact/bar-[..]/staticlib/bar-[..].lib\n\
601-
[..]/artifact/bar-[..]/cdylib/bar.dll\n\
602-
[..]/artifact/bar-[..]/bin\n\
603-
[..]/artifact/bar-[..]/bin/bar.exe\n\
604-
[..]/artifact/bar-[..]/bin/bar.exe",
599+
assert_e2e().eq(
605600
&build_script_output,
606-
)
601+
str![[r#"
602+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/baz[EXE]
603+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/staticlib/bar-[..].lib
604+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/cdylib/bar.dll
605+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin
606+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar[EXE]
607+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar[EXE]
608+
609+
"#]],
610+
);
607611
} else {
608-
compare::assert_match_exact(
609-
"[..]/artifact/bar-[..]/bin/baz-[..]\n\
610-
[..]/artifact/bar-[..]/staticlib/libbar-[..].a\n\
611-
[..]/artifact/bar-[..]/cdylib/[..]bar.[..]\n\
612-
[..]/artifact/bar-[..]/bin\n\
613-
[..]/artifact/bar-[..]/bin/bar-[..]\n\
614-
[..]/artifact/bar-[..]/bin/bar-[..]",
612+
assert_e2e().eq(
615613
&build_script_output,
616-
)
614+
str![[r#"
615+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/baz-[..]
616+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/staticlib/libbar-[..].a
617+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/cdylib/[..]bar.[..]
618+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin
619+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar-[..]
620+
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar-[..]
621+
622+
"#]],
623+
);
617624
}
618625

619626
assert!(
@@ -777,19 +784,22 @@ fn build_script_with_selected_dashed_bin_artifact_and_lib_true() {
777784
let build_script_output = build_script_output_string(&p, "foo");
778785
// we need the binary directory for this artifact and the binary itself
779786
if cfg!(target_env = "msvc") {
780-
compare::assert_match_exact(
781-
&format!(
782-
"[..]/artifact/bar-baz-[..]/bin\n\
783-
[..]/artifact/bar-baz-[..]/bin/baz_suffix{}",
784-
std::env::consts::EXE_SUFFIX,
785-
),
787+
assert_e2e().eq(
786788
&build_script_output,
789+
str![[r#"
790+
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin
791+
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin/baz_suffix[EXE]
792+
793+
"#]],
787794
);
788795
} else {
789-
compare::assert_match_exact(
790-
"[..]/artifact/bar-baz-[..]/bin\n\
791-
[..]/artifact/bar-baz-[..]/bin/baz_suffix-[..]",
796+
assert_e2e().eq(
792797
&build_script_output,
798+
str![[r#"
799+
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin
800+
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin/baz_suffix-[..]
801+
802+
"#]],
793803
);
794804
}
795805

@@ -1740,7 +1750,14 @@ fn allow_dep_renames_with_multiple_versions() {
17401750
.with_stderr_contains("[COMPILING] foo [..]")
17411751
.run();
17421752
let build_script_output = build_script_output_string(&p, "foo");
1743-
compare::assert_match_exact("0.5.0\n1.0.0", &build_script_output);
1753+
assert_e2e().eq(
1754+
&build_script_output,
1755+
str![[r#"
1756+
0.5.0
1757+
1.0.0
1758+
1759+
"#]],
1760+
);
17441761
}
17451762

17461763
#[cargo_test]
@@ -3216,18 +3233,26 @@ fn build_only_specified_artifact_library() {
32163233
.cargo("build -Z bindeps")
32173234
.masquerade_as_nightly_cargo(&["bindeps"])
32183235
.run();
3219-
compare::assert_match_exact(
3220-
"cdylib present: true\nstaticlib present: false",
3236+
assert_e2e().eq(
32213237
&build_script_output_string(&cdylib, "foo"),
3238+
str![[r#"
3239+
cdylib present: true
3240+
staticlib present: false
3241+
3242+
"#]],
32223243
);
32233244

32243245
let staticlib = create_project("staticlib");
32253246
staticlib
32263247
.cargo("build -Z bindeps")
32273248
.masquerade_as_nightly_cargo(&["bindeps"])
32283249
.run();
3229-
compare::assert_match_exact(
3230-
"cdylib present: false\nstaticlib present: true",
3250+
assert_e2e().eq(
32313251
&build_script_output_string(&staticlib, "foo"),
3252+
str![[r#"
3253+
cdylib present: false
3254+
staticlib present: true
3255+
3256+
"#]],
32323257
);
32333258
}

tests/testsuite/build_script.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Tests for build.rs scripts.
22
3-
use cargo_test_support::compare::assert_match_exact;
3+
use cargo_test_support::compare::assert_e2e;
44
use cargo_test_support::install::cargo_home;
55
use cargo_test_support::paths::CargoPathExt;
66
use cargo_test_support::registry::Package;
7+
use cargo_test_support::str;
78
use cargo_test_support::tools;
89
use cargo_test_support::{
910
basic_manifest, cargo_exe, cross_compile, is_coarse_mtime, project, project_in,
@@ -3398,9 +3399,12 @@ fn generate_good_d_files() {
33983399

33993400
println!("*.d file content*: {}", &dot_d);
34003401

3401-
assert_match_exact(
3402-
"[..]/target/debug/meow[EXE]: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]",
3402+
assert_e2e().eq(
34033403
&dot_d,
3404+
str![[r#"
3405+
[ROOT]/foo/target/debug/meow[EXE]: [ROOT]/foo/awoo/barkbarkbark [ROOT]/foo/awoo/build.rs [ROOT]/foo/awoo/src/lib.rs [ROOT]/foo/src/main.rs
3406+
3407+
"#]],
34043408
);
34053409

34063410
// paths relative to dependency roots should not be allowed
@@ -3421,9 +3425,12 @@ fn generate_good_d_files() {
34213425

34223426
println!("*.d file content with dep-info-basedir*: {}", &dot_d);
34233427

3424-
assert_match_exact(
3425-
"target/debug/meow[EXE]: awoo/barkbarkbark awoo/build.rs[..]",
3428+
assert_e2e().eq(
34263429
&dot_d,
3430+
str![[r#"
3431+
target/debug/meow[EXE]: awoo/barkbarkbark awoo/build.rs awoo/src/lib.rs src/main.rs
3432+
3433+
"#]],
34273434
);
34283435

34293436
// paths relative to dependency roots should not be allowed
@@ -3485,16 +3492,10 @@ fn generate_good_d_files_for_external_tools() {
34853492

34863493
println!("*.d file content with dep-info-basedir*: {}", &dot_d);
34873494

3488-
assert_match_exact(
3489-
concat!(
3490-
"rust_things/foo/target/debug/meow[EXE]:",
3491-
" rust_things/foo/awoo/barkbarkbark",
3492-
" rust_things/foo/awoo/build.rs",
3493-
" rust_things/foo/awoo/src/lib.rs",
3494-
" rust_things/foo/src/main.rs",
3495-
),
3496-
&dot_d,
3497-
);
3495+
assert_e2e().eq(&dot_d, str![[r#"
3496+
rust_things/foo/target/debug/meow[EXE]: rust_things/foo/awoo/barkbarkbark rust_things/foo/awoo/build.rs rust_things/foo/awoo/src/lib.rs rust_things/foo/src/main.rs
3497+
3498+
"#]]);
34983499
}
34993500

35003501
#[cargo_test]

tests/testsuite/check.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
use std::fmt::{self, Write};
44

55
use crate::messages::raw_rustc_output;
6-
use cargo_test_support::compare;
6+
use cargo_test_support::compare::assert_e2e;
77
use cargo_test_support::install::exe;
88
use cargo_test_support::paths::CargoPathExt;
99
use cargo_test_support::registry::Package;
10+
use cargo_test_support::str;
1011
use cargo_test_support::tools;
1112
use cargo_test_support::{basic_bin_manifest, basic_manifest, git, project};
1213

@@ -1553,7 +1554,10 @@ fn pkgid_querystring_works() {
15531554
let output = p.cargo("pkgid").arg("gitdep").exec_with_output().unwrap();
15541555
let gitdep_pkgid = String::from_utf8(output.stdout).unwrap();
15551556
let gitdep_pkgid = gitdep_pkgid.trim();
1556-
compare::assert_match_exact("git+file://[..]/gitdep?branch=master#1.0.0", &gitdep_pkgid);
1557+
assert_e2e().eq(
1558+
gitdep_pkgid,
1559+
str!["git+[ROOTURL]/gitdep?branch=master#1.0.0"],
1560+
);
15571561

15581562
p.cargo("build -p")
15591563
.arg(gitdep_pkgid)

0 commit comments

Comments
 (0)