Skip to content

Commit 5fd30f8

Browse files
committed
Extract {prepare,run}_cargo_test into test_helpers module
1 parent c5e8f58 commit 5fd30f8

File tree

2 files changed

+126
-114
lines changed

2 files changed

+126
-114
lines changed

src/bootstrap/src/core/build_steps/test/mod.rs

Lines changed: 2 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::{env, fs, iter};
1313

1414
use clap_complete::shells;
1515

16+
use self::test_helpers::{prepare_cargo_test, run_cargo_test};
1617
use crate::core::build_steps::compile::run_cargo;
1718
use crate::core::build_steps::doc::DocumentationFormat;
1819
use crate::core::build_steps::llvm::get_llvm_version;
@@ -35,6 +36,7 @@ use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
3536
use crate::{CLang, DocTests, GitRepo, Mode, PathSet, envify};
3637

3738
mod book_tests;
39+
mod test_helpers;
3840
mod tidy;
3941

4042
pub(crate) use book_tests::{
@@ -2128,120 +2130,6 @@ impl Step for CrateLibrustc {
21282130
}
21292131
}
21302132

2131-
/// Given a `cargo test` subcommand, add the appropriate flags and run it.
2132-
///
2133-
/// Returns whether the test succeeded.
2134-
fn run_cargo_test<'a>(
2135-
cargo: builder::Cargo,
2136-
libtest_args: &[&str],
2137-
crates: &[String],
2138-
primary_crate: &str,
2139-
description: impl Into<Option<&'a str>>,
2140-
target: TargetSelection,
2141-
builder: &Builder<'_>,
2142-
) -> bool {
2143-
let compiler = cargo.compiler();
2144-
let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, primary_crate, target, builder);
2145-
let _time = helpers::timeit(builder);
2146-
let _group = description.into().and_then(|what| {
2147-
builder.msg_sysroot_tool(Kind::Test, compiler.stage, what, compiler.host, target)
2148-
});
2149-
2150-
#[cfg(feature = "build-metrics")]
2151-
builder.metrics.begin_test_suite(
2152-
build_helper::metrics::TestSuiteMetadata::CargoPackage {
2153-
crates: crates.iter().map(|c| c.to_string()).collect(),
2154-
target: target.triple.to_string(),
2155-
host: compiler.host.triple.to_string(),
2156-
stage: compiler.stage,
2157-
},
2158-
builder,
2159-
);
2160-
add_flags_and_try_run_tests(builder, &mut cargo)
2161-
}
2162-
2163-
/// Given a `cargo test` subcommand, pass it the appropriate test flags given a `builder`.
2164-
fn prepare_cargo_test(
2165-
cargo: builder::Cargo,
2166-
libtest_args: &[&str],
2167-
crates: &[String],
2168-
primary_crate: &str,
2169-
target: TargetSelection,
2170-
builder: &Builder<'_>,
2171-
) -> BootstrapCommand {
2172-
let compiler = cargo.compiler();
2173-
let mut cargo: BootstrapCommand = cargo.into();
2174-
2175-
// Propagate `--bless` if it has not already been set/unset
2176-
// Any tools that want to use this should bless if `RUSTC_BLESS` is set to
2177-
// anything other than `0`.
2178-
if builder.config.cmd.bless() && !cargo.get_envs().any(|v| v.0 == "RUSTC_BLESS") {
2179-
cargo.env("RUSTC_BLESS", "Gesundheit");
2180-
}
2181-
2182-
// Pass in some standard flags then iterate over the graph we've discovered
2183-
// in `cargo metadata` with the maps above and figure out what `-p`
2184-
// arguments need to get passed.
2185-
if builder.kind == Kind::Test && !builder.fail_fast {
2186-
cargo.arg("--no-fail-fast");
2187-
}
2188-
2189-
if builder.config.json_output {
2190-
cargo.arg("--message-format=json");
2191-
}
2192-
2193-
match builder.doc_tests {
2194-
DocTests::Only => {
2195-
cargo.arg("--doc");
2196-
}
2197-
DocTests::No => {
2198-
let krate = &builder
2199-
.crates
2200-
.get(primary_crate)
2201-
.unwrap_or_else(|| panic!("missing crate {primary_crate}"));
2202-
if krate.has_lib {
2203-
cargo.arg("--lib");
2204-
}
2205-
cargo.args(["--bins", "--examples", "--tests", "--benches"]);
2206-
}
2207-
DocTests::Yes => {}
2208-
}
2209-
2210-
for krate in crates {
2211-
cargo.arg("-p").arg(krate);
2212-
}
2213-
2214-
cargo.arg("--").args(builder.config.test_args()).args(libtest_args);
2215-
if !builder.config.verbose_tests {
2216-
cargo.arg("--quiet");
2217-
}
2218-
2219-
// The tests are going to run with the *target* libraries, so we need to
2220-
// ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
2221-
//
2222-
// Note that to run the compiler we need to run with the *host* libraries,
2223-
// but our wrapper scripts arrange for that to be the case anyway.
2224-
//
2225-
// We skip everything on Miri as then this overwrites the libdir set up
2226-
// by `Cargo::new` and that actually makes things go wrong.
2227-
if builder.kind != Kind::Miri {
2228-
let mut dylib_path = dylib_path();
2229-
dylib_path.insert(0, PathBuf::from(&*builder.sysroot_target_libdir(compiler, target)));
2230-
cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
2231-
}
2232-
2233-
if builder.remote_tested(target) {
2234-
cargo.env(
2235-
format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
2236-
format!("{} run 0", builder.tool_exe(Tool::RemoteTestClient).display()),
2237-
);
2238-
} else if let Some(tool) = builder.runner(target) {
2239-
cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)), tool);
2240-
}
2241-
2242-
cargo
2243-
}
2244-
22452133
/// Runs `cargo test` for standard library crates.
22462134
///
22472135
/// (Also used internally to run `cargo test` for compiler crates.)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
4+
use crate::core::build_steps::tool::Tool;
5+
use crate::core::builder::{self, Builder, Kind};
6+
use crate::core::config::TargetSelection;
7+
use crate::utils::exec::BootstrapCommand;
8+
use crate::utils::helpers::{self, dylib_path, dylib_path_var};
9+
use crate::utils::render_tests::add_flags_and_try_run_tests;
10+
use crate::{DocTests, envify};
11+
12+
/// Given a `cargo test` subcommand, add the appropriate flags and run it.
13+
///
14+
/// Returns whether the test succeeded.
15+
pub(super) fn run_cargo_test<'a>(
16+
cargo: builder::Cargo,
17+
libtest_args: &[&str],
18+
crates: &[String],
19+
primary_crate: &str,
20+
description: impl Into<Option<&'a str>>,
21+
target: TargetSelection,
22+
builder: &Builder<'_>,
23+
) -> bool {
24+
let compiler = cargo.compiler();
25+
let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, primary_crate, target, builder);
26+
let _time = helpers::timeit(builder);
27+
let _group = description.into().and_then(|what| {
28+
builder.msg_sysroot_tool(Kind::Test, compiler.stage, what, compiler.host, target)
29+
});
30+
31+
#[cfg(feature = "build-metrics")]
32+
builder.metrics.begin_test_suite(
33+
build_helper::metrics::TestSuiteMetadata::CargoPackage {
34+
crates: crates.iter().map(|c| c.to_string()).collect(),
35+
target: target.triple.to_string(),
36+
host: compiler.host.triple.to_string(),
37+
stage: compiler.stage,
38+
},
39+
builder,
40+
);
41+
add_flags_and_try_run_tests(builder, &mut cargo)
42+
}
43+
44+
/// Given a `cargo test` subcommand, pass it the appropriate test flags given a `builder`.
45+
pub(super) fn prepare_cargo_test(
46+
cargo: builder::Cargo,
47+
libtest_args: &[&str],
48+
crates: &[String],
49+
primary_crate: &str,
50+
target: TargetSelection,
51+
builder: &Builder<'_>,
52+
) -> BootstrapCommand {
53+
let compiler = cargo.compiler();
54+
let mut cargo: BootstrapCommand = cargo.into();
55+
56+
// Propagate `--bless` if it has not already been set/unset
57+
// Any tools that want to use this should bless if `RUSTC_BLESS` is set to
58+
// anything other than `0`.
59+
if builder.config.cmd.bless() && !cargo.get_envs().any(|v| v.0 == "RUSTC_BLESS") {
60+
cargo.env("RUSTC_BLESS", "Gesundheit");
61+
}
62+
63+
// Pass in some standard flags then iterate over the graph we've discovered
64+
// in `cargo metadata` with the maps above and figure out what `-p`
65+
// arguments need to get passed.
66+
if builder.kind == Kind::Test && !builder.fail_fast {
67+
cargo.arg("--no-fail-fast");
68+
}
69+
70+
if builder.config.json_output {
71+
cargo.arg("--message-format=json");
72+
}
73+
74+
match builder.doc_tests {
75+
DocTests::Only => {
76+
cargo.arg("--doc");
77+
}
78+
DocTests::No => {
79+
let krate = &builder
80+
.crates
81+
.get(primary_crate)
82+
.unwrap_or_else(|| panic!("missing crate {primary_crate}"));
83+
if krate.has_lib {
84+
cargo.arg("--lib");
85+
}
86+
cargo.args(["--bins", "--examples", "--tests", "--benches"]);
87+
}
88+
DocTests::Yes => {}
89+
}
90+
91+
for krate in crates {
92+
cargo.arg("-p").arg(krate);
93+
}
94+
95+
cargo.arg("--").args(builder.config.test_args()).args(libtest_args);
96+
if !builder.config.verbose_tests {
97+
cargo.arg("--quiet");
98+
}
99+
100+
// The tests are going to run with the *target* libraries, so we need to
101+
// ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
102+
//
103+
// Note that to run the compiler we need to run with the *host* libraries,
104+
// but our wrapper scripts arrange for that to be the case anyway.
105+
//
106+
// We skip everything on Miri as then this overwrites the libdir set up
107+
// by `Cargo::new` and that actually makes things go wrong.
108+
if builder.kind != Kind::Miri {
109+
let mut dylib_path = dylib_path();
110+
dylib_path.insert(0, PathBuf::from(&*builder.sysroot_target_libdir(compiler, target)));
111+
cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
112+
}
113+
114+
if builder.remote_tested(target) {
115+
cargo.env(
116+
format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
117+
format!("{} run 0", builder.tool_exe(Tool::RemoteTestClient).display()),
118+
);
119+
} else if let Some(tool) = builder.runner(target) {
120+
cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)), tool);
121+
}
122+
123+
cargo
124+
}

0 commit comments

Comments
 (0)