|
1 | 1 | //! Tests for `-Ztrim-paths`.
|
2 | 2 |
|
| 3 | +use cargo_test_support::basic_manifest; |
| 4 | +use cargo_test_support::git; |
| 5 | +use cargo_test_support::paths; |
3 | 6 | use cargo_test_support::project;
|
| 7 | +use cargo_test_support::registry::Package; |
4 | 8 |
|
5 | 9 | #[cargo_test]
|
6 | 10 | fn gated_manifest() {
|
@@ -57,3 +61,380 @@ Caused by:
|
57 | 61 | )
|
58 | 62 | .run();
|
59 | 63 | }
|
| 64 | + |
| 65 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 66 | +fn release_profile_default_to_object() { |
| 67 | + let p = project() |
| 68 | + .file( |
| 69 | + "Cargo.toml", |
| 70 | + r#" |
| 71 | + [package] |
| 72 | + name = "foo" |
| 73 | + version = "0.0.1" |
| 74 | + "#, |
| 75 | + ) |
| 76 | + .file("src/lib.rs", "") |
| 77 | + .build(); |
| 78 | + |
| 79 | + p.cargo("build --release --verbose -Ztrim-paths") |
| 80 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 81 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 82 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 83 | + .run(); |
| 84 | +} |
| 85 | + |
| 86 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 87 | +fn one_option() { |
| 88 | + let build = |option| { |
| 89 | + let p = project() |
| 90 | + .file( |
| 91 | + "Cargo.toml", |
| 92 | + &format!( |
| 93 | + r#" |
| 94 | + [package] |
| 95 | + name = "foo" |
| 96 | + version = "0.0.1" |
| 97 | +
|
| 98 | + [profile.dev] |
| 99 | + trim-paths = "{option}" |
| 100 | + "# |
| 101 | + ), |
| 102 | + ) |
| 103 | + .file("src/lib.rs", "") |
| 104 | + .build(); |
| 105 | + |
| 106 | + p.cargo("build -v -Ztrim-paths") |
| 107 | + }; |
| 108 | + |
| 109 | + for option in ["macro", "diagnostics", "object", "all"] { |
| 110 | + build(option) |
| 111 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 112 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 113 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 114 | + .run(); |
| 115 | + } |
| 116 | + build("none") |
| 117 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 118 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 119 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 120 | + .run(); |
| 121 | +} |
| 122 | + |
| 123 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 124 | +fn multiple_options() { |
| 125 | + let p = project() |
| 126 | + .file( |
| 127 | + "Cargo.toml", |
| 128 | + r#" |
| 129 | + [package] |
| 130 | + name = "foo" |
| 131 | + version = "0.0.1" |
| 132 | +
|
| 133 | + [profile.dev] |
| 134 | + trim-paths = ["diagnostics", "macro", "object"] |
| 135 | + "#, |
| 136 | + ) |
| 137 | + .file("src/lib.rs", "") |
| 138 | + .build(); |
| 139 | + |
| 140 | + p.cargo("build --verbose -Ztrim-paths") |
| 141 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 142 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 143 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 144 | + .run(); |
| 145 | +} |
| 146 | + |
| 147 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 148 | +fn multiple_options_with_none() { |
| 149 | + let p = project() |
| 150 | + .file( |
| 151 | + "Cargo.toml", |
| 152 | + r#" |
| 153 | + [package] |
| 154 | + name = "foo" |
| 155 | + version = "0.0.1" |
| 156 | +
|
| 157 | + [profile.dev] |
| 158 | + trim-paths = ["diagnostics", "macro", "object", "all", "none"] |
| 159 | + "#, |
| 160 | + ) |
| 161 | + .file("src/lib.rs", "") |
| 162 | + .build(); |
| 163 | + |
| 164 | + p.cargo("build --verbose -Ztrim-paths") |
| 165 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 166 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 167 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 168 | + .run(); |
| 169 | +} |
| 170 | + |
| 171 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 172 | +fn registry_dependency() { |
| 173 | + Package::new("bar", "0.0.1") |
| 174 | + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) |
| 175 | + .file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#) |
| 176 | + .publish(); |
| 177 | + let p = project() |
| 178 | + .file( |
| 179 | + "Cargo.toml", |
| 180 | + r#" |
| 181 | + [package] |
| 182 | + name = "foo" |
| 183 | + version = "0.0.1" |
| 184 | +
|
| 185 | + [dependencies] |
| 186 | + bar = "0.0.1" |
| 187 | +
|
| 188 | + [profile.dev] |
| 189 | + trim-paths = "object" |
| 190 | + "#, |
| 191 | + ) |
| 192 | + .file("src/main.rs", "fn main() { bar::f(); }") |
| 193 | + .build(); |
| 194 | + |
| 195 | + let registry_src = paths::home().join(".cargo/registry/src"); |
| 196 | + let registry_src = registry_src.display(); |
| 197 | + |
| 198 | + p.cargo("run --verbose -Ztrim-paths") |
| 199 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 200 | + .with_stdout(format!("{registry_src}/[..]/bar-0.0.1/src/lib.rs")) |
| 201 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 202 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 203 | + .run(); |
| 204 | +} |
| 205 | + |
| 206 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 207 | +fn git_dependency() { |
| 208 | + let git_project = git::new("bar", |project| { |
| 209 | + project |
| 210 | + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) |
| 211 | + .file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#) |
| 212 | + }); |
| 213 | + let url = git_project.url(); |
| 214 | + |
| 215 | + let p = project() |
| 216 | + .file( |
| 217 | + "Cargo.toml", |
| 218 | + &format!( |
| 219 | + r#" |
| 220 | + [package] |
| 221 | + name = "foo" |
| 222 | + version = "0.0.1" |
| 223 | +
|
| 224 | + [dependencies] |
| 225 | + bar = {{ git = "{url}" }} |
| 226 | +
|
| 227 | + [profile.dev] |
| 228 | + trim-paths = "object" |
| 229 | + "# |
| 230 | + ), |
| 231 | + ) |
| 232 | + .file("src/main.rs", "fn main() { bar::f(); }") |
| 233 | + .build(); |
| 234 | + |
| 235 | + let git_checkouts_src = paths::home().join(".cargo/git/checkouts"); |
| 236 | + let git_checkouts_src = git_checkouts_src.display(); |
| 237 | + |
| 238 | + p.cargo("run --verbose -Ztrim-paths") |
| 239 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 240 | + .with_stdout(format!("{git_checkouts_src}/bar-[..]/[..]/src/lib.rs")) |
| 241 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 242 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 243 | + .run(); |
| 244 | +} |
| 245 | + |
| 246 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 247 | +fn path_dependency() { |
| 248 | + let p = project() |
| 249 | + .file( |
| 250 | + "Cargo.toml", |
| 251 | + r#" |
| 252 | + [package] |
| 253 | + name = "foo" |
| 254 | + version = "0.0.1" |
| 255 | +
|
| 256 | + [dependencies] |
| 257 | + bar = { path = "cocktail-bar" } |
| 258 | +
|
| 259 | + [profile.dev] |
| 260 | + trim-paths = "object" |
| 261 | + "#, |
| 262 | + ) |
| 263 | + .file("src/main.rs", "fn main() { bar::f(); }") |
| 264 | + .file("cocktail-bar/Cargo.toml", &basic_manifest("bar", "0.0.1")) |
| 265 | + .file( |
| 266 | + "cocktail-bar/src/lib.rs", |
| 267 | + r#"pub fn f() { println!("{}", file!()); }"#, |
| 268 | + ) |
| 269 | + .build(); |
| 270 | + |
| 271 | + p.cargo("run --verbose -Ztrim-paths") |
| 272 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 273 | + .with_stdout("cocktail-bar/src/lib.rs") |
| 274 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 275 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 276 | + .run(); |
| 277 | +} |
| 278 | + |
| 279 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 280 | +fn path_dependency_outside_workspace() { |
| 281 | + let bar = project() |
| 282 | + .at("bar") |
| 283 | + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) |
| 284 | + .file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#) |
| 285 | + .build(); |
| 286 | + let bar_path = bar.url().to_file_path().unwrap(); |
| 287 | + let bar_path = bar_path.display(); |
| 288 | + |
| 289 | + let p = project() |
| 290 | + .file( |
| 291 | + "Cargo.toml", |
| 292 | + r#" |
| 293 | + [package] |
| 294 | + name = "foo" |
| 295 | + version = "0.0.1" |
| 296 | +
|
| 297 | + [dependencies] |
| 298 | + bar = { path = "../bar" } |
| 299 | +
|
| 300 | + [profile.dev] |
| 301 | + trim-paths = "object" |
| 302 | + "#, |
| 303 | + ) |
| 304 | + .file("src/main.rs", "fn main() { bar::f(); }") |
| 305 | + .build(); |
| 306 | + |
| 307 | + p.cargo("run --verbose -Ztrim-paths") |
| 308 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 309 | + .with_stdout(format!("{bar_path}/src/lib.rs")) |
| 310 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 311 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 312 | + .run(); |
| 313 | +} |
| 314 | + |
| 315 | +#[cargo_test(nightly, reason = "-Zremap-path-scope is unstable")] |
| 316 | +fn diagnostics_works() { |
| 317 | + Package::new("bar", "0.0.1") |
| 318 | + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) |
| 319 | + .file("src/lib.rs", r#"pub fn f() { let unused = 0; }"#) |
| 320 | + .publish(); |
| 321 | + let p = project() |
| 322 | + .file( |
| 323 | + "Cargo.toml", |
| 324 | + r#" |
| 325 | + [package] |
| 326 | + name = "foo" |
| 327 | + version = "0.0.1" |
| 328 | +
|
| 329 | + [dependencies] |
| 330 | + bar = "0.0.1" |
| 331 | +
|
| 332 | + [profile.dev] |
| 333 | + trim-paths = "diagnostics" |
| 334 | + "#, |
| 335 | + ) |
| 336 | + .file("src/lib.rs", "") |
| 337 | + .build(); |
| 338 | + |
| 339 | + let registry_src = paths::home().join(".cargo/registry/src"); |
| 340 | + let registry_src = registry_src.display(); |
| 341 | + |
| 342 | + p.cargo("build -vv -Ztrim-paths") |
| 343 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 344 | + .with_stderr_contains(format!( |
| 345 | + "\ |
| 346 | +warning: unused variable: `unused` |
| 347 | + --> {registry_src}/[..]/bar-0.0.1/src/lib.rs:1:18 |
| 348 | + | |
| 349 | +1 | pub fn f() {{ let unused = 0; }} |
| 350 | + | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_unused` |
| 351 | + | |
| 352 | + = note: `#[warn(unused_variables)]` on by default", |
| 353 | + )) |
| 354 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 355 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 356 | + .run(); |
| 357 | +} |
| 358 | + |
| 359 | +#[cfg(target_os = "linux")] |
| 360 | +#[cargo_test(requires_readelf, nightly, reason = "-Zremap-path-scope is unstable")] |
| 361 | +fn object_works() { |
| 362 | + use std::os::unix::ffi::OsStrExt; |
| 363 | + |
| 364 | + let run_readelf = |path| { |
| 365 | + std::process::Command::new("readelf") |
| 366 | + .arg("-wi") |
| 367 | + .arg(path) |
| 368 | + .output() |
| 369 | + .expect("readelf works") |
| 370 | + }; |
| 371 | + |
| 372 | + let registry_src = paths::home().join(".cargo/registry/src"); |
| 373 | + let registry_src_bytes = registry_src.as_os_str().as_bytes(); |
| 374 | + |
| 375 | + Package::new("bar", "0.0.1") |
| 376 | + .file("Cargo.toml", &basic_manifest("bar", "0.0.1")) |
| 377 | + .file("src/lib.rs", r#"pub fn f() { println!("{}", file!()); }"#) |
| 378 | + .publish(); |
| 379 | + |
| 380 | + let p = project() |
| 381 | + .file( |
| 382 | + "Cargo.toml", |
| 383 | + r#" |
| 384 | + [package] |
| 385 | + name = "foo" |
| 386 | + version = "0.0.1" |
| 387 | +
|
| 388 | + [dependencies] |
| 389 | + bar = "0.0.1" |
| 390 | + "#, |
| 391 | + ) |
| 392 | + .file("src/main.rs", "fn main() { bar::f(); }") |
| 393 | + .build(); |
| 394 | + |
| 395 | + let pkg_root = p.root(); |
| 396 | + let pkg_root = pkg_root.as_os_str().as_bytes(); |
| 397 | + |
| 398 | + p.cargo("build").run(); |
| 399 | + |
| 400 | + let bin_path = p.bin("foo"); |
| 401 | + assert!(bin_path.is_file()); |
| 402 | + let stdout = run_readelf(bin_path).stdout; |
| 403 | + // TODO: re-enable this check when rustc bootstrap disables remapping |
| 404 | + // <https://github.com/rust-lang/cargo/pull/12625#discussion_r1371714791> |
| 405 | + // assert!(memchr::memmem::find(&stdout, rust_src).is_some()); |
| 406 | + assert!(memchr::memmem::find(&stdout, registry_src_bytes).is_some()); |
| 407 | + assert!(memchr::memmem::find(&stdout, pkg_root).is_some()); |
| 408 | + |
| 409 | + p.cargo("clean").run(); |
| 410 | + |
| 411 | + p.change_file( |
| 412 | + "Cargo.toml", |
| 413 | + r#" |
| 414 | + [package] |
| 415 | + name = "foo" |
| 416 | + version = "0.0.1" |
| 417 | +
|
| 418 | + [dependencies] |
| 419 | + bar = "0.0.1" |
| 420 | +
|
| 421 | + [profile.dev] |
| 422 | + trim-paths = "object" |
| 423 | + "#, |
| 424 | + ); |
| 425 | + |
| 426 | + p.cargo("build --verbose -Ztrim-paths") |
| 427 | + .masquerade_as_nightly_cargo(&["-Ztrim-paths"]) |
| 428 | + .with_stderr_does_not_contain("[..]-Zremap-path-scope=[..]") |
| 429 | + .with_stderr_does_not_contain("[..]--remap-path-prefix=[..]") |
| 430 | + .run(); |
| 431 | + |
| 432 | + let bin_path = p.bin("foo"); |
| 433 | + assert!(bin_path.is_file()); |
| 434 | + let stdout = run_readelf(bin_path).stdout; |
| 435 | + // TODO: re-enable this check when rustc bootstrap disables remapping |
| 436 | + // <https://github.com/rust-lang/cargo/pull/12625#discussion_r1371714791> |
| 437 | + // assert!(memchr::memmem::find(&stdout, rust_src).is_some()); |
| 438 | + assert!(memchr::memmem::find(&stdout, registry_src_bytes).is_some()); |
| 439 | + assert!(memchr::memmem::find(&stdout, pkg_root).is_some()); |
| 440 | +} |
0 commit comments