Skip to content

Commit

Permalink
refactor: Port from assert_matches_exact to assert_e2e
Browse files Browse the repository at this point in the history
This leaves off `validate_crate_contents` as that would be an effort on
its own
  • Loading branch information
epage committed May 29, 2024
1 parent fe5c2d3 commit 3054936
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 140 deletions.
56 changes: 55 additions & 1 deletion crates/cargo-test-support/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,60 @@ pub fn assert_ui() -> snapbox::Assert {
.redact_with(subs)
}

/// Assertion policy for functional end-to-end tests
///
/// This emphasizes showing as much content as possible at the cost of more brittleness
///
/// # Snapshots
///
/// Updating of snapshots is controlled with the `SNAPSHOTS` environment variable:
///
/// - `skip`: do not run the tests
/// - `ignore`: run the tests but ignore their failure
/// - `verify`: run the tests
/// - `overwrite`: update the snapshots based on the output of the tests
///
/// # Patterns
///
/// - `[..]` is a character wildcard, stopping at line breaks
/// - `\n...\n` is a multi-line wildcard
/// - `[EXE]` matches the exe suffix for the current platform
/// - `[ROOT]` matches [`paths::root()`][crate::paths::root]
/// - `[ROOTURL]` matches [`paths::root()`][crate::paths::root] as a URL
///
/// # Normalization
///
/// In addition to the patterns described above, text is normalized
/// in such a way to avoid unwanted differences. The normalizations are:
///
/// - Backslashes are converted to forward slashes to deal with Windows paths.
/// This helps so that all tests can be written assuming forward slashes.
/// Other heuristics are applied to try to ensure Windows-style paths aren't
/// a problem.
/// - Carriage returns are removed, which can help when running on Windows.
pub fn assert_e2e() -> snapbox::Assert {
let root = paths::root();
// Use `from_file_path` instead of `from_dir_path` so the trailing slash is
// put in the users output, rather than hidden in the variable
let root_url = url::Url::from_file_path(&root).unwrap().to_string();

let mut subs = snapbox::Redactions::new();
subs.extend(MIN_LITERAL_REDACTIONS.into_iter().cloned())
.unwrap();
subs.extend(E2E_LITERAL_REDACTIONS.into_iter().cloned())
.unwrap();
subs.insert("[ROOT]", root).unwrap();
subs.insert("[ROOTURL]", root_url).unwrap();
subs.insert(
"[ELAPSED]",
regex::Regex::new("[FINISHED].*in (?<redacted>[0-9]+(\\.[0-9]+))s").unwrap(),
)
.unwrap();
snapbox::Assert::new()
.action_env(snapbox::assert::DEFAULT_ACTION_ENV)
.redact_with(subs)
}

