From b73e3d4fa57caceb6ce025581121e36fc3a019b1 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 15 Jun 2021 19:23:17 -0700 Subject: [PATCH] Don't export lines_match. Use better high-level interfaces to achieve the same thing. --- crates/cargo-test-support/src/compare.rs | 22 ++++++------ crates/cargo-test-support/src/publish.rs | 13 ++----- tests/testsuite/build_script.rs | 46 ++++-------------------- tests/testsuite/features_namespaced.rs | 10 ++++-- tests/testsuite/lockfile_compat.rs | 27 +++++--------- tests/testsuite/publish.rs | 36 +++++++++++++++---- tests/testsuite/weak_dep_features.rs | 5 ++- 7 files changed, 69 insertions(+), 90 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index ebae661e2..1d923bd6c 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -165,7 +165,7 @@ pub fn match_exact( "{} did not match:\n\ {}\n\n\ other output:\n\ - `{}`", + {}\n", description, diffs.join("\n"), other_output, @@ -173,6 +173,14 @@ pub fn match_exact( } } +/// Convenience wrapper around [`match_exact`] which will panic on error. +#[track_caller] +pub fn assert_match_exact(expected: &str, actual: &str) { + if let Err(e) = match_exact(expected, actual, "", "", None) { + crate::panic_error("", e); + } +} + /// Checks that the given string contains the given lines, ignoring the order /// of the lines. /// @@ -487,17 +495,7 @@ fn zip_all, I2: Iterator>(a: I1, b: I2) -> Z } } -/// Compares a line with an expected pattern. -/// - Use `[..]` as a wildcard to match 0 or more characters on the same line -/// (similar to `.*` in a regex). It is non-greedy. -/// - Use `[EXE]` to optionally add `.exe` on Windows (empty string on other -/// platforms). -/// - There is a wide range of macros (such as `[COMPILING]` or `[WARNING]`) -/// to match cargo's "status" output and allows you to ignore the alignment. -/// See `substitute_macros` for a complete list of macros. -/// - `[ROOT]` the path to the test directory's root -/// - `[CWD]` is the working directory of the process that was run. -pub fn lines_match(expected: &str, mut actual: &str) -> bool { +fn lines_match(expected: &str, mut actual: &str) -> bool { for (i, part) in expected.split("[..]").enumerate() { match actual.find(part) { Some(j) => { diff --git a/crates/cargo-test-support/src/publish.rs b/crates/cargo-test-support/src/publish.rs index a0b31be21..94f2559a7 100644 --- a/crates/cargo-test-support/src/publish.rs +++ b/crates/cargo-test-support/src/publish.rs @@ -1,4 +1,4 @@ -use crate::compare::{find_json_mismatch, lines_match}; +use crate::compare::{assert_match_exact, find_json_mismatch}; use crate::registry::{self, alt_api_path}; use flate2::read::GzDecoder; use std::collections::{HashMap, HashSet}; @@ -151,16 +151,7 @@ pub fn validate_crate_contents( let actual_contents = files .get(&full_e_name) .unwrap_or_else(|| panic!("file `{}` missing in archive", e_file_name)); - if !lines_match(e_file_contents, actual_contents) { - panic!( - "Crate contents mismatch for {:?}:\n\ - --- expected\n\ - {}\n\ - --- actual \n\ - {}\n", - e_file_name, e_file_contents, actual_contents - ); - } + assert_match_exact(e_file_contents, actual_contents); } } } diff --git a/tests/testsuite/build_script.rs b/tests/testsuite/build_script.rs index 46ccb703e..da0bb4ba5 100644 --- a/tests/testsuite/build_script.rs +++ b/tests/testsuite/build_script.rs @@ -1,6 +1,6 @@ //! Tests for build.rs scripts. -use cargo_test_support::compare::lines_match; +use cargo_test_support::compare::assert_match_exact; use cargo_test_support::paths::CargoPathExt; use cargo_test_support::registry::Package; use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project}; @@ -3038,25 +3038,9 @@ fn generate_good_d_files() { println!("*.d file content*: {}", &dot_d); - #[cfg(windows)] - assert!( - lines_match( - "[..]\\target\\debug\\meow.exe: [..]\\awoo\\barkbarkbark [..]\\awoo\\build.rs[..]", - &dot_d - ) || lines_match( - "[..]\\target\\debug\\meow.exe: [..]\\awoo\\build.rs [..]\\awoo\\barkbarkbark[..]", - &dot_d - ) - ); - #[cfg(not(windows))] - assert!( - lines_match( - "[..]/target/debug/meow: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]", - &dot_d - ) || lines_match( - "[..]/target/debug/meow: [..]/awoo/build.rs [..]/awoo/barkbarkbark[..]", - &dot_d - ) + assert_match_exact( + "[..]/target/debug/meow[EXE]: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]", + &dot_d, ); // paths relative to dependency roots should not be allowed @@ -3077,25 +3061,9 @@ fn generate_good_d_files() { println!("*.d file content with dep-info-basedir*: {}", &dot_d); - #[cfg(windows)] - assert!( - lines_match( - "target\\debug\\meow.exe: [..]awoo\\barkbarkbark [..]awoo\\build.rs[..]", - &dot_d - ) || lines_match( - "target\\debug\\meow.exe: [..]awoo\\build.rs [..]awoo\\barkbarkbark[..]", - &dot_d - ) - ); - #[cfg(not(windows))] - assert!( - lines_match( - "target/debug/meow: [..]awoo/barkbarkbark [..]awoo/build.rs[..]", - &dot_d - ) || lines_match( - "target/debug/meow: [..]awoo/build.rs [..]awoo/barkbarkbark[..]", - &dot_d - ) + assert_match_exact( + "target/debug/meow[EXE]: awoo/barkbarkbark awoo/build.rs[..]", + &dot_d, ); // paths relative to dependency roots should not be allowed diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index 0c3022c8d..2e92b2bdb 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -1151,7 +1151,8 @@ fn publish_no_implicit() { &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], &[( "Cargo.toml", - r#"[..] + &format!( + r#"{} [package] name = "foo" version = "0.1.0" @@ -1169,6 +1170,8 @@ optional = true [features] feat = ["opt-dep1"] "#, + cargo::core::package::MANIFEST_PREAMBLE + ), )], ); } @@ -1255,7 +1258,8 @@ fn publish() { &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], &[( "Cargo.toml", - r#"[..] + &format!( + r#"{} [package] name = "foo" version = "0.1.0" @@ -1271,6 +1275,8 @@ feat1 = [] feat2 = ["dep:bar"] feat3 = ["feat2"] "#, + cargo::core::package::MANIFEST_PREAMBLE + ), )], ); } diff --git a/tests/testsuite/lockfile_compat.rs b/tests/testsuite/lockfile_compat.rs index 8955beaae..087e28ac9 100644 --- a/tests/testsuite/lockfile_compat.rs +++ b/tests/testsuite/lockfile_compat.rs @@ -1,6 +1,6 @@ //! Tests for supporting older versions of the Cargo.lock file format. -use cargo_test_support::compare::lines_match; +use cargo_test_support::compare::assert_match_exact; use cargo_test_support::git; use cargo_test_support::registry::Package; use cargo_test_support::{basic_lib_manifest, basic_manifest, project}; @@ -13,15 +13,6 @@ fn oldest_lockfile_still_works() { } } -#[track_caller] -fn assert_lockfiles_eq(expected: &str, actual: &str) { - for (l, r) in expected.lines().zip(actual.lines()) { - assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r); - } - - assert_eq!(expected.lines().count(), actual.lines().count()); -} - fn oldest_lockfile_still_works_with_command(cargo_command: &str) { Package::new("bar", "0.1.0").publish(); @@ -77,7 +68,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" p.cargo(cargo_command).run(); let lock = p.read_lockfile(); - assert_lockfiles_eq(expected_lockfile, &lock); + assert_match_exact(expected_lockfile, &lock); } #[cargo_test] @@ -123,7 +114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" p.cargo("build --locked").run(); let lock = p.read_lockfile(); - assert_lockfiles_eq(&old_lockfile, &lock); + assert_match_exact(&old_lockfile, &lock); } #[cargo_test] @@ -170,7 +161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" p.cargo("build").run(); let lock = p.read_lockfile(); - assert_lockfiles_eq( + assert_match_exact( r#"# This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 @@ -428,7 +419,7 @@ dependencies = [ \"bar\", ] "; - assert_lockfiles_eq(expected, &actual); + assert_match_exact(expected, &actual); } #[cargo_test] @@ -472,7 +463,7 @@ dependencies = [ p.cargo("build").run(); let lock = p.read_lockfile(); - assert_lockfiles_eq( + assert_match_exact( r#"# [..] # [..] version = 3 @@ -569,7 +560,7 @@ dependencies = [ p.cargo("fetch").run(); let lock = p.read_lockfile(); - assert_lockfiles_eq(&lockfile, &lock); + assert_match_exact(&lockfile, &lock); } #[cargo_test] @@ -640,7 +631,7 @@ dependencies = [ p.cargo("fetch").run(); let lock = p.read_lockfile(); - assert_lockfiles_eq(&lockfile, &lock); + assert_match_exact(&lockfile, &lock); } #[cargo_test] @@ -696,7 +687,7 @@ dependencies = [ p.cargo("fetch").run(); let lock = p.read_lockfile(); - assert_lockfiles_eq(&lockfile, &lock); + assert_match_exact(&lockfile, &lock); } #[cargo_test] diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 95addc04c..f05ca6d28 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -1213,22 +1213,41 @@ fn publish_git_with_version() { ( "Cargo.toml", // Check that only `version` is included in Cargo.toml. - "[..]\n\ - [dependencies.dep1]\n\ - version = \"1.0\"\n\ - ", + &format!( + "{}\n\ + [package]\n\ + edition = \"2018\"\n\ + name = \"foo\"\n\ + version = \"0.1.0\"\n\ + authors = []\n\ + description = \"foo\"\n\ + license = \"MIT\"\n\ + [dependencies.dep1]\n\ + version = \"1.0\"\n\ + ", + cargo::core::package::MANIFEST_PREAMBLE + ), ), ( "Cargo.lock", // The important check here is that it is 1.0.1 in the registry. - "[..]\n\ + "# This file is automatically @generated by Cargo.\n\ + # It is not intended for manual editing.\n\ + version = 3\n\ + \n\ + [[package]]\n\ + name = \"dep1\"\n\ + version = \"1.0.1\"\n\ + source = \"registry+https://github.com/rust-lang/crates.io-index\"\n\ + checksum = \"[..]\"\n\ + \n\ [[package]]\n\ name = \"foo\"\n\ version = \"0.1.0\"\n\ dependencies = [\n\ \x20\"dep1\",\n\ ]\n\ - [..]", + ", ), ], ); @@ -1297,7 +1316,8 @@ fn publish_dev_dep_no_version() { &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], &[( "Cargo.toml", - r#"[..] + &format!( + r#"{} [package] name = "foo" version = "0.1.0" @@ -1310,6 +1330,8 @@ repository = "foo" [dev-dependencies] "#, + cargo::core::package::MANIFEST_PREAMBLE + ), )], ); } diff --git a/tests/testsuite/weak_dep_features.rs b/tests/testsuite/weak_dep_features.rs index d51c40772..4d3bf8b1a 100644 --- a/tests/testsuite/weak_dep_features.rs +++ b/tests/testsuite/weak_dep_features.rs @@ -702,7 +702,8 @@ fn publish() { &["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"], &[( "Cargo.toml", - r#"[..] + &format!( + r#"{} [package] name = "foo" version = "0.1.0" @@ -717,6 +718,8 @@ optional = true feat1 = [] feat2 = ["bar?/feat"] "#, + cargo::core::package::MANIFEST_PREAMBLE + ), )], ); }