refactor: Port from assert_matches_exact to assert_e2e

This leaves off `validate_crate_contents` as that would be an effort on
its own
This commit is contained in:
Ed Page 2024-05-24 14:30:40 -05:00
parent fe5c2d393a
commit 3054936cab
14 changed files with 253 additions and 140 deletions

View File

@ -97,6 +97,60 @@ pub fn assert_ui() -> snapbox::Assert {
.redact_with(subs) .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 MIN_LITERAL_REDACTIONS: &[(&str, &str)] = &[("[EXE]", std::env::consts::EXE_SUFFIX)];
static E2E_LITERAL_REDACTIONS: &[(&str, &str)] = &[ static E2E_LITERAL_REDACTIONS: &[(&str, &str)] = &[
("[RUNNING]", " Running"), ("[RUNNING]", " Running"),
@ -287,7 +341,7 @@ pub(crate) fn match_exact(
/// Convenience wrapper around [`match_exact`] which will panic on error. /// Convenience wrapper around [`match_exact`] which will panic on error.
#[track_caller] #[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) { if let Err(e) = match_exact(expected, actual, "", "", None) {
crate::panic_error("", e); crate::panic_error("", e);
} }

View File

@ -76,6 +76,7 @@ pub mod prelude {
pub use crate::CargoCommand; pub use crate::CargoCommand;
pub use crate::ChannelChanger; pub use crate::ChannelChanger;
pub use crate::TestEnv; pub use crate::TestEnv;
pub use snapbox::IntoData;
} }
/* /*

View File

@ -1,8 +1,9 @@
//! Tests for alternative registries. //! 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::publish::validate_alt_upload;
use cargo_test_support::registry::{self, Package, RegistryBuilder}; use cargo_test_support::registry::{self, Package, RegistryBuilder};
use cargo_test_support::str;
use cargo_test_support::{basic_manifest, paths, project}; use cargo_test_support::{basic_manifest, paths, project};
use std::fs; use std::fs;
@ -1476,9 +1477,10 @@ fn sparse_lockfile() {
.build(); .build();
p.cargo("generate-lockfile").run(); p.cargo("generate-lockfile").run();
assert_match_exact( assert_e2e().eq(
&p.read_lockfile(), &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. # It is not intended for manual editing.
version = 3 version = 3
@ -1492,8 +1494,10 @@ dependencies = [
[[package]] [[package]]
name = "foo" name = "foo"
version = "0.1.0" version = "0.1.0"
source = "sparse+http://[..]/" source = "sparse+http://127.0.0.1:[..]/index/"
checksum = "458c1addb23fde7dfbca0410afdbcc0086f96197281ec304d9e0e10def3cb899""#, checksum = "458c1addb23fde7dfbca0410afdbcc0086f96197281ec304d9e0e10def3cb899"
"##]],
); );
} }

View File

@ -1,8 +1,9 @@
//! Tests specific to artifact dependencies, designated using //! Tests specific to artifact dependencies, designated using
//! the new `dep = { artifact = "bin", … }` syntax in manifests. //! 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::registry::{Package, RegistryBuilder};
use cargo_test_support::str;
use cargo_test_support::{ use cargo_test_support::{
basic_bin_manifest, basic_manifest, cross_compile, project, publish, registry, rustc_host, basic_bin_manifest, basic_manifest, cross_compile, project, publish, registry, rustc_host,
Project, Project,
@ -595,25 +596,31 @@ fn build_script_with_bin_artifacts() {
let build_script_output = build_script_output_string(&p, "foo"); let build_script_output = build_script_output_string(&p, "foo");
// we need the binary directory for this artifact along with all binary paths // we need the binary directory for this artifact along with all binary paths
if cfg!(target_env = "msvc") { if cfg!(target_env = "msvc") {
compare::assert_match_exact( assert_e2e().eq(
"[..]/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",
&build_script_output, &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 { } else {
compare::assert_match_exact( assert_e2e().eq(
"[..]/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-[..]",
&build_script_output, &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!( assert!(
@ -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"); let build_script_output = build_script_output_string(&p, "foo");
// we need the binary directory for this artifact and the binary itself // we need the binary directory for this artifact and the binary itself
if cfg!(target_env = "msvc") { if cfg!(target_env = "msvc") {
compare::assert_match_exact( assert_e2e().eq(
&format!(
"[..]/artifact/bar-baz-[..]/bin\n\
[..]/artifact/bar-baz-[..]/bin/baz_suffix{}",
std::env::consts::EXE_SUFFIX,
),
&build_script_output, &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 { } else {
compare::assert_match_exact( assert_e2e().eq(
"[..]/artifact/bar-baz-[..]/bin\n\
[..]/artifact/bar-baz-[..]/bin/baz_suffix-[..]",
&build_script_output, &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-[..]
"#]],
); );
} }
@ -1740,7 +1750,14 @@ fn allow_dep_renames_with_multiple_versions() {
.with_stderr_contains("[COMPILING] foo [..]") .with_stderr_contains("[COMPILING] foo [..]")
.run(); .run();
let build_script_output = build_script_output_string(&p, "foo"); 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] #[cargo_test]
@ -3216,9 +3233,13 @@ fn build_only_specified_artifact_library() {
.cargo("build -Z bindeps") .cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"]) .masquerade_as_nightly_cargo(&["bindeps"])
.run(); .run();
compare::assert_match_exact( assert_e2e().eq(
"cdylib present: true\nstaticlib present: false",
&build_script_output_string(&cdylib, "foo"), &build_script_output_string(&cdylib, "foo"),
str![[r#"
cdylib present: true
staticlib present: false
"#]],
); );
let staticlib = create_project("staticlib"); let staticlib = create_project("staticlib");
@ -3226,8 +3247,12 @@ fn build_only_specified_artifact_library() {
.cargo("build -Z bindeps") .cargo("build -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"]) .masquerade_as_nightly_cargo(&["bindeps"])
.run(); .run();
compare::assert_match_exact( assert_e2e().eq(
"cdylib present: false\nstaticlib present: true",
&build_script_output_string(&staticlib, "foo"), &build_script_output_string(&staticlib, "foo"),
str![[r#"
cdylib present: false
staticlib present: true
"#]],
); );
} }

View File

@ -1,9 +1,10 @@
//! Tests for build.rs scripts. //! 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::install::cargo_home;
use cargo_test_support::paths::CargoPathExt; use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package; use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::tools; use cargo_test_support::tools;
use cargo_test_support::{ use cargo_test_support::{
basic_manifest, cargo_exe, cross_compile, is_coarse_mtime, project, project_in, basic_manifest, cargo_exe, cross_compile, is_coarse_mtime, project, project_in,
@ -3398,9 +3399,12 @@ fn generate_good_d_files() {
println!("*.d file content*: {}", &dot_d); println!("*.d file content*: {}", &dot_d);
assert_match_exact( assert_e2e().eq(
"[..]/target/debug/meow[EXE]: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]",
&dot_d, &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 // paths relative to dependency roots should not be allowed
@ -3421,9 +3425,12 @@ fn generate_good_d_files() {
println!("*.d file content with dep-info-basedir*: {}", &dot_d); println!("*.d file content with dep-info-basedir*: {}", &dot_d);
assert_match_exact( assert_e2e().eq(
"target/debug/meow[EXE]: awoo/barkbarkbark awoo/build.rs[..]",
&dot_d, &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 // paths relative to dependency roots should not be allowed
@ -3485,16 +3492,10 @@ fn generate_good_d_files_for_external_tools() {
println!("*.d file content with dep-info-basedir*: {}", &dot_d); println!("*.d file content with dep-info-basedir*: {}", &dot_d);
assert_match_exact( assert_e2e().eq(&dot_d, str![[r#"
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
"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,
);
} }
#[cargo_test] #[cargo_test]

View File

@ -3,10 +3,11 @@
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use crate::messages::raw_rustc_output; 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::install::exe;
use cargo_test_support::paths::CargoPathExt; use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package; use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::tools; use cargo_test_support::tools;
use cargo_test_support::{basic_bin_manifest, basic_manifest, git, project}; use cargo_test_support::{basic_bin_manifest, basic_manifest, git, project};
@ -1553,7 +1554,10 @@ fn pkgid_querystring_works() {
let output = p.cargo("pkgid").arg("gitdep").exec_with_output().unwrap(); let output = p.cargo("pkgid").arg("gitdep").exec_with_output().unwrap();
let gitdep_pkgid = String::from_utf8(output.stdout).unwrap(); let gitdep_pkgid = String::from_utf8(output.stdout).unwrap();
let gitdep_pkgid = gitdep_pkgid.trim(); 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") p.cargo("build -p")
.arg(gitdep_pkgid) .arg(gitdep_pkgid)

View File

@ -5,7 +5,9 @@ use cargo::util::context::{
self, Definition, GlobalContext, JobsConfig, SslVersionConfig, StringList, self, Definition, GlobalContext, JobsConfig, SslVersionConfig, StringList,
}; };
use cargo::CargoResult; use cargo::CargoResult;
use cargo_test_support::compare; use cargo_test_support::compare::assert_e2e;
use cargo_test_support::prelude::*;
use cargo_test_support::str;
use cargo_test_support::{paths, project, symlink_supported, t}; use cargo_test_support::{paths, project, symlink_supported, t};
use cargo_util_schemas::manifest::TomlTrimPaths; use cargo_util_schemas::manifest::TomlTrimPaths;
use cargo_util_schemas::manifest::TomlTrimPathsValue; use cargo_util_schemas::manifest::TomlTrimPathsValue;
@ -208,7 +210,7 @@ fn rename_config_toml_to_config_replacing_with_symlink() {
} }
#[track_caller] #[track_caller]
pub fn assert_error<E: Borrow<anyhow::Error>>(error: E, msgs: &str) { pub fn assert_error<E: Borrow<anyhow::Error>>(error: E, msgs: impl IntoData) {
let causes = error let causes = error
.borrow() .borrow()
.chain() .chain()
@ -222,7 +224,7 @@ pub fn assert_error<E: Borrow<anyhow::Error>>(error: E, msgs: &str) {
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\n\n"); .join("\n\n");
compare::assert_match_exact(msgs, &causes); assert_e2e().eq(&causes, msgs);
} }
#[cargo_test] #[cargo_test]
@ -287,10 +289,12 @@ f1 = 1
// It should NOT have warned for the symlink. // It should NOT have warned for the symlink.
let output = read_output(gctx); let output = read_output(gctx);
let expected = "\ let expected = str![[r#"
[WARNING] `[ROOT]/.cargo/config` is deprecated in favor of `config.toml` [WARNING] `[ROOT]/.cargo/config` is deprecated in favor of `config.toml`
[NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`"; [NOTE] if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
compare::assert_match_exact(expected, &output);
"#]];
assert_e2e().eq(&output, expected);
} }
#[cargo_test] #[cargo_test]
@ -316,7 +320,7 @@ f1 = 1
// It should NOT have warned for the symlink. // It should NOT have warned for the symlink.
let output = read_output(gctx); let output = read_output(gctx);
compare::assert_match_exact("", &output); assert_e2e().eq(&output, str![[""]]);
} }
#[cargo_test] #[cargo_test]
@ -342,7 +346,7 @@ f1 = 1
// It should NOT have warned for the symlink. // It should NOT have warned for the symlink.
let output = read_output(gctx); let output = read_output(gctx);
compare::assert_match_exact("", &output); assert_e2e().eq(&output, str![[""]]);
} }
#[cargo_test] #[cargo_test]
@ -368,7 +372,7 @@ f1 = 1
// It should NOT have warned for this situation. // It should NOT have warned for this situation.
let output = read_output(gctx); let output = read_output(gctx);
compare::assert_match_exact("", &output); assert_e2e().eq(&output, str![[""]]);
} }
#[cargo_test] #[cargo_test]
@ -395,10 +399,11 @@ f1 = 2
// But it also should have warned. // But it also should have warned.
let output = read_output(gctx); let output = read_output(gctx);
let expected = "\ let expected = str![[r#"
[WARNING] both `[..]/.cargo/config` and `[..]/.cargo/config.toml` exist. Using `[..]/.cargo/config` [WARNING] both `[ROOT]/.cargo/config` and `[ROOT]/.cargo/config.toml` exist. Using `[ROOT]/.cargo/config`
";
compare::assert_match_exact(expected, &output); "#]];
assert_e2e().eq(&output, expected);
} }
#[cargo_test] #[cargo_test]
@ -429,10 +434,11 @@ unused = 456
// Verify the warnings. // Verify the warnings.
let output = read_output(gctx); let output = read_output(gctx);
let expected = "\ let expected = str![[r#"
warning: unused config key `S.unused` in `[..]/.cargo/config.toml` [WARNING] unused config key `S.unused` in `[ROOT]/.cargo/config.toml`
";
compare::assert_match_exact(expected, &output); "#]];
assert_e2e().eq(&output, expected);
} }
#[cargo_test] #[cargo_test]
@ -824,7 +830,8 @@ Caused by:
| |
1 | asdf 1 | asdf
| ^ | ^
expected `.`, `=`", expected `.`, `=`
",
); );
} }
@ -1630,7 +1637,7 @@ fn all_profile_options() {
let profile_toml = toml::to_string(&profile).unwrap(); let profile_toml = toml::to_string(&profile).unwrap();
let roundtrip: cargo_toml::TomlProfile = toml::from_str(&profile_toml).unwrap(); let roundtrip: cargo_toml::TomlProfile = toml::from_str(&profile_toml).unwrap();
let roundtrip_toml = toml::to_string(&roundtrip).unwrap(); let roundtrip_toml = toml::to_string(&roundtrip).unwrap();
compare::assert_match_exact(&profile_toml, &roundtrip_toml); assert_e2e().eq(&roundtrip_toml, &profile_toml);
} }
#[cargo_test] #[cargo_test]

View File

@ -4,8 +4,9 @@ use super::config::{
assert_error, read_output, write_config_at, write_config_toml, GlobalContextBuilder, assert_error, read_output, write_config_at, write_config_toml, GlobalContextBuilder,
}; };
use cargo::util::context::Definition; use cargo::util::context::Definition;
use cargo_test_support::compare; use cargo_test_support::compare::assert_e2e;
use cargo_test_support::paths; use cargo_test_support::paths;
use cargo_test_support::str;
use std::{collections::HashMap, fs}; use std::{collections::HashMap, fs};
#[cargo_test] #[cargo_test]
@ -348,10 +349,11 @@ fn unused_key() {
gctx.build_config().unwrap(); gctx.build_config().unwrap();
let output = read_output(gctx); let output = read_output(gctx);
let expected = "\ let expected = str![[r#"
warning: unused config key `build.unused` in `--config cli option` [WARNING] unused config key `build.unused` in `--config cli option`
";
compare::assert_match_exact(expected, &output); "#]];
assert_e2e().eq(&output, expected);
} }
#[cargo_test] #[cargo_test]

View File

@ -182,8 +182,7 @@ fn wrong_file_extension() {
could not load Cargo configuration could not load Cargo configuration
Caused by: Caused by:
expected a config include path ending with `.toml`, but found `config.png` from `[..]/.cargo/config.toml` expected a config include path ending with `.toml`, but found `config.png` from `[ROOT]/.cargo/config.toml`",
",
); );
} }

View File

@ -1,9 +1,10 @@
//! Tests for dep-info files. This includes the dep-info file Cargo creates in //! Tests for dep-info files. This includes the dep-info file Cargo creates in
//! the output directory, and the ones stored in the fingerprint. //! the output directory, and the ones stored in the fingerprint.
use cargo_test_support::compare::assert_match_exact; use cargo_test_support::compare::assert_e2e;
use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::registry::Package; use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::{ use cargo_test_support::{
basic_bin_manifest, basic_manifest, main_file, project, rustc_host, Project, basic_bin_manifest, basic_manifest, main_file, project, rustc_host, Project,
}; };
@ -593,8 +594,11 @@ fn non_local_build_script() {
p.cargo("build").run(); p.cargo("build").run();
let contents = p.read_file("target/debug/foo.d"); let contents = p.read_file("target/debug/foo.d");
assert_match_exact( assert_e2e().eq(
"[ROOT]/foo/target/debug/foo[EXE]: [ROOT]/foo/src/main.rs",
&contents, &contents,
str![[r#"
[ROOT]/foo/target/debug/foo[EXE]: [ROOT]/foo/src/main.rs
"#]],
); );
} }

View File

@ -1,10 +1,11 @@
//! Tests for the `cargo fix` command. //! Tests for the `cargo fix` command.
use cargo::core::Edition; use cargo::core::Edition;
use cargo_test_support::compare::assert_match_exact; use cargo_test_support::compare::assert_e2e;
use cargo_test_support::git::{self, init}; use cargo_test_support::git::{self, init};
use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::registry::{Dependency, Package}; use cargo_test_support::registry::{Dependency, Package};
use cargo_test_support::str;
use cargo_test_support::tools; use cargo_test_support::tools;
use cargo_test_support::{basic_manifest, is_nightly, project}; use cargo_test_support::{basic_manifest, is_nightly, project};
@ -1537,9 +1538,9 @@ fn fix_shared_cross_workspace() {
) )
.run(); .run();
assert_match_exact( assert_e2e().eq(
"pub fn fixme(_x: Box<&dyn Fn() -> ()>) {}",
&p.read_file("foo/src/shared.rs"), &p.read_file("foo/src/shared.rs"),
str!["pub fn fixme(_x: Box<&dyn Fn() -> ()>) {}"],
); );
} }

View File

@ -6,9 +6,11 @@ use std::path::Path;
use std::thread; use std::thread;
use cargo_test_support::compare; use cargo_test_support::compare;
use cargo_test_support::compare::assert_e2e;
use cargo_test_support::cross_compile; use cargo_test_support::cross_compile;
use cargo_test_support::git; use cargo_test_support::git;
use cargo_test_support::registry::{self, registry_path, Package}; use cargo_test_support::registry::{self, registry_path, Package};
use cargo_test_support::str;
use cargo_test_support::{ use cargo_test_support::{
basic_manifest, cargo_process, no_such_file_err_msg, project, project_in, symlink_supported, t, basic_manifest, cargo_process, no_such_file_err_msg, project, project_in, symlink_supported, t,
}; };
@ -2354,39 +2356,39 @@ fn sparse_install() {
assert_has_installed_exe(cargo_home(), "foo"); assert_has_installed_exe(cargo_home(), "foo");
let assert_v1 = |expected| { let assert_v1 = |expected| {
let v1 = fs::read_to_string(paths::home().join(".cargo/.crates.toml")).unwrap(); let v1 = fs::read_to_string(paths::home().join(".cargo/.crates.toml")).unwrap();
compare::assert_match_exact(expected, &v1); assert_e2e().eq(&v1, expected);
}; };
assert_v1( assert_v1(str![[r#"
r#"[v1] [v1]
"foo 0.0.1 (sparse+http://127.0.0.1:[..]/index/)" = ["foo[EXE]"] "foo 0.0.1 (sparse+http://127.0.0.1:[..]/index/)" = ["foo[EXE]"]
"#,
); "#]]);
cargo_process("install bar").run(); cargo_process("install bar").run();
assert_has_installed_exe(cargo_home(), "bar"); assert_has_installed_exe(cargo_home(), "bar");
assert_v1( assert_v1(str![[r#"
r#"[v1] [v1]
"bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = ["bar[EXE]"] "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = ["bar[EXE]"]
"foo 0.0.1 (sparse+http://127.0.0.1:[..]/index/)" = ["foo[EXE]"] "foo 0.0.1 (sparse+http://127.0.0.1:[..]/index/)" = ["foo[EXE]"]
"#,
); "#]]);
cargo_process("uninstall bar") cargo_process("uninstall bar")
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/bar[EXE]") .with_stderr("[REMOVING] [CWD]/home/.cargo/bin/bar[EXE]")
.run(); .run();
assert_has_not_installed_exe(cargo_home(), "bar"); assert_has_not_installed_exe(cargo_home(), "bar");
assert_v1( assert_v1(str![[r#"
r#"[v1] [v1]
"foo 0.0.1 (sparse+http://127.0.0.1:[..]/index/)" = ["foo[EXE]"] "foo 0.0.1 (sparse+http://127.0.0.1:[..]/index/)" = ["foo[EXE]"]
"#,
); "#]]);
cargo_process("uninstall foo") cargo_process("uninstall foo")
.with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]") .with_stderr("[REMOVING] [CWD]/home/.cargo/bin/foo[EXE]")
.run(); .run();
assert_has_not_installed_exe(cargo_home(), "foo"); assert_has_not_installed_exe(cargo_home(), "foo");
assert_v1( assert_v1(str![[r#"
r#"[v1] [v1]
"#,
); "#]]);
} }
#[cargo_test] #[cargo_test]

View File

@ -1,8 +1,9 @@
//! Tests for supporting older versions of the Cargo.lock file format. //! Tests for supporting older versions of the Cargo.lock file format.
use cargo_test_support::compare::assert_match_exact; use cargo_test_support::compare::assert_e2e;
use cargo_test_support::git; use cargo_test_support::git;
use cargo_test_support::registry::Package; use cargo_test_support::registry::Package;
use cargo_test_support::str;
use cargo_test_support::{basic_lib_manifest, basic_manifest, project}; use cargo_test_support::{basic_lib_manifest, basic_manifest, project};
#[cargo_test] #[cargo_test]
@ -16,7 +17,8 @@ fn oldest_lockfile_still_works() {
fn oldest_lockfile_still_works_with_command(cargo_command: &str) { fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
Package::new("bar", "0.1.0").publish(); Package::new("bar", "0.1.0").publish();
let expected_lockfile = r#"# This file is automatically @generated by Cargo. let expected_lockfile = str![[r##"
# This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
@ -32,7 +34,8 @@ version = "0.0.1"
dependencies = [ dependencies = [
"bar", "bar",
] ]
"#;
"##]];
let old_lockfile = r#" let old_lockfile = r#"
[root] [root]
@ -69,7 +72,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
p.cargo(cargo_command).run(); p.cargo(cargo_command).run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(expected_lockfile, &lock); assert_e2e().eq(&lock, expected_lockfile);
} }
#[cargo_test] #[cargo_test]
@ -116,7 +119,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
p.cargo("check --locked").run(); p.cargo("check --locked").run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(&old_lockfile, &lock); assert_e2e().eq(&lock, &old_lockfile);
} }
#[cargo_test] #[cargo_test]
@ -164,8 +167,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
p.cargo("check").run(); p.cargo("check").run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact( assert_e2e().eq(
r#"# This file is automatically @generated by Cargo. &lock,
str![[r##"
# This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
@ -181,8 +186,8 @@ version = "0.0.1"
dependencies = [ dependencies = [
"bar", "bar",
] ]
"#,
&lock, "##]],
); );
} }
@ -409,24 +414,26 @@ fn current_lockfile_format() {
let actual = p.read_lockfile(); let actual = p.read_lockfile();
let expected = "\ let expected = str![[r##"
# This file is automatically @generated by Cargo.\n# It is not intended for manual editing. # This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3 version = 3
[[package]] [[package]]
name = \"bar\" name = "bar"
version = \"0.1.0\" version = "0.1.0"
source = \"registry+https://github.com/rust-lang/crates.io-index\" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = \"[..]\" checksum = "[..]"
[[package]] [[package]]
name = \"foo\" name = "foo"
version = \"0.0.1\" version = "0.0.1"
dependencies = [ dependencies = [
\"bar\", "bar",
] ]
";
assert_match_exact(expected, &actual); "##]];
assert_e2e().eq(&actual, expected);
} }
#[cargo_test] #[cargo_test]
@ -471,9 +478,11 @@ dependencies = [
p.cargo("check").run(); p.cargo("check").run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact( assert_e2e().eq(
r#"# [..] &lock,
# [..] str![[r##"
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3 version = 3
[[package]] [[package]]
@ -488,8 +497,8 @@ version = "0.0.1"
dependencies = [ dependencies = [
"bar", "bar",
] ]
"#,
&lock, "##]],
); );
} }
@ -571,7 +580,7 @@ dependencies = [
p.cargo("fetch").run(); p.cargo("fetch").run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(&lockfile, &lock); assert_e2e().eq(&lock, &lockfile);
} }
#[cargo_test] #[cargo_test]
@ -644,7 +653,7 @@ dependencies = [
p.cargo("fetch").run(); p.cargo("fetch").run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(&lockfile, &lock); assert_e2e().eq(&lock, &lockfile);
} }
#[cargo_test] #[cargo_test]
@ -664,7 +673,7 @@ version = 3
[[package]] [[package]]
name = "dep1" name = "dep1"
version = "0.5.0" version = "0.5.0"
source = "git+{}?branch=master#{}" source = "git+[ROOTURL]/dep1?branch=master#{}"
[[package]] [[package]]
name = "foo" name = "foo"
@ -673,7 +682,6 @@ dependencies = [
"dep1", "dep1",
] ]
"#, "#,
git_project.url(),
head_id, head_id,
); );
@ -701,7 +709,7 @@ dependencies = [
p.cargo("fetch").run(); p.cargo("fetch").run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(&lockfile, &lock); assert_e2e().eq(&lock, &lockfile);
} }
#[cargo_test] #[cargo_test]
@ -982,7 +990,7 @@ version = "0.0.1"
"#; "#;
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(lockfile, &lock); assert_e2e().eq(&lock, lockfile);
} }
fn create_branch(repo: &git2::Repository, branch: &str, head_id: git2::Oid) { fn create_branch(repo: &git2::Repository, branch: &str, head_id: git2::Oid) {
@ -1021,7 +1029,7 @@ version = 3
[[package]] [[package]]
name = "dep1" name = "dep1"
version = "0.5.0" version = "0.5.0"
source = "git+{url}?{ref_kind}={git_ref}#{head_id}" source = "git+[ROOTURL]/dep1?{ref_kind}={git_ref}#{head_id}"
[[package]] [[package]]
name = "foo" name = "foo"
@ -1066,7 +1074,7 @@ dependencies = [
.run(); .run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(&lockfile, &lock); assert_e2e().eq(&lock, &lockfile);
// v3 doesn't URL-encode URL parameters, but `url` crate does decode as it // v3 doesn't URL-encode URL parameters, but `url` crate does decode as it
// was URL-encoded. Therefore Cargo thinks they are from different source // was URL-encoded. Therefore Cargo thinks they are from different source
@ -1117,7 +1125,7 @@ version = 4
[[package]] [[package]]
name = "dep1" name = "dep1"
version = "0.5.0" version = "0.5.0"
source = "git+{url}?{ref_kind}={encoded_ref}#{head_id}" source = "git+[ROOTURL]/dep1?{ref_kind}={encoded_ref}#{head_id}"
[[package]] [[package]]
name = "foo" name = "foo"
@ -1162,7 +1170,7 @@ dependencies = [
.run(); .run();
let lock = p.read_lockfile(); let lock = p.read_lockfile();
assert_match_exact(&lockfile, &lock); assert_e2e().eq(&lock, &lockfile);
// Unlike v3_and_git_url_encoded, v4 encodes URL parameters so no git // Unlike v3_and_git_url_encoded, v4 encodes URL parameters so no git
// repository re-clone happen. // repository re-clone happen.

View File

@ -1,10 +1,11 @@
//! Tests for the `cargo pkgid` command. //! Tests for the `cargo pkgid` command.
use cargo_test_support::basic_lib_manifest; use cargo_test_support::basic_lib_manifest;
use cargo_test_support::compare; use cargo_test_support::compare::assert_e2e;
use cargo_test_support::git; use cargo_test_support::git;
use cargo_test_support::project; use cargo_test_support::project;
use cargo_test_support::registry::Package; use cargo_test_support::registry::Package;
use cargo_test_support::str;
#[cargo_test] #[cargo_test]
fn local() { fn local() {
@ -305,7 +306,7 @@ fn pkgid_json_message_metadata_consistency() {
let output = p.cargo("pkgid").arg("foo").exec_with_output().unwrap(); let output = p.cargo("pkgid").arg("foo").exec_with_output().unwrap();
let pkgid = String::from_utf8(output.stdout).unwrap(); let pkgid = String::from_utf8(output.stdout).unwrap();
let pkgid = pkgid.trim(); let pkgid = pkgid.trim();
compare::assert_match_exact("path+file://[..]/foo#0.5.0", &pkgid); assert_e2e().eq(pkgid, str!["path+[ROOTURL]/foo#0.5.0"]);
p.cargo("check --message-format=json") p.cargo("check --message-format=json")
.with_json( .with_json(