Skip to content

Commit

Permalink
test(trim-paths): verify current behavior with -Ztrim-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Oct 30, 2023
1 parent fd893b2 commit 557fc4f
Showing 1 changed file with 349 additions and 0 deletions.
349 changes: 349 additions & 0 deletions tests/testsuite/profile_trim_paths.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! Tests for `-Ztrim-paths`.
use cargo_test_support::basic_manifest;
use cargo_test_support::git;
use cargo_test_support::paths;
use cargo_test_support::project;
use cargo_test_support::registry::Package;

#[cargo_test]
fn gated_manifest() {
Expand Down Expand Up @@ -57,3 +61,348 @@ Caused by:
)
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn release_profile_default_to_object() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("build --release --verbose -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn one_option() {
let build = |option| {
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.1"
[profile.dev]
trim-paths = "{option}"
"#
),
)
.file("src/lib.rs", "")
.build();

p.cargo("build -v -Ztrim-paths")
};

for option in ["macro", "diagnostics", "object", "all"] {
build(option)
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}
build("none")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn multiple_options() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[profile.dev]
trim-paths = ["diagnostics", "macro", "object"]
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("build --verbose -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn registry_dependency() {
Package::new("bar", "0.0.1")
.file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = "0.0.1"
[profile.dev]
trim-paths = "object"
"#,
)
.file("src/main.rs", "fn main() { bar::f(); }")
.build();

let registry_src = paths::home().join(".cargo/registry/src");
let registry_src = registry_src.display();

p.cargo("run --verbose -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stdout(format!("{registry_src}/[..]/bar-0.0.1/src/lib.rs"))
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn git_dependency() {
let git_project = git::new("bar", |project| {
project
.file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#)
});
let url = git_project.url();

let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = {{ git = "{url}" }}
[profile.dev]
trim-paths = "object"
"#
),
)
.file("src/main.rs", "fn main() { bar::f(); }")
.build();

let git_checkouts_src = paths::home().join(".cargo/git/checkouts");
let git_checkouts_src = git_checkouts_src.display();

p.cargo("run --verbose -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stdout(format!("{git_checkouts_src}/bar-[..]/[..]/src/lib.rs"))
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn path_dependency() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = { path = "cocktail-bar" }
[profile.dev]
trim-paths = "object"
"#,
)
.file("src/main.rs", "fn main() { bar::f(); }")
.file("cocktail-bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file(
"cocktail-bar/src/lib.rs",
r#"pub fn f() { println!("{}", file!()); }"#,
)
.build();

p.cargo("run --verbose -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stdout("cocktail-bar/src/lib.rs")
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn path_dependency_outside_workspace() {
let bar = project()
.at("bar")
.file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#)
.build();
let bar_path = bar.url().to_file_path().unwrap();
let bar_path = bar_path.display();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = { path = "../bar" }
[profile.dev]
trim-paths = "object"
"#,
)
.file("src/main.rs", "fn main() { bar::f(); }")
.build();

p.cargo("run --verbose -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stdout(format!("{bar_path}/src/lib.rs"))
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")]
fn diagnostics_works() {
Package::new("bar", "0.0.1")
.file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("src/lib.rs", r#"pub fn f() { let unused = 0; }"#)
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = "0.0.1"
[profile.dev]
trim-paths = "diagnostics"
"#,
)
.file("src/lib.rs", "")
.build();

let registry_src = paths::home().join(".cargo/registry/src");
let registry_src = registry_src.display();

p.cargo("build -vv -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stderr_contains(format!("[..]{registry_src}/[..]/bar-0.0.1/src/lib.rs:1[..]"))
.with_stderr_contains("[..]unused_variables[..]")
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();
}

#[cfg(target_os = "linux")]
#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")]
fn object_works() {
use std::os::unix::ffi::OsStrExt;

let run_readelf = |path| {
std::process::Command::new("readelf")
.arg("-wi")
.arg(path)
.output()
.expect("readelf works")
};

let registry_src = paths::home().join(".cargo/registry/src");
let registry_src_bytes = registry_src.as_os_str().as_bytes();

Package::new("bar", "0.0.1")
.file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
.file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#)
.publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = "0.0.1"
"#,
)
.file("src/main.rs", "fn main() { bar::f(); }")
.build();

let pkg_root = p.root();
let pkg_root = pkg_root.as_os_str().as_bytes();

p.cargo("build").run();

let bin_path = p.bin("foo");
assert!(bin_path.is_file());
let stdout = run_readelf(bin_path).stdout;
// TODO: re-enable this check when rustc bootstrap disables remapping
// <https://github.com/rust-lang/cargo/pull/12625#discussion_r1371714791>
// assert!(memchr::memmem::find(&stdout, rust_src).is_some());
assert!(memchr::memmem::find(&stdout, registry_src_bytes).is_some());
assert!(memchr::memmem::find(&stdout, pkg_root).is_some());

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

p.change_file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
[dependencies]
bar = "0.0.1"
[profile.dev]
trim-paths = "object"
"#,
);

p.cargo("build --verbose -Ztrim-paths")
.masquerade_as_nightly_cargo(&["-Ztrim-paths"])
.with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]")
.with_stderr_does_not_contain("[..]--remap-path-prefix=[..]")
.run();

let bin_path = p.bin("foo");
assert!(bin_path.is_file());
let stdout = run_readelf(bin_path).stdout;
// TODO: re-enable this check when rustc bootstrap disables remapping
// <https://github.com/rust-lang/cargo/pull/12625#discussion_r1371714791>
// assert!(memchr::memmem::find(&stdout, rust_src).is_some());
assert!(memchr::memmem::find(&stdout, registry_src_bytes).is_some());
assert!(memchr::memmem::find(&stdout, pkg_root).is_some());
}

0 comments on commit 557fc4f

Please sign in to comment.