From d475e10dcb1bbd3193edb9968dc9e8f60b7247df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 20 Jun 2025 08:17:39 +0200 Subject: [PATCH] Add temporary directory for executing snapshot tests --- src/bootstrap/src/core/builder/tests.rs | 5 ++- src/bootstrap/src/utils/tests/mod.rs | 56 ++++++++++++++++++------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 1d1c315c1cfd..f1af2b285a28 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -1242,12 +1242,13 @@ mod staging { use crate::core::builder::tests::{ TEST_TRIPLE_1, configure, configure_with_args, render_steps, run_build, }; - use crate::utils::tests::ConfigBuilder; + use crate::utils::tests::{ConfigBuilder, TestCtx}; #[test] fn build_compiler_stage_1() { + let ctx = TestCtx::new(); insta::assert_snapshot!( - ConfigBuilder::build() + ctx.config("build") .path("compiler") .stage(1) .get_steps(), @r" diff --git a/src/bootstrap/src/utils/tests/mod.rs b/src/bootstrap/src/utils/tests/mod.rs index ec6293f3c2c1..91877fd0da4d 100644 --- a/src/bootstrap/src/utils/tests/mod.rs +++ b/src/bootstrap/src/utils/tests/mod.rs @@ -1,25 +1,49 @@ //! This module contains shared utilities for bootstrap tests. +use std::path::{Path, PathBuf}; +use std::thread; + +use tempfile::TempDir; + use crate::core::builder::Builder; use crate::core::config::DryRun; -use crate::{Build, Config, Flags}; +use crate::{Build, Config, Flags, t}; pub mod git; +/// Holds temporary state of a bootstrap test. +/// Right now it is only used to redirect the build directory of the bootstrap +/// invocation, in the future it would be great if we could actually execute +/// the whole test with this directory set as the workdir. +pub struct TestCtx { + directory: TempDir, +} + +impl TestCtx { + pub fn new() -> Self { + let directory = TempDir::new().expect("cannot create temporary directory"); + eprintln!("Running test in {}", directory.path().display()); + Self { directory } + } + + /// Starts a new invocation of bootstrap that executes `kind` as its top level command + /// (i.e. `x `). Returns a builder that configures the created config through CLI flags. + pub fn config(&self, kind: &str) -> ConfigBuilder { + ConfigBuilder::from_args(&[kind], self.directory.path().to_owned()) + } +} + /// Used to configure an invocation of bootstrap. /// Currently runs in the rustc checkout, long-term it should be switched /// to run in a (cache-primed) temporary directory instead. pub struct ConfigBuilder { args: Vec, + directory: PathBuf, } impl ConfigBuilder { - pub fn from_args(args: &[&str]) -> Self { - Self::new(args) - } - - pub fn build() -> Self { - Self::new(&["build"]) + fn from_args(args: &[&str], directory: PathBuf) -> Self { + Self { args: args.iter().copied().map(String::from).collect(), directory } } pub fn path(mut self, path: &str) -> Self { @@ -33,14 +57,18 @@ impl ConfigBuilder { self } - fn new(args: &[&str]) -> Self { - Self { args: args.iter().copied().map(String::from).collect() } - } - pub fn create_config(mut self) -> Config { - let mut config = Config::parse(Flags::parse(&self.args)); // Run in dry-check, otherwise the test would be too slow - config.set_dry_run(DryRun::SelfCheck); - config + self.args.push("--dry-run".to_string()); + + // Ignore submodules + self.args.push("--set".to_string()); + self.args.push("build.submodules=false".to_string()); + + // Do not mess with the local rustc checkout build directory + self.args.push("--build-dir".to_string()); + self.args.push(self.directory.join("build").display().to_string()); + + Config::parse(Flags::parse(&self.args)) } }