Skip to content

Commit b73e3d4

Browse files
committed
Don't export lines_match.
Use better high-level interfaces to achieve the same thing.
1 parent b9f15ab commit b73e3d4

File tree

7 files changed

+69
-90
lines changed

7 files changed

+69
-90
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,22 @@ pub fn match_exact(
165165
"{} did not match:\n\
166166
{}\n\n\
167167
other output:\n\
168-
`{}`",
168+
{}\n",
169169
description,
170170
diffs.join("\n"),
171171
other_output,
172172
)
173173
}
174174
}
175175

176+
/// Convenience wrapper around [`match_exact`] which will panic on error.
177+
#[track_caller]
178+
pub fn assert_match_exact(expected: &str, actual: &str) {
179+
if let Err(e) = match_exact(expected, actual, "", "", None) {
180+
crate::panic_error("", e);
181+
}
182+
}
183+
176184
/// Checks that the given string contains the given lines, ignoring the order
177185
/// of the lines.
178186
///
@@ -487,17 +495,7 @@ fn zip_all<T, I1: Iterator<Item = T>, I2: Iterator<Item = T>>(a: I1, b: I2) -> Z
487495
}
488496
}
489497

490-
/// Compares a line with an expected pattern.
491-
/// - Use `[..]` as a wildcard to match 0 or more characters on the same line
492-
/// (similar to `.*` in a regex). It is non-greedy.
493-
/// - Use `[EXE]` to optionally add `.exe` on Windows (empty string on other
494-
/// platforms).
495-
/// - There is a wide range of macros (such as `[COMPILING]` or `[WARNING]`)
496-
/// to match cargo's "status" output and allows you to ignore the alignment.
497-
/// See `substitute_macros` for a complete list of macros.
498-
/// - `[ROOT]` the path to the test directory's root
499-
/// - `[CWD]` is the working directory of the process that was run.
500-
pub fn lines_match(expected: &str, mut actual: &str) -> bool {
498+
fn lines_match(expected: &str, mut actual: &str) -> bool {
501499
for (i, part) in expected.split("[..]").enumerate() {
502500
match actual.find(part) {
503501
Some(j) => {

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::compare::{find_json_mismatch, lines_match};
1+
use crate::compare::{assert_match_exact, find_json_mismatch};
22
use crate::registry::{self, alt_api_path};
33
use flate2::read::GzDecoder;
44
use std::collections::{HashMap, HashSet};
@@ -151,16 +151,7 @@ pub fn validate_crate_contents(
151151
let actual_contents = files
152152
.get(&full_e_name)
153153
.unwrap_or_else(|| panic!("file `{}` missing in archive", e_file_name));
154-
if !lines_match(e_file_contents, actual_contents) {
155-
panic!(
156-
"Crate contents mismatch for {:?}:\n\
157-
--- expected\n\
158-
{}\n\
159-
--- actual \n\
160-
{}\n",
161-
e_file_name, e_file_contents, actual_contents
162-
);
163-
}
154+
assert_match_exact(e_file_contents, actual_contents);
164155
}
165156
}
166157
}

tests/testsuite/build_script.rs

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tests for build.rs scripts.
22
3-
use cargo_test_support::compare::lines_match;
3+
use cargo_test_support::compare::assert_match_exact;
44
use cargo_test_support::paths::CargoPathExt;
55
use cargo_test_support::registry::Package;
66
use cargo_test_support::{basic_manifest, cross_compile, is_coarse_mtime, project};
@@ -3038,25 +3038,9 @@ fn generate_good_d_files() {
30383038

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

3041-
#[cfg(windows)]
3042-
assert!(
3043-
lines_match(
3044-
"[..]\\target\\debug\\meow.exe: [..]\\awoo\\barkbarkbark [..]\\awoo\\build.rs[..]",
3045-
&dot_d
3046-
) || lines_match(
3047-
"[..]\\target\\debug\\meow.exe: [..]\\awoo\\build.rs [..]\\awoo\\barkbarkbark[..]",
3048-
&dot_d
3049-
)
3050-
);
3051-
#[cfg(not(windows))]
3052-
assert!(
3053-
lines_match(
3054-
"[..]/target/debug/meow: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]",
3055-
&dot_d
3056-
) || lines_match(
3057-
"[..]/target/debug/meow: [..]/awoo/build.rs [..]/awoo/barkbarkbark[..]",
3058-
&dot_d
3059-
)
3041+
assert_match_exact(
3042+
"[..]/target/debug/meow[EXE]: [..]/awoo/barkbarkbark [..]/awoo/build.rs[..]",
3043+
&dot_d,
30603044
);
30613045

30623046
// paths relative to dependency roots should not be allowed
@@ -3077,25 +3061,9 @@ fn generate_good_d_files() {
30773061

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

3080-
#[cfg(windows)]
3081-
assert!(
3082-
lines_match(
3083-
"target\\debug\\meow.exe: [..]awoo\\barkbarkbark [..]awoo\\build.rs[..]",
3084-
&dot_d
3085-
) || lines_match(
3086-
"target\\debug\\meow.exe: [..]awoo\\build.rs [..]awoo\\barkbarkbark[..]",
3087-
&dot_d
3088-
)
3089-
);
3090-
#[cfg(not(windows))]
3091-
assert!(
3092-
lines_match(
3093-
"target/debug/meow: [..]awoo/barkbarkbark [..]awoo/build.rs[..]",
3094-
&dot_d
3095-
) || lines_match(
3096-
"target/debug/meow: [..]awoo/build.rs [..]awoo/barkbarkbark[..]",
3097-
&dot_d
3098-
)
3064+
assert_match_exact(
3065+
"target/debug/meow[EXE]: awoo/barkbarkbark awoo/build.rs[..]",
3066+
&dot_d,
30993067
);
31003068

31013069
// paths relative to dependency roots should not be allowed

tests/testsuite/features_namespaced.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,8 @@ fn publish_no_implicit() {
11511151
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
11521152
&[(
11531153
"Cargo.toml",
1154-
r#"[..]
1154+
&format!(
1155+
r#"{}
11551156
[package]
11561157
name = "foo"
11571158
version = "0.1.0"
@@ -1169,6 +1170,8 @@ optional = true
11691170
[features]
11701171
feat = ["opt-dep1"]
11711172
"#,
1173+
cargo::core::package::MANIFEST_PREAMBLE
1174+
),
11721175
)],
11731176
);
11741177
}
@@ -1255,7 +1258,8 @@ fn publish() {
12551258
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
12561259
&[(
12571260
"Cargo.toml",
1258-
r#"[..]
1261+
&format!(
1262+
r#"{}
12591263
[package]
12601264
name = "foo"
12611265
version = "0.1.0"
@@ -1271,6 +1275,8 @@ feat1 = []
12711275
feat2 = ["dep:bar"]
12721276
feat3 = ["feat2"]
12731277
"#,
1278+
cargo::core::package::MANIFEST_PREAMBLE
1279+
),
12741280
)],
12751281
);
12761282
}

tests/testsuite/lockfile_compat.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tests for supporting older versions of the Cargo.lock file format.
22
3-
use cargo_test_support::compare::lines_match;
3+
use cargo_test_support::compare::assert_match_exact;
44
use cargo_test_support::git;
55
use cargo_test_support::registry::Package;
66
use cargo_test_support::{basic_lib_manifest, basic_manifest, project};
@@ -13,15 +13,6 @@ fn oldest_lockfile_still_works() {
1313
}
1414
}
1515

16-
#[track_caller]
17-
fn assert_lockfiles_eq(expected: &str, actual: &str) {
18-
for (l, r) in expected.lines().zip(actual.lines()) {
19-
assert!(lines_match(l, r), "Lines differ:\n{}\n\n{}", l, r);
20-
}
21-
22-
assert_eq!(expected.lines().count(), actual.lines().count());
23-
}
24-
2516
fn oldest_lockfile_still_works_with_command(cargo_command: &str) {
2617
Package::new("bar", "0.1.0").publish();
2718

@@ -77,7 +68,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
7768
p.cargo(cargo_command).run();
7869

7970
let lock = p.read_lockfile();
80-
assert_lockfiles_eq(expected_lockfile, &lock);
71+
assert_match_exact(expected_lockfile, &lock);
8172
}
8273

8374
#[cargo_test]
@@ -123,7 +114,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
123114
p.cargo("build --locked").run();
124115

125116
let lock = p.read_lockfile();
126-
assert_lockfiles_eq(&old_lockfile, &lock);
117+
assert_match_exact(&old_lockfile, &lock);
127118
}
128119

129120
#[cargo_test]
@@ -170,7 +161,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
170161
p.cargo("build").run();
171162

172163
let lock = p.read_lockfile();
173-
assert_lockfiles_eq(
164+
assert_match_exact(
174165
r#"# This file is automatically @generated by Cargo.
175166
# It is not intended for manual editing.
176167
version = 3
@@ -428,7 +419,7 @@ dependencies = [
428419
\"bar\",
429420
]
430421
";
431-
assert_lockfiles_eq(expected, &actual);
422+
assert_match_exact(expected, &actual);
432423
}
433424

434425
#[cargo_test]
@@ -472,7 +463,7 @@ dependencies = [
472463
p.cargo("build").run();
473464

474465
let lock = p.read_lockfile();
475-
assert_lockfiles_eq(
466+
assert_match_exact(
476467
r#"# [..]
477468
# [..]
478469
version = 3
@@ -569,7 +560,7 @@ dependencies = [
569560
p.cargo("fetch").run();
570561

571562
let lock = p.read_lockfile();
572-
assert_lockfiles_eq(&lockfile, &lock);
563+
assert_match_exact(&lockfile, &lock);
573564
}
574565

575566
#[cargo_test]
@@ -640,7 +631,7 @@ dependencies = [
640631
p.cargo("fetch").run();
641632

642633
let lock = p.read_lockfile();
643-
assert_lockfiles_eq(&lockfile, &lock);
634+
assert_match_exact(&lockfile, &lock);
644635
}
645636

646637
#[cargo_test]
@@ -696,7 +687,7 @@ dependencies = [
696687
p.cargo("fetch").run();
697688

698689
let lock = p.read_lockfile();
699-
assert_lockfiles_eq(&lockfile, &lock);
690+
assert_match_exact(&lockfile, &lock);
700691
}
701692

702693
#[cargo_test]

tests/testsuite/publish.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,22 +1213,41 @@ fn publish_git_with_version() {
12131213
(
12141214
"Cargo.toml",
12151215
// Check that only `version` is included in Cargo.toml.
1216-
"[..]\n\
1217-
[dependencies.dep1]\n\
1218-
version = \"1.0\"\n\
1219-
",
1216+
&format!(
1217+
"{}\n\
1218+
[package]\n\
1219+
edition = \"2018\"\n\
1220+
name = \"foo\"\n\
1221+
version = \"0.1.0\"\n\
1222+
authors = []\n\
1223+
description = \"foo\"\n\
1224+
license = \"MIT\"\n\
1225+
[dependencies.dep1]\n\
1226+
version = \"1.0\"\n\
1227+
",
1228+
cargo::core::package::MANIFEST_PREAMBLE
1229+
),
12201230
),
12211231
(
12221232
"Cargo.lock",
12231233
// The important check here is that it is 1.0.1 in the registry.
1224-
"[..]\n\
1234+
"# This file is automatically @generated by Cargo.\n\
1235+
# It is not intended for manual editing.\n\
1236+
version = 3\n\
1237+
\n\
1238+
[[package]]\n\
1239+
name = \"dep1\"\n\
1240+
version = \"1.0.1\"\n\
1241+
source = \"registry+https://github.com/rust-lang/crates.io-index\"\n\
1242+
checksum = \"[..]\"\n\
1243+
\n\
12251244
[[package]]\n\
12261245
name = \"foo\"\n\
12271246
version = \"0.1.0\"\n\
12281247
dependencies = [\n\
12291248
\x20\"dep1\",\n\
12301249
]\n\
1231-
[..]",
1250+
",
12321251
),
12331252
],
12341253
);
@@ -1297,7 +1316,8 @@ fn publish_dev_dep_no_version() {
12971316
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
12981317
&[(
12991318
"Cargo.toml",
1300-
r#"[..]
1319+
&format!(
1320+
r#"{}
13011321
[package]
13021322
name = "foo"
13031323
version = "0.1.0"
@@ -1310,6 +1330,8 @@ repository = "foo"
13101330
13111331
[dev-dependencies]
13121332
"#,
1333+
cargo::core::package::MANIFEST_PREAMBLE
1334+
),
13131335
)],
13141336
);
13151337
}

tests/testsuite/weak_dep_features.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,8 @@ fn publish() {
702702
&["Cargo.toml", "Cargo.toml.orig", "src/lib.rs"],
703703
&[(
704704
"Cargo.toml",
705-
r#"[..]
705+
&format!(
706+
r#"{}
706707
[package]
707708
name = "foo"
708709
version = "0.1.0"
@@ -717,6 +718,8 @@ optional = true
717718
feat1 = []
718719
feat2 = ["bar?/feat"]
719720
"#,
721+
cargo::core::package::MANIFEST_PREAMBLE
722+
),
720723
)],
721724
);
722725
}

0 commit comments

Comments
 (0)