|
12 | 12 | use std::path::PathBuf;
|
13 | 13 |
|
14 | 14 | use cargo_test_support::prelude::*;
|
15 |
| -use cargo_test_support::project; |
| 15 | +use cargo_test_support::{paths, project, str}; |
16 | 16 | use std::env::consts::{DLL_PREFIX, DLL_SUFFIX, EXE_SUFFIX};
|
17 | 17 |
|
18 | 18 | #[cargo_test]
|
@@ -491,6 +491,170 @@ fn future_incompat_should_output_to_build_dir() {
|
491 | 491 | assert_exists(&p.root().join("build-dir/.future-incompat-report.json"));
|
492 | 492 | }
|
493 | 493 |
|
| 494 | +#[cargo_test] |
| 495 | +fn template_should_error_for_invalid_variables() { |
| 496 | + let p = project() |
| 497 | + .file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#) |
| 498 | + .file( |
| 499 | + ".cargo/config.toml", |
| 500 | + r#" |
| 501 | + [build] |
| 502 | + build-dir = "{fake}/build-dir" |
| 503 | + target-dir = "target-dir" |
| 504 | + "#, |
| 505 | + ) |
| 506 | + .build(); |
| 507 | + |
| 508 | + p.cargo("build -Z build-dir") |
| 509 | + .masquerade_as_nightly_cargo(&["build-dir"]) |
| 510 | + .enable_mac_dsym() |
| 511 | + .with_status(101) |
| 512 | + .with_stderr_data(str![[r#" |
| 513 | +[ERROR] unexpected variable `fake` in build.build-dir path `{fake}/build-dir` |
| 514 | +
|
| 515 | +"#]]) |
| 516 | + .run(); |
| 517 | +} |
| 518 | + |
| 519 | +#[cargo_test] |
| 520 | +fn template_workspace_root() { |
| 521 | + let p = project() |
| 522 | + .file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#) |
| 523 | + .file( |
| 524 | + ".cargo/config.toml", |
| 525 | + r#" |
| 526 | + [build] |
| 527 | + build-dir = "{workspace-root}/build-dir" |
| 528 | + target-dir = "target-dir" |
| 529 | + "#, |
| 530 | + ) |
| 531 | + .build(); |
| 532 | + |
| 533 | + p.cargo("build -Z build-dir") |
| 534 | + .masquerade_as_nightly_cargo(&["build-dir"]) |
| 535 | + .enable_mac_dsym() |
| 536 | + .run(); |
| 537 | + |
| 538 | + assert_build_dir_layout(p.root().join("build-dir"), "debug"); |
| 539 | + assert_artifact_dir_layout(p.root().join("target-dir"), "debug"); |
| 540 | + |
| 541 | + // Verify the binary was uplifted to the target-dir |
| 542 | + assert_exists(&p.root().join(&format!("target-dir/debug/foo{EXE_SUFFIX}"))); |
| 543 | +} |
| 544 | + |
| 545 | +#[cargo_test] |
| 546 | +fn template_cargo_cache_home() { |
| 547 | + let p = project() |
| 548 | + .file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#) |
| 549 | + .file( |
| 550 | + ".cargo/config.toml", |
| 551 | + r#" |
| 552 | + [build] |
| 553 | + build-dir = "{cargo-cache-home}/build-dir" |
| 554 | + target-dir = "target-dir" |
| 555 | + "#, |
| 556 | + ) |
| 557 | + .build(); |
| 558 | + |
| 559 | + p.cargo("build -Z build-dir") |
| 560 | + .masquerade_as_nightly_cargo(&["build-dir"]) |
| 561 | + .enable_mac_dsym() |
| 562 | + .run(); |
| 563 | + |
| 564 | + assert_build_dir_layout(paths::home().join(".cargo/build-dir"), "debug"); |
| 565 | + assert_artifact_dir_layout(p.root().join("target-dir"), "debug"); |
| 566 | + |
| 567 | + // Verify the binary was uplifted to the target-dir |
| 568 | + assert_exists(&p.root().join(&format!("target-dir/debug/foo{EXE_SUFFIX}"))); |
| 569 | +} |
| 570 | + |
| 571 | +#[cargo_test] |
| 572 | +fn template_workspace_manfiest_path_hash() { |
| 573 | + let p = project() |
| 574 | + .file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#) |
| 575 | + .file( |
| 576 | + "Cargo.toml", |
| 577 | + r#" |
| 578 | + [package] |
| 579 | + name = "foo" |
| 580 | + version = "1.0.0" |
| 581 | + authors = [] |
| 582 | + edition = "2015" |
| 583 | + "#, |
| 584 | + ) |
| 585 | + .file( |
| 586 | + ".cargo/config.toml", |
| 587 | + r#" |
| 588 | + [build] |
| 589 | + build-dir = "foo/{workspace-manifest-path-hash}/build-dir" |
| 590 | + target-dir = "target-dir" |
| 591 | + "#, |
| 592 | + ) |
| 593 | + .build(); |
| 594 | + |
| 595 | + p.cargo("build -Z build-dir") |
| 596 | + .masquerade_as_nightly_cargo(&["build-dir"]) |
| 597 | + .enable_mac_dsym() |
| 598 | + .run(); |
| 599 | + |
| 600 | + let foo_dir = p.root().join("foo"); |
| 601 | + assert_exists(&foo_dir); |
| 602 | + let hash_dir = parse_workspace_manifest_path_hash(&foo_dir); |
| 603 | + |
| 604 | + let build_dir = hash_dir.as_path().join("build-dir"); |
| 605 | + assert_build_dir_layout(build_dir, "debug"); |
| 606 | + assert_artifact_dir_layout(p.root().join("target-dir"), "debug"); |
| 607 | + |
| 608 | + // Verify the binary was uplifted to the target-dir |
| 609 | + assert_exists(&p.root().join(&format!("target-dir/debug/foo{EXE_SUFFIX}"))); |
| 610 | +} |
| 611 | + |
| 612 | +fn parse_workspace_manifest_path_hash(hash_dir: &PathBuf) -> PathBuf { |
| 613 | + // Since the hash will change between test runs simply find the first directories and assume |
| 614 | + // that is the hash dir. The format is a 2 char directory followed by the remaining hash in the |
| 615 | + // inner directory (ie. `34/f9d02eb8411c05`) |
| 616 | + let mut dirs = std::fs::read_dir(hash_dir).unwrap().into_iter(); |
| 617 | + let outer_hash_dir = dirs.next().unwrap().unwrap(); |
| 618 | + // Validate there are no other directories in `hash_dir` |
| 619 | + assert!( |
| 620 | + dirs.next().is_none(), |
| 621 | + "Found multiple dir entries in {hash_dir:?}" |
| 622 | + ); |
| 623 | + // Validate the outer hash dir hash is a directory and has the correct hash length |
| 624 | + assert!( |
| 625 | + outer_hash_dir.path().is_dir(), |
| 626 | + "{outer_hash_dir:?} was not a directory" |
| 627 | + ); |
| 628 | + assert_eq!( |
| 629 | + outer_hash_dir.path().file_name().unwrap().len(), |
| 630 | + 2, |
| 631 | + "Path {:?} should have been 2 chars", |
| 632 | + outer_hash_dir.path().file_name() |
| 633 | + ); |
| 634 | + |
| 635 | + let mut dirs = std::fs::read_dir(outer_hash_dir.path()) |
| 636 | + .unwrap() |
| 637 | + .into_iter(); |
| 638 | + let inner_hash_dir = dirs.next().unwrap().unwrap(); |
| 639 | + // Validate there are no other directories in first hash dir |
| 640 | + assert!( |
| 641 | + dirs.next().is_none(), |
| 642 | + "Found multiple dir entries in {outer_hash_dir:?}" |
| 643 | + ); |
| 644 | + // Validate the outer hash dir hash is a directory and has the correct hash length |
| 645 | + assert!( |
| 646 | + inner_hash_dir.path().is_dir(), |
| 647 | + "{inner_hash_dir:?} was not a directory" |
| 648 | + ); |
| 649 | + assert_eq!( |
| 650 | + inner_hash_dir.path().file_name().unwrap().len(), |
| 651 | + 14, |
| 652 | + "Path {:?} should have been 2 chars", |
| 653 | + inner_hash_dir.path().file_name() |
| 654 | + ); |
| 655 | + return inner_hash_dir.path(); |
| 656 | +} |
| 657 | + |
494 | 658 | #[track_caller]
|
495 | 659 | fn assert_build_dir_layout(path: PathBuf, profile: &str) {
|
496 | 660 | assert_dir_layout(path, profile, true);
|
|
0 commit comments