Skip to content

Commit d475e10

Browse files
committed
Add temporary directory for executing snapshot tests
1 parent 718e475 commit d475e10

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/bootstrap/src/core/builder/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,12 +1242,13 @@ mod staging {
12421242
use crate::core::builder::tests::{
12431243
TEST_TRIPLE_1, configure, configure_with_args, render_steps, run_build,
12441244
};
1245-
use crate::utils::tests::ConfigBuilder;
1245+
use crate::utils::tests::{ConfigBuilder, TestCtx};
12461246

12471247
#[test]
12481248
fn build_compiler_stage_1() {
1249+
let ctx = TestCtx::new();
12491250
insta::assert_snapshot!(
1250-
ConfigBuilder::build()
1251+
ctx.config("build")
12511252
.path("compiler")
12521253
.stage(1)
12531254
.get_steps(), @r"

src/bootstrap/src/utils/tests/mod.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
11
//! This module contains shared utilities for bootstrap tests.
22
3+
use std::path::{Path, PathBuf};
4+
use std::thread;
5+
6+
use tempfile::TempDir;
7+
38
use crate::core::builder::Builder;
49
use crate::core::config::DryRun;
5-
use crate::{Build, Config, Flags};
10+
use crate::{Build, Config, Flags, t};
611

712
pub mod git;
813

14+
/// Holds temporary state of a bootstrap test.
15+
/// Right now it is only used to redirect the build directory of the bootstrap
16+
/// invocation, in the future it would be great if we could actually execute
17+
/// the whole test with this directory set as the workdir.
18+
pub struct TestCtx {
19+
directory: TempDir,
20+
}
21+
22+
impl TestCtx {
23+
pub fn new() -> Self {
24+
let directory = TempDir::new().expect("cannot create temporary directory");
25+
eprintln!("Running test in {}", directory.path().display());
26+
Self { directory }
27+
}
28+
29+
/// Starts a new invocation of bootstrap that executes `kind` as its top level command
30+
/// (i.e. `x <kind>`). Returns a builder that configures the created config through CLI flags.
31+
pub fn config(&self, kind: &str) -> ConfigBuilder {
32+
ConfigBuilder::from_args(&[kind], self.directory.path().to_owned())
33+
}
34+
}
35+
936
/// Used to configure an invocation of bootstrap.
1037
/// Currently runs in the rustc checkout, long-term it should be switched
1138
/// to run in a (cache-primed) temporary directory instead.
1239
pub struct ConfigBuilder {
1340
args: Vec<String>,
41+
directory: PathBuf,
1442
}
1543

1644
impl ConfigBuilder {
17-
pub fn from_args(args: &[&str]) -> Self {
18-
Self::new(args)
19-
}
20-
21-
pub fn build() -> Self {
22-
Self::new(&["build"])
45+
fn from_args(args: &[&str], directory: PathBuf) -> Self {
46+
Self { args: args.iter().copied().map(String::from).collect(), directory }
2347
}
2448

2549
pub fn path(mut self, path: &str) -> Self {
@@ -33,14 +57,18 @@ impl ConfigBuilder {
3357
self
3458
}
3559

36-
fn new(args: &[&str]) -> Self {
37-
Self { args: args.iter().copied().map(String::from).collect() }
38-
}
39-
4060
pub fn create_config(mut self) -> Config {
41-
let mut config = Config::parse(Flags::parse(&self.args));
4261
// Run in dry-check, otherwise the test would be too slow
43-
config.set_dry_run(DryRun::SelfCheck);
44-
config
62+
self.args.push("--dry-run".to_string());
63+
64+
// Ignore submodules
65+
self.args.push("--set".to_string());
66+
self.args.push("build.submodules=false".to_string());
67+
68+
// Do not mess with the local rustc checkout build directory
69+
self.args.push("--build-dir".to_string());
70+
self.args.push(self.directory.join("build").display().to_string());
71+
72+
Config::parse(Flags::parse(&self.args))
4573
}
4674
}

0 commit comments

Comments
 (0)