static MIN_LITERAL_REDACTIONS: &[(&str, &str)] = &[("[EXE]", std::env::consts::EXE_SUFFIX)];
static E2E_LITERAL_REDACTIONS: &[(&str, &str)] = &[
("[RUNNING]", " Running"),
Expand Down Expand Up @@ -287,7 +341,7 @@ pub(crate) fn match_exact(

/// Convenience wrapper around [`match_exact`] which will panic on error.
#[track_caller]
pub fn assert_match_exact(expected: &str, actual: &str) {
pub(crate) fn assert_match_exact(expected: &str, actual: &str) {
if let Err(e) = match_exact(expected, actual, "", "", None) {
crate::panic_error("", e);
}
Expand Down
1 change: 1 addition & 0 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub mod prelude {
pub use crate::CargoCommand;
pub use crate::ChannelChanger;
pub use crate::TestEnv;
pub use snapbox::IntoData;
}

/*
Expand Down
14 changes: 9 additions & 5 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Tests for alternative registries.

use cargo_test_support::compare::assert_match_exact;
use cargo_test_support::compare::assert_e2e;
use cargo_test_support::publish::validate_alt_upload;
use cargo_test_support::registry::{self, Package, RegistryBuilder};
use cargo_test_support::str;
use cargo_test_support::{basic_manifest, paths, project};
use std::fs;

Expand Down Expand Up @@ -1476,9 +1477,10 @@ fn sparse_lockfile() {
.build();

p.cargo("generate-lockfile").run();
assert_match_exact(
assert_e2e().eq(
&p.read_lockfile(),
r#"# This file is automatically @generated by Cargo.
str![[r##"
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
Expand All @@ -1492,8 +1494,10 @@ dependencies = [
[[package]]
name = "foo"
version = "0.1.0"
source = "sparse+http://[..]/"
checksum = "458c1addb23fde7dfbca0410afdbcc0086f96197281ec304d9e0e10def3cb899""#,
source = "sparse+http://127.0.0.1:[..]/index/"
checksum = "458c1addb23fde7dfbca0410afdbcc0086f96197281ec304d9e0e10def3cb899"
"##]],
);
}

Expand Down
87 changes: 56 additions & 31 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Tests specific to artifact dependencies, designated using
//! the new `dep = { artifact = "bin", … }` syntax in manifests.

use cargo_test_support::compare;
use cargo_test_support::compare::assert_e2e;
use cargo_test_support::registry::{Package, RegistryBuilder};
use cargo_test_support::str;
use cargo_test_support::{
basic_bin_manifest, basic_manifest, cross_compile, project, publish, registry, rustc_host,
Project,
Expand Down Expand Up @@ -595,25 +596,31 @@ fn build_script_with_bin_artifacts() {
let build_script_output = build_script_output_string(&p, "foo");
// we need the binary directory for this artifact along with all binary paths
if cfg!(target_env = "msvc") {
compare::assert_match_exact(
"[..]/artifact/bar-[..]/bin/baz.exe\n\
[..]/artifact/bar-[..]/staticlib/bar-[..].lib\n\
[..]/artifact/bar-[..]/cdylib/bar.dll\n\
[..]/artifact/bar-[..]/bin\n\
[..]/artifact/bar-[..]/bin/bar.exe\n\
[..]/artifact/bar-[..]/bin/bar.exe",
assert_e2e().eq(
&build_script_output,
)
str![[r#"
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/baz[EXE]
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/staticlib/bar-[..].lib
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/cdylib/bar.dll
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar[EXE]
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar[EXE]
"#]],
);
} else {
compare::assert_match_exact(
"[..]/artifact/bar-[..]/bin/baz-[..]\n\
[..]/artifact/bar-[..]/staticlib/libbar-[..].a\n\
[..]/artifact/bar-[..]/cdylib/[..]bar.[..]\n\
[..]/artifact/bar-[..]/bin\n\
[..]/artifact/bar-[..]/bin/bar-[..]\n\
[..]/artifact/bar-[..]/bin/bar-[..]",
assert_e2e().eq(
&build_script_output,
)
str![[r#"
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/baz-[..]
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/staticlib/libbar-[..].a
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/cdylib/[..]bar.[..]
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar-[..]
[ROOT]/foo/target/debug/deps/artifact/bar-[..]/bin/bar-[..]
"#]],
);
}

assert!(
Expand Down Expand Up @@ -777,19 +784,22 @@ fn build_script_with_selected_dashed_bin_artifact_and_lib_true() {
let build_script_output = build_script_output_string(&p, "foo");
// we need the binary directory for this artifact and the binary itself
if cfg!(target_env = "msvc") {
compare::assert_match_exact(
&format!(
"[..]/artifact/bar-baz-[..]/bin\n\
[..]/artifact/bar-baz-[..]/bin/baz_suffix{}",
std::env::consts::EXE_SUFFIX,
),
assert_e2e().eq(
&build_script_output,
str![[r#"
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin/baz_suffix[EXE]
"#]],
);
} else {
compare::assert_match_exact(
"[..]/artifact/bar-baz-[..]/bin\n\
[..]/artifact/bar-baz-[..]/bin/baz_suffix-[..]",
assert_e2e().eq(
&build_script_output,
str![[r#"
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin
[ROOT]/foo/target/debug/deps/artifact/bar-baz-[..]/bin/baz_suffix-[..]
"#]],
);
}

Expand Down Expand Up @@ -1740,7 +1750,14 @@ fn allow_dep_renames_with_multiple_versions() {
.with_stderr_contains("[COMPILING] foo [..]")
.run();
let build_script_output = build_script_output_string(&p, "foo");
compare::assert_match_exact("0.5.0\n1.0.0", &build_script_output);
assert_e2e().eq(
&build_script_output,
str![[r#"
0.5.0
1.0.0
"#]],
);
}

#[cargo_test]
Expand Down Expand Up @@ -3216,18 +3233,26 @@ fn build_only_specified_artifact_library() {
.cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
compare::assert_match_exact(
"cdylib present: true\nstaticlib present: false",
assert_e2e().eq(
&build_script_output_string(&cdylib, "foo"),
str![[r#"
cdylib present: true
staticlib present: false
"#]],
);

let staticlib = create_project("staticlib");
staticlib
.cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
compare::assert_match_exact(
"cdylib present: false\nstaticlib present: true",
assert_e2e().eq(
&build_script_output_string(&staticlib, "foo"),
str![[r#"
cdylib present: false
staticlib present: true
"#]],
);
}
31 changes: 16 additions & 15 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Tests for build.rs scripts.

use cargo_test_support::compare::assert_match_exact;
use cargo_test_support::compare::assert_e2e;
use cargo_test_support::install::cargo_home;
use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::tools;
use cargo_test_support::{
basic_manifest, cargo_exe, cross_compile, is_coarse_mtime, project, project_in,
Expand Down Expand Up @@ -3398,9 +3399,12 @@ fn generate_good_d_files() {

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

assert_match_exact(
"[..]/target/debug/meow[EXE]: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]",
assert_e2e().eq(
&dot_d,
str![[r#"
[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
"#]],
);

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

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

assert_match_exact(
"target/debug/meow[EXE]: awoo/barkbarkbark awoo/build.rs[..]",
assert_e2e().eq(
&dot_d,
str![[r#"
target/debug/meow[EXE]: awoo/barkbarkbark awoo/build.rs awoo/src/lib.rs src/main.rs
"#]],
);

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

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

assert_match_exact(
concat!(
"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",
),
&dot_d,
);
assert_e2e().eq(&dot_d, str![[r#"
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
"#]]);
}

#[cargo_test]
Expand Down
8 changes: 6 additions & 2 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use std::fmt::{self, Write};

use crate::messages::raw_rustc_output;
use cargo_test_support::compare;
use cargo_test_support::compare::assert_e2e;
use cargo_test_support::install::exe;
use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::tools;
use cargo_test_support::{basic_bin_manifest, basic_manifest, git, project};

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

p.cargo("build -p")
.arg(gitdep_pkgid)
Expand Down
Loading

0 comments on commit 3054936

Please sign in to comment.