Skip to content

Breakup test.rs into more-manageable sub-modules #137224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bbee4a5
Convert `test.rs` to directory-module
jieyouxu Feb 17, 2025
8b27aa0
Break out `tidy` test step into its own module
jieyouxu Feb 17, 2025
488e956
Add temporary `#[expect(unused_imports)]` to help splitting
jieyouxu Feb 17, 2025
2961b46
Move tidy step's `{should,make}_run` before `run` for consistency
jieyouxu Feb 17, 2025
7e31dac
Stop reinventing `builder.test_out` as `testdir`
jieyouxu Feb 18, 2025
c5e8f58
Move book tests and error-index into their own `book_tests` module
jieyouxu Feb 18, 2025
5fd30f8
Extract `{prepare,run}_cargo_test` into `test_helpers` module
jieyouxu Feb 18, 2025
25be3eb
Extract `{Crate,}Bootstrap` bootstrap self-test steps into `bootstrap…
jieyouxu Feb 18, 2025
7b15551
Reorder `Bootstrap::{should,make}_run` to match usual order
jieyouxu Feb 18, 2025
89cc96d
Break out `{Cargo,}Miri` into their own `miri_tests` module
jieyouxu Feb 18, 2025
3bd2561
Break out `{Cargo,RustAnalyzer,Rustfmt,Clippy}` into their own `devto…
jieyouxu Feb 18, 2025
fbf743c
Break out `CrateRustdoc{,JsonTypes}`/`RustdocTheme` into their own `r…
jieyouxu Feb 18, 2025
34a1749
Break out compiletest self-test into its own `compiletest_self_tests`…
jieyouxu Feb 18, 2025
9d58554
Move `RemoteCopyLibs` to `test_helpers`
jieyouxu Feb 18, 2025
06768f4
Move compiletest test steps and related helpers into `compiletest_sui…
jieyouxu Feb 18, 2025
f1974d7
Extract `Crate` to `test_helpers`
jieyouxu Feb 18, 2025
a55e8d2
Move `CrateLibrustc` and `Codegen{Cranelift,GCC}` into their own `com…
jieyouxu Feb 18, 2025
f8c59dc
Extract `TestParseFloat` into `std_tests` module
jieyouxu Feb 18, 2025
70b63a6
Break out `Cargotest` into its own `cargotest` module
jieyouxu Feb 18, 2025
c10e7dd
Move `RustdocTheme` to `rustdoc_tests`
jieyouxu Feb 18, 2025
d4f534f
Extract `{HtmlCheck,Linkcheck}` into their own `doc_tool_tests` module
jieyouxu Feb 18, 2025
4a44164
Move `CrateBuildHelper` into `bootstrap_self_tests`
jieyouxu Feb 18, 2025
d6a9f7d
Extract `{RustInstaller,Distcheck,CollectLicenseMetadata}` into their…
jieyouxu Feb 18, 2025
fbca76f
Move `{LintDocs,TierCheck}` into `doc_tool_tests`
jieyouxu Feb 18, 2025
77adc12
Remove unused imports in `test/mod.rs`
jieyouxu Feb 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract {prepare,run}_cargo_test into test_helpers module
  • Loading branch information
jieyouxu committed Feb 18, 2025
commit 5fd30f840a9a47233e76840eddf18f88b1848fe0
116 changes: 2 additions & 114 deletions src/bootstrap/src/core/build_steps/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::{env, fs, iter};

use clap_complete::shells;

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

mod book_tests;
mod test_helpers;
mod tidy;

pub(crate) use book_tests::{
Expand Down Expand Up @@ -2128,120 +2130,6 @@ impl Step for CrateLibrustc {
}
}

/// Given a `cargo test` subcommand, add the appropriate flags and run it.
///
/// Returns whether the test succeeded.
fn run_cargo_test<'a>(
cargo: builder::Cargo,
libtest_args: &[&str],
crates: &[String],
primary_crate: &str,
description: impl Into<Option<&'a str>>,
target: TargetSelection,
builder: &Builder<'_>,
) -> bool {
let compiler = cargo.compiler();
let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, primary_crate, target, builder);
let _time = helpers::timeit(builder);
let _group = description.into().and_then(|what| {
builder.msg_sysroot_tool(Kind::Test, compiler.stage, what, compiler.host, target)
});

#[cfg(feature = "build-metrics")]
builder.metrics.begin_test_suite(
build_helper::metrics::TestSuiteMetadata::CargoPackage {
crates: crates.iter().map(|c| c.to_string()).collect(),
target: target.triple.to_string(),
host: compiler.host.triple.to_string(),
stage: compiler.stage,
},
builder,
);
add_flags_and_try_run_tests(builder, &mut cargo)
}

/// Given a `cargo test` subcommand, pass it the appropriate test flags given a `builder`.
fn prepare_cargo_test(
cargo: builder::Cargo,
libtest_args: &[&str],
crates: &[String],
primary_crate: &str,
target: TargetSelection,
builder: &Builder<'_>,
) -> BootstrapCommand {
let compiler = cargo.compiler();
let mut cargo: BootstrapCommand = cargo.into();

// Propagate `--bless` if it has not already been set/unset
// Any tools that want to use this should bless if `RUSTC_BLESS` is set to
// anything other than `0`.
if builder.config.cmd.bless() && !cargo.get_envs().any(|v| v.0 == "RUSTC_BLESS") {
cargo.env("RUSTC_BLESS", "Gesundheit");
}

// Pass in some standard flags then iterate over the graph we've discovered
// in `cargo metadata` with the maps above and figure out what `-p`
// arguments need to get passed.
if builder.kind == Kind::Test && !builder.fail_fast {
cargo.arg("--no-fail-fast");
}

if builder.config.json_output {
cargo.arg("--message-format=json");
}

match builder.doc_tests {
DocTests::Only => {
cargo.arg("--doc");
}
DocTests::No => {
let krate = &builder
.crates
.get(primary_crate)
.unwrap_or_else(|| panic!("missing crate {primary_crate}"));
if krate.has_lib {
cargo.arg("--lib");
}
cargo.args(["--bins", "--examples", "--tests", "--benches"]);
}
DocTests::Yes => {}
}

for krate in crates {
cargo.arg("-p").arg(krate);
}

cargo.arg("--").args(builder.config.test_args()).args(libtest_args);
if !builder.config.verbose_tests {
cargo.arg("--quiet");
}

// The tests are going to run with the *target* libraries, so we need to
// ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
//
// Note that to run the compiler we need to run with the *host* libraries,
// but our wrapper scripts arrange for that to be the case anyway.
//
// We skip everything on Miri as then this overwrites the libdir set up
// by `Cargo::new` and that actually makes things go wrong.
if builder.kind != Kind::Miri {
let mut dylib_path = dylib_path();
dylib_path.insert(0, PathBuf::from(&*builder.sysroot_target_libdir(compiler, target)));
cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
}

if builder.remote_tested(target) {
cargo.env(
format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
format!("{} run 0", builder.tool_exe(Tool::RemoteTestClient).display()),
);
} else if let Some(tool) = builder.runner(target) {
cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)), tool);
}

cargo
}

/// Runs `cargo test` for standard library crates.
///
/// (Also used internally to run `cargo test` for compiler crates.)
Expand Down
124 changes: 124 additions & 0 deletions src/bootstrap/src/core/build_steps/test/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use std::env;
use std::path::PathBuf;

use crate::core::build_steps::tool::Tool;
use crate::core::builder::{self, Builder, Kind};
use crate::core::config::TargetSelection;
use crate::utils::exec::BootstrapCommand;
use crate::utils::helpers::{self, dylib_path, dylib_path_var};
use crate::utils::render_tests::add_flags_and_try_run_tests;
use crate::{DocTests, envify};

/// Given a `cargo test` subcommand, add the appropriate flags and run it.
///
/// Returns whether the test succeeded.
pub(super) fn run_cargo_test<'a>(
cargo: builder::Cargo,
libtest_args: &[&str],
crates: &[String],
primary_crate: &str,
description: impl Into<Option<&'a str>>,
target: TargetSelection,
builder: &Builder<'_>,
) -> bool {
let compiler = cargo.compiler();
let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, primary_crate, target, builder);
let _time = helpers::timeit(builder);
let _group = description.into().and_then(|what| {
builder.msg_sysroot_tool(Kind::Test, compiler.stage, what, compiler.host, target)
});

#[cfg(feature = "build-metrics")]
builder.metrics.begin_test_suite(
build_helper::metrics::TestSuiteMetadata::CargoPackage {
crates: crates.iter().map(|c| c.to_string()).collect(),
target: target.triple.to_string(),
host: compiler.host.triple.to_string(),
stage: compiler.stage,
},
builder,
);
add_flags_and_try_run_tests(builder, &mut cargo)
}

/// Given a `cargo test` subcommand, pass it the appropriate test flags given a `builder`.
pub(super) fn prepare_cargo_test(
cargo: builder::Cargo,
libtest_args: &[&str],
crates: &[String],
primary_crate: &str,
target: TargetSelection,
builder: &Builder<'_>,
) -> BootstrapCommand {
let compiler = cargo.compiler();
let mut cargo: BootstrapCommand = cargo.into();

// Propagate `--bless` if it has not already been set/unset
// Any tools that want to use this should bless if `RUSTC_BLESS` is set to
// anything other than `0`.
if builder.config.cmd.bless() && !cargo.get_envs().any(|v| v.0 == "RUSTC_BLESS") {
cargo.env("RUSTC_BLESS", "Gesundheit");
}

// Pass in some standard flags then iterate over the graph we've discovered
// in `cargo metadata` with the maps above and figure out what `-p`
// arguments need to get passed.
if builder.kind == Kind::Test && !builder.fail_fast {
cargo.arg("--no-fail-fast");
}

if builder.config.json_output {
cargo.arg("--message-format=json");
}

match builder.doc_tests {
DocTests::Only => {
cargo.arg("--doc");
}
DocTests::No => {
let krate = &builder
.crates
.get(primary_crate)
.unwrap_or_else(|| panic!("missing crate {primary_crate}"));
if krate.has_lib {
cargo.arg("--lib");
}
cargo.args(["--bins", "--examples", "--tests", "--benches"]);
}
DocTests::Yes => {}
}

for krate in crates {
cargo.arg("-p").arg(krate);
}

cargo.arg("--").args(builder.config.test_args()).args(libtest_args);
if !builder.config.verbose_tests {
cargo.arg("--quiet");
}

// The tests are going to run with the *target* libraries, so we need to
// ensure that those libraries show up in the LD_LIBRARY_PATH equivalent.
//
// Note that to run the compiler we need to run with the *host* libraries,
// but our wrapper scripts arrange for that to be the case anyway.
//
// We skip everything on Miri as then this overwrites the libdir set up
// by `Cargo::new` and that actually makes things go wrong.
if builder.kind != Kind::Miri {
let mut dylib_path = dylib_path();
dylib_path.insert(0, PathBuf::from(&*builder.sysroot_target_libdir(compiler, target)));
cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
}

if builder.remote_tested(target) {
cargo.env(
format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)),
format!("{} run 0", builder.tool_exe(Tool::RemoteTestClient).display()),
);
} else if let Some(tool) = builder.runner(target) {
cargo.env(format!("CARGO_TARGET_{}_RUNNER", envify(&target.triple)), tool);
}

cargo
}