Skip to content

Commit 9388c8e

Browse files
committed
record tests in build metrics
1 parent f816d3a commit 9388c8e

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/bootstrap/metrics.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl BuildMetrics {
5151
duration_excluding_children_sec: Duration::ZERO,
5252

5353
children: Vec::new(),
54+
tests: Vec::new(),
5455
});
5556
}
5657

@@ -72,6 +73,16 @@ impl BuildMetrics {
7273
}
7374
}
7475

76+
pub(crate) fn record_test(&self, name: &str, outcome: TestOutcome) {
77+
let mut state = self.state.borrow_mut();
78+
state
79+
.running_steps
80+
.last_mut()
81+
.unwrap()
82+
.tests
83+
.push(Test { name: name.to_string(), outcome });
84+
}
85+
7586
fn collect_stats(&self, state: &mut MetricsState) {
7687
let step = state.running_steps.last_mut().unwrap();
7788

@@ -125,6 +136,14 @@ impl BuildMetrics {
125136
}
126137

127138
fn prepare_json_step(&self, step: StepMetrics) -> JsonNode {
139+
let mut children = Vec::new();
140+
children.extend(step.children.into_iter().map(|child| self.prepare_json_step(child)));
141+
children.extend(
142+
step.tests
143+
.into_iter()
144+
.map(|test| JsonNode::Test { name: test.name, outcome: test.outcome }),
145+
);
146+
128147
JsonNode::RustbuildStep {
129148
type_: step.type_,
130149
debug_repr: step.debug_repr,
@@ -135,11 +154,7 @@ impl BuildMetrics {
135154
/ step.duration_excluding_children_sec.as_secs_f64(),
136155
},
137156

138-
children: step
139-
.children
140-
.into_iter()
141-
.map(|child| self.prepare_json_step(child))
142-
.collect(),
157+
children,
143158
}
144159
}
145160
}
@@ -161,6 +176,12 @@ struct StepMetrics {
161176
duration_excluding_children_sec: Duration,
162177

163178
children: Vec<StepMetrics>,
179+
tests: Vec<Test>,
180+
}
181+
182+
struct Test {
183+
name: String,
184+
outcome: TestOutcome,
164185
}
165186

166187
#[derive(Serialize, Deserialize)]
@@ -190,6 +211,19 @@ enum JsonNode {
190211

191212
children: Vec<JsonNode>,
192213
},
214+
Test {
215+
name: String,
216+
#[serde(flatten)]
217+
outcome: TestOutcome,
218+
},
219+
}
220+
221+
#[derive(Serialize, Deserialize)]
222+
#[serde(tag = "outcome", rename_all = "snake_case")]
223+
pub(crate) enum TestOutcome {
224+
Passed,
225+
Failed,
226+
Ignored { ignore_reason: Option<String> },
193227
}
194228

195229
#[derive(Serialize, Deserialize)]

src/bootstrap/render_tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ impl<'a> Renderer<'a> {
9999

100100
fn render_test_outcome(&mut self, outcome: Outcome<'_>, test: &TestOutcome) {
101101
self.executed_tests += 1;
102+
103+
#[cfg(feature = "build-metrics")]
104+
self.builder.metrics.record_test(
105+
&test.name,
106+
match outcome {
107+
Outcome::Ok => crate::metrics::TestOutcome::Passed,
108+
Outcome::Failed => crate::metrics::TestOutcome::Failed,
109+
Outcome::Ignored { reason } => crate::metrics::TestOutcome::Ignored {
110+
ignore_reason: reason.map(|s| s.to_string()),
111+
},
112+
},
113+
);
114+
102115
if self.builder.config.verbose_tests {
103116
self.render_test_outcome_verbose(outcome, test);
104117
} else {

0 commit comments

Comments
 (0)