Skip to content

Commit d0a3ec7

Browse files
committed
Added build_dir templating tests.
This is in preparation for adding templating suppport to the `build.build-dir` configuration option.
1 parent ca694a8 commit d0a3ec7

File tree

1 file changed

+148
-1
lines changed

1 file changed

+148
-1
lines changed

tests/testsuite/build_dir.rs

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use std::path::PathBuf;
1313

1414
use cargo_test_support::prelude::*;
15-
use cargo_test_support::project;
15+
use cargo_test_support::{paths, project};
1616
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX, EXE_SUFFIX};
1717

1818
#[cargo_test]
@@ -491,6 +491,153 @@ fn future_incompat_should_output_to_build_dir() {
491491
assert_exists(&p.root().join("build-dir/.future-incompat-report.json"));
492492
}
493493

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

0 commit comments

Comments
 (0)