Skip to content

Commit 0ce24c1

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 abe461c commit 0ce24c1

File tree

1 file changed

+132
-1
lines changed

1 file changed

+132
-1
lines changed

tests/testsuite/build_dir.rs

Lines changed: 132 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,137 @@ 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+
586+
// Since the hash will change between test runs simply find the first directories and assume
587+
// that is the hash dir. The format is a 2 char directory followed by the remaining hash in the
588+
// inner directory (ie. `34/f9d02eb8411c05`)
589+
let mut dirs = std::fs::read_dir(foo_dir).unwrap().into_iter();
590+
let outer_hash_dir = dirs.next().unwrap().unwrap();
591+
// Validate there are no other directories in `foo`
592+
assert!(dirs.next().is_none());
593+
// Validate the outer hash dir hash is a directory and has the correct hash length
594+
assert!(outer_hash_dir.path().is_dir());
595+
assert_eq!(
596+
outer_hash_dir.path().file_name().unwrap().len(),
597+
2,
598+
"Path {:?} should have been 2 chars",
599+
outer_hash_dir.path().file_name()
600+
);
601+
602+
let mut dirs = std::fs::read_dir(outer_hash_dir.path())
603+
.unwrap()
604+
.into_iter();
605+
let inner_hash_dir = dirs.next().unwrap().unwrap();
606+
// Validate there are no other directories in first hash dir
607+
assert!(dirs.next().is_none());
608+
// Validate the outer hash dir hash is a directory and has the correct hash length
609+
assert!(inner_hash_dir.path().is_dir());
610+
assert_eq!(
611+
inner_hash_dir.path().file_name().unwrap().len(),
612+
14,
613+
"Path {:?} should have been 2 chars",
614+
inner_hash_dir.path().file_name()
615+
);
616+
617+
let build_dir = inner_hash_dir.path().join("build-dir");
618+
assert_build_dir_layout(build_dir, "debug");
619+
assert_artifact_dir_layout(p.root().join("target-dir"), "debug");
620+
621+
// Verify the binary was uplifted to the target-dir
622+
assert_exists(&p.root().join(&format!("target-dir/debug/foo{EXE_SUFFIX}")));
623+
}
624+
494625
#[track_caller]
495626
fn assert_build_dir_layout(path: PathBuf, profile: &str) {
496627
assert_dir_layout(path, profile, true);

0 commit comments

Comments
 (0)