Skip to content

Commit fda267e

Browse files
committed
test(rustflags): Clarify remap tests
The tests were comparing rustc vs RUSTFLAGS which was obscuring the case it was trying to test which was that different remaps shouldn't cause different results.
1 parent 72b5c79 commit fda267e

File tree

1 file changed

+98
-5
lines changed

1 file changed

+98
-5
lines changed

tests/testsuite/rustflags.rs

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ fn remap_path_prefix_works() {
15211521
}
15221522

15231523
#[cargo_test]
1524-
fn remap_path_prefix_ignored() {
1524+
fn rustflags_remap_path_prefix_ignored_for_c_metadata() {
15251525
let p = project().file("src/lib.rs", "").build();
15261526

15271527
let build_output = p
@@ -1531,16 +1531,89 @@ fn remap_path_prefix_ignored() {
15311531
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
15321532
)
15331533
.run();
1534-
let build_c_metadata = dbg!(get_c_metadata(build_output));
1534+
let first_c_metadata = dbg!(get_c_metadata(build_output));
15351535

15361536
p.cargo("clean").run();
15371537

1538-
let rustc_output = p
1538+
let build_output = p
1539+
.cargo("build -v")
1540+
.env(
1541+
"RUSTFLAGS",
1542+
"--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo",
1543+
)
1544+
.run();
1545+
let second_c_metadata = dbg!(get_c_metadata(build_output));
1546+
1547+
assert_data_eq!(first_c_metadata, second_c_metadata);
1548+
}
1549+
1550+
#[cargo_test]
1551+
fn rustc_remap_path_prefix_ignored_for_c_metadata() {
1552+
let p = project().file("src/lib.rs", "").build();
1553+
1554+
let build_output = p
1555+
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
1556+
.run();
1557+
let first_c_metadata = dbg!(get_c_metadata(build_output));
1558+
1559+
p.cargo("clean").run();
1560+
1561+
let build_output = p
1562+
.cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo")
1563+
.run();
1564+
let second_c_metadata = dbg!(get_c_metadata(build_output));
1565+
1566+
assert_data_eq!(first_c_metadata, second_c_metadata);
1567+
}
1568+
1569+
// `--remap-path-prefix` is meant to take two different binaries and make them the same but the
1570+
// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change
1571+
#[cargo_test]
1572+
fn rustflags_remap_path_prefix_ignored_for_c_extra_filename() {
1573+
let p = project().file("src/lib.rs", "").build();
1574+
1575+
let build_output = p
1576+
.cargo("build -v")
1577+
.env(
1578+
"RUSTFLAGS",
1579+
"--remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo",
1580+
)
1581+
.run();
1582+
let first_c_extra_filename = dbg!(get_c_extra_filename(build_output));
1583+
1584+
p.cargo("clean").run();
1585+
1586+
let build_output = p
1587+
.cargo("build -v")
1588+
.env(
1589+
"RUSTFLAGS",
1590+
"--remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo",
1591+
)
1592+
.run();
1593+
let second_c_extra_filename = dbg!(get_c_extra_filename(build_output));
1594+
1595+
assert_data_eq!(first_c_extra_filename, second_c_extra_filename);
1596+
}
1597+
1598+
// `--remap-path-prefix` is meant to take two different binaries and make them the same but the
1599+
// rlib name, including `-Cextra-filename`, can still end up in the binary so it can't change
1600+
#[cargo_test]
1601+
fn rustc_remap_path_prefix_ignored_for_c_extra_filename() {
1602+
let p = project().file("src/lib.rs", "").build();
1603+
1604+
let build_output = p
15391605
.cargo("rustc -v -- --remap-path-prefix=/abc=/zoo --remap-path-prefix /spaced=/zoo")
15401606
.run();
1541-
let rustc_c_metadata = dbg!(get_c_metadata(rustc_output));
1607+
let first_c_extra_filename = dbg!(get_c_extra_filename(build_output));
1608+
1609+
p.cargo("clean").run();
1610+
1611+
let build_output = p
1612+
.cargo("rustc -v -- --remap-path-prefix=/def=/zoo --remap-path-prefix /earth=/zoo")
1613+
.run();
1614+
let second_c_extra_filename = dbg!(get_c_extra_filename(build_output));
15421615

1543-
assert_data_eq!(rustc_c_metadata, build_c_metadata);
1616+
assert_data_eq!(first_c_extra_filename, second_c_extra_filename);
15441617
}
15451618

15461619
fn get_c_metadata(output: RawOutput) -> String {
@@ -1563,6 +1636,26 @@ fn get_c_metadata(output: RawOutput) -> String {
15631636
c_metadata.join("\n")
15641637
}
15651638

1639+
fn get_c_extra_filename(output: RawOutput) -> String {
1640+
let get_c_extra_filename_re =
1641+
regex::Regex::new(r".* (--crate-name [^ ]+).* (-C ?extra-filename=[^ ]+).*").unwrap();
1642+
1643+
let stderr = String::from_utf8(output.stderr).unwrap();
1644+
let mut c_extra_filename = get_c_extra_filename_re
1645+
.captures_iter(&stderr)
1646+
.map(|c| {
1647+
let (_, [name, c_extra_filename]) = c.extract();
1648+
format!("{name} {c_extra_filename}")
1649+
})
1650+
.collect::<Vec<_>>();
1651+
assert!(
1652+
!c_extra_filename.is_empty(),
1653+
"`{get_c_extra_filename_re:?}` did not match:\n```\n{stderr}\n```"
1654+
);
1655+
c_extra_filename.sort();
1656+
c_extra_filename.join("\n")
1657+
}
1658+
15661659
#[cargo_test]
15671660
fn host_config_rustflags_with_target() {
15681661
// regression test for https://github.com/rust-lang/cargo/issues/10206

0 commit comments

Comments
 (0)