Add temporary directory for executing snapshot tests

This commit is contained in:
Jakub Beránek 2025-06-20 08:17:39 +02:00
parent 718e475cb1
commit d475e10dcb
No known key found for this signature in database
GPG key ID: 909CD0D26483516B
2 changed files with 45 additions and 16 deletions

View file

@ -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"

View file

@ -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 <kind>`). 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<String>,
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))
}
}