Skip to content

Commit 8cf860e

Browse files
committed
Normalize host target in snapshot tests
1 parent e86215f commit 8cf860e

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::*;
88
use crate::Flags;
99
use crate::core::build_steps::doc::DocumentationFormat;
1010
use crate::core::config::Config;
11+
use crate::utils::helpers::get_host_target;
1112
use crate::utils::tests::git::{GitCtx, git_test};
1213

1314
static TEST_TRIPLE_1: &str = "i686-unknown-haiku";
@@ -1235,24 +1236,38 @@ fn any_debug() {
12351236
/// The staging tests use insta for snapshot testing.
12361237
/// See bootstrap's README on how to bless the snapshots.
12371238
mod staging {
1239+
use crate::Build;
1240+
use crate::core::builder::Builder;
12381241
use crate::core::builder::tests::{
12391242
TEST_TRIPLE_1, configure, configure_with_args, render_steps, run_build,
12401243
};
1244+
use crate::utils::tests::ConfigBuilder;
12411245

12421246
#[test]
12431247
fn build_compiler_stage_1() {
1244-
let mut cache = run_build(
1245-
&["compiler".into()],
1246-
configure_with_args(&["build", "--stage", "1"], &[TEST_TRIPLE_1], &[TEST_TRIPLE_1]),
1247-
);
1248-
let steps = cache.into_executed_steps();
1249-
insta::assert_snapshot!(render_steps(&steps), @r"
1250-
[build] rustc 0 <target1> -> std 0 <target1>
1251-
[build] llvm <target1>
1252-
[build] rustc 0 <target1> -> rustc 1 <target1>
1253-
[build] rustc 0 <target1> -> rustc 1 <target1>
1248+
insta::assert_snapshot!(
1249+
ConfigBuilder::build()
1250+
.path("compiler")
1251+
.stage(1)
1252+
.get_steps(), @r"
1253+
[build] rustc 0 <host> -> std 0 <host>
1254+
[build] llvm <host>
1255+
[build] rustc 0 <host> -> rustc 1 <host>
1256+
[build] rustc 0 <host> -> rustc 1 <host>
12541257
");
12551258
}
1259+
1260+
impl ConfigBuilder {
1261+
fn get_steps(self) -> String {
1262+
let config = self.create_config();
1263+
1264+
let kind = config.cmd.kind();
1265+
let build = Build::new(config);
1266+
let builder = Builder::new(&build);
1267+
builder.run_step_descriptions(&Builder::get_step_descriptions(kind), &builder.paths);
1268+
render_steps(&builder.cache.into_executed_steps())
1269+
}
1270+
}
12561271
}
12571272

12581273
/// Renders the executed bootstrap steps for usage in snapshot tests with insta.
@@ -1276,19 +1291,14 @@ fn render_steps(steps: &[(Box<dyn Any>, Box<dyn Any>)]) -> String {
12761291
"[build] {} -> std {} <{}>",
12771292
render_compiler(std.compiler),
12781293
std.compiler.stage,
1279-
std.target
1294+
normalize_target(std.target)
12801295
))
12811296
} else if let Some((llvm, output)) = downcast_step::<llvm::Llvm>(step, output) {
1282-
Some(format!("[build] llvm <{}>", llvm.target))
1297+
Some(format!("[build] llvm <{}>", normalize_target(llvm.target)))
12831298
} else {
12841299
None
12851300
}
12861301
})
1287-
.map(|line| {
1288-
line.replace(TEST_TRIPLE_1, "target1")
1289-
.replace(TEST_TRIPLE_2, "target2")
1290-
.replace(TEST_TRIPLE_3, "target3")
1291-
})
12921302
.collect::<Vec<_>>()
12931303
.join("\n")
12941304
}
@@ -1306,6 +1316,10 @@ fn downcast_step<'a, S: Step>(
13061316
Some((step, output))
13071317
}
13081318

1319+
fn normalize_target(target: TargetSelection) -> String {
1320+
target.to_string().replace(&get_host_target().to_string(), "host")
1321+
}
1322+
13091323
fn render_compiler(compiler: Compiler) -> String {
1310-
format!("rustc {} <{}>", compiler.stage, compiler.host)
1324+
format!("rustc {} <{}>", compiler.stage, normalize_target(compiler.host))
13111325
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
11
//! This module contains shared utilities for bootstrap tests.
22
3+
use crate::core::builder::Builder;
4+
use crate::core::config::DryRun;
5+
use crate::{Build, Config, Flags};
6+
37
pub mod git;
8+
9+
/// Used to configure an invocation of bootstrap.
10+
/// Currently runs in the rustc checkout, long-term it should be switched
11+
/// to run in a (cache-primed) temporary directory instead.
12+
pub struct ConfigBuilder {
13+
args: Vec<String>,
14+
}
15+
16+
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"])
23+
}
24+
25+
pub fn path(mut self, path: &str) -> Self {
26+
self.args.push(path.to_string());
27+
self
28+
}
29+
30+
pub fn stage(mut self, stage: u32) -> Self {
31+
self.args.push("--stage".to_string());
32+
self.args.push(stage.to_string());
33+
self
34+
}
35+
36+
fn new(args: &[&str]) -> Self {
37+
Self { args: args.iter().copied().map(String::from).collect() }
38+
}
39+
40+
pub fn create_config(mut self) -> Config {
41+
let mut config = Config::parse(Flags::parse(&self.args));
42+
// Run in dry-check, otherwise the test would be too slow
43+
config.set_dry_run(DryRun::SelfCheck);
44+
config
45+
}
46+
}

0 commit comments

Comments
 (0)