Skip to content

Commit a3c7a16

Browse files
committed
make cargo test work for bootstrap self test
1 parent d1ed52b commit a3c7a16

File tree

3 files changed

+17
-73
lines changed

3 files changed

+17
-73
lines changed

src/bootstrap/src/core/config/config.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,9 @@ impl Config {
626626
let llvm_assertions = llvm_assertions.unwrap_or(false);
627627
let mut target_config = HashMap::new();
628628
let mut channel = "dev".to_string();
629-
let out = flags_build_dir.or(build_build_dir.map(PathBuf::from)).unwrap_or_else(|| {
630-
if cfg!(test) {
631-
// Use the build directory of the original x.py invocation, so that we can set `initial_rustc` properly.
632-
Path::new(
633-
&env::var_os("CARGO_TARGET_DIR").expect("cargo test directly is not supported"),
634-
)
635-
.parent()
636-
.unwrap()
637-
.to_path_buf()
638-
} else {
639-
PathBuf::from("build")
640-
}
641-
});
629+
let out = flags_build_dir
630+
.or(build_build_dir.map(PathBuf::from))
631+
.unwrap_or_else(|| PathBuf::from("build"));
642632

643633
// NOTE: Bootstrap spawns various commands with different working directories.
644634
// To avoid writing to random places on the file system, `config.out` needs to be an absolute path.
@@ -689,8 +679,14 @@ impl Config {
689679
};
690680

691681
let initial_rustc = build_rustc.unwrap_or_else(|| {
682+
let out = if cfg!(test) { Path::new("../../build").to_path_buf() } else { out.clone() };
683+
692684
download_beta_toolchain(&dwn_ctx, &out);
693-
out.join(host_target).join("stage0").join("bin").join(exe("rustc", host_target))
685+
686+
out.join(if cfg!(test) { get_host_target() } else { host_target })
687+
.join("stage0")
688+
.join("bin")
689+
.join(exe("rustc", host_target))
694690
});
695691

696692
let initial_sysroot = t!(PathBuf::from_str(

src/bootstrap/src/core/config/tests.rs

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ fn prepare_test_specific_dir() -> PathBuf {
3939
// Replace "::" with "_" to make it safe for directory names on Windows systems
4040
let test_path = current.name().unwrap().replace("::", "_");
4141

42-
let testdir = parse("").tempdir().join(test_path);
42+
let testdir = crate::utils::tests::TestCtx::new()
43+
.config("check")
44+
.create_config()
45+
.tempdir()
46+
.join(test_path);
4347

4448
// clean up any old test files
4549
let _ = fs::remove_dir_all(&testdir);
@@ -64,62 +68,6 @@ fn download_ci_llvm() {
6468
}
6569
}
6670

67-
// FIXME(onur-ozkan): extend scope of the test
68-
// refs:
69-
// - https://github.com/rust-lang/rust/issues/109120
70-
// - https://github.com/rust-lang/rust/pull/109162#issuecomment-1496782487
71-
#[test]
72-
fn detect_src_and_out() {
73-
fn test(cfg: Config, build_dir: Option<&str>) {
74-
// This will bring absolute form of `src/bootstrap` path
75-
let current_dir = std::env::current_dir().unwrap();
76-
77-
// get `src` by moving into project root path
78-
let expected_src = current_dir.ancestors().nth(2).unwrap();
79-
assert_eq!(&cfg.src, expected_src);
80-
81-
// Sanity check for `src`
82-
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
83-
let expected_src = manifest_dir.ancestors().nth(2).unwrap();
84-
assert_eq!(&cfg.src, expected_src);
85-
86-
// test if build-dir was manually given in bootstrap.toml
87-
if let Some(custom_build_dir) = build_dir {
88-
assert_eq!(&cfg.out, Path::new(custom_build_dir));
89-
}
90-
// test the native bootstrap way
91-
else {
92-
// This should bring output path of bootstrap in absolute form
93-
let cargo_target_dir = env::var_os("CARGO_TARGET_DIR").expect(
94-
"CARGO_TARGET_DIR must been provided for the test environment from bootstrap",
95-
);
96-
97-
// Move to `build` from `build/bootstrap`
98-
let expected_out = Path::new(&cargo_target_dir).parent().unwrap();
99-
assert_eq!(&cfg.out, expected_out);
100-
101-
let args: Vec<String> = env::args().collect();
102-
103-
// Another test for `out` as a sanity check
104-
//
105-
// This will bring something similar to:
106-
// `{build-dir}/bootstrap/debug/deps/bootstrap-c7ee91d5661e2804`
107-
// `{build-dir}` can be anywhere, not just in the rust project directory.
108-
let dep = Path::new(args.first().unwrap());
109-
let expected_out = dep.ancestors().nth(5).unwrap();
110-
111-
assert_eq!(&cfg.out, expected_out);
112-
}
113-
}
114-
115-
test(parse(""), None);
116-
117-
{
118-
let build_dir = if cfg!(windows) { "C:\\tmp" } else { "/tmp" };
119-
test(parse(&format!("build.build-dir = '{build_dir}'")), Some(build_dir));
120-
}
121-
}
122-
12371
#[test]
12472
fn clap_verify() {
12573
Flags::command().debug_assert();

src/bootstrap/src/utils/helpers/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::utils::helpers::{
66
check_cfg_arg, extract_beta_rev, hex_encode, make, set_file_times, submodule_path_of,
77
symlink_dir,
88
};
9+
use crate::utils::tests::TestCtx;
910
use crate::{Config, Flags};
1011

1112
#[test]
@@ -80,8 +81,7 @@ fn test_symlink_dir() {
8081

8182
#[test]
8283
fn test_set_file_times_sanity_check() {
83-
let config =
84-
Config::parse(Flags::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()]));
84+
let config = TestCtx::new().config("check").create_config();
8585
let tempfile = config.tempdir().join(".tmp-file");
8686

8787
{

0 commit comments

Comments
 (0)