Skip to content

Commit 97dcb27

Browse files
authored
CI tests can exit directly after taking a screenshot (#19806)
# Objective - Currently, CI tests take a screenshot at frame X and exits at frame Y with X < Y, and both number fixed - This means tests can take longer than they actually need when taking the screenshot is fast, and can fail to take the screenshot when it's taking too long ## Solution - Add a new event `ScreenshotAndExit` that exit directly after the screenshot is saved
1 parent 04c2b32 commit 97dcb27

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

crates/bevy_dev_tools/src/ci_testing/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub enum CiTestingEvent {
3737
/// Takes a screenshot of the entire screen, and saves the results to
3838
/// `screenshot-{current_frame}.png`.
3939
Screenshot,
40+
/// Takes a screenshot of the entire screen, saves the results to
41+
/// `screenshot-{current_frame}.png`, and exits once the screenshot is taken.
42+
ScreenshotAndExit,
4043
/// Takes a screenshot of the entire screen, and saves the results to
4144
/// `screenshot-{name}.png`.
4245
NamedScreenshot(String),

crates/bevy_dev_tools/src/ci_testing/systems.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ pub(crate) fn send_events(world: &mut World, mut current_frame: Local<u32>) {
2121
world.send_event(AppExit::Success);
2222
info!("Exiting after {} frames. Test successful!", *current_frame);
2323
}
24+
CiTestingEvent::ScreenshotAndExit => {
25+
let this_frame = *current_frame;
26+
world.spawn(Screenshot::primary_window()).observe(
27+
move |captured: On<bevy_render::view::screenshot::ScreenshotCaptured>,
28+
mut exit_event: EventWriter<AppExit>| {
29+
let path = format!("./screenshot-{}.png", this_frame);
30+
save_to_disk(path)(captured);
31+
info!("Exiting. Test successful!");
32+
exit_event.write(AppExit::Success);
33+
},
34+
);
35+
info!("Took a screenshot at frame {}.", *current_frame);
36+
}
2437
CiTestingEvent::Screenshot => {
2538
let path = format!("./screenshot-{}.png", *current_frame);
2639
world

tools/example-showcase/src/main.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ enum Action {
5555
/// This defaults to frame 250. Set it to 0 to not stop the example automatically.
5656
stop_frame: u32,
5757

58+
#[arg(long, default_value = "false")]
59+
/// Automatically ends after taking a screenshot
60+
///
61+
/// Only works if `screenshot-frame` is set to non-0, and overrides `stop-frame`.
62+
auto_stop_frame: bool,
63+
5864
#[arg(long)]
5965
/// Which frame to take a screenshot at. Set to 0 for no screenshot.
6066
screenshot_frame: u32,
@@ -150,6 +156,7 @@ fn main() {
150156
Action::Run {
151157
wgpu_backend,
152158
stop_frame,
159+
auto_stop_frame,
153160
screenshot_frame,
154161
fixed_frame_time,
155162
in_ci,
@@ -183,11 +190,21 @@ fn main() {
183190

184191
let mut extra_parameters = vec![];
185192

186-
match (stop_frame, screenshot_frame) {
193+
match (stop_frame, screenshot_frame, auto_stop_frame) {
187194
// When the example does not automatically stop nor take a screenshot.
188-
(0, 0) => (),
195+
(0, 0, _) => (),
196+
// When the example automatically stops at an automatic frame.
197+
(0, _, true) => {
198+
let mut file = File::create("example_showcase_config.ron").unwrap();
199+
file.write_all(
200+
format!("(setup: (fixed_frame_time: Some({fixed_frame_time})), events: [({screenshot_frame}, ScreenshotAndExit)])").as_bytes(),
201+
)
202+
.unwrap();
203+
extra_parameters.push("--features");
204+
extra_parameters.push("bevy_ci_testing");
205+
}
189206
// When the example does not automatically stop.
190-
(0, _) => {
207+
(0, _, false) => {
191208
let mut file = File::create("example_showcase_config.ron").unwrap();
192209
file.write_all(
193210
format!("(setup: (fixed_frame_time: Some({fixed_frame_time})), events: [({screenshot_frame}, Screenshot)])").as_bytes(),
@@ -197,15 +214,25 @@ fn main() {
197214
extra_parameters.push("bevy_ci_testing");
198215
}
199216
// When the example does not take a screenshot.
200-
(_, 0) => {
217+
(_, 0, _) => {
201218
let mut file = File::create("example_showcase_config.ron").unwrap();
202219
file.write_all(format!("(events: [({stop_frame}, AppExit)])").as_bytes())
203220
.unwrap();
204221
extra_parameters.push("--features");
205222
extra_parameters.push("bevy_ci_testing");
206223
}
224+
// When the example both automatically stops at an automatic frame and takes a screenshot.
225+
(_, _, true) => {
226+
let mut file = File::create("example_showcase_config.ron").unwrap();
227+
file.write_all(
228+
format!("(setup: (fixed_frame_time: Some({fixed_frame_time})), events: [({screenshot_frame}, ScreenshotAndExit)])").as_bytes(),
229+
)
230+
.unwrap();
231+
extra_parameters.push("--features");
232+
extra_parameters.push("bevy_ci_testing");
233+
}
207234
// When the example both automatically stops and takes a screenshot.
208-
(_, _) => {
235+
(_, _, false) => {
209236
let mut file = File::create("example_showcase_config.ron").unwrap();
210237
file.write_all(
211238
format!("(setup: (fixed_frame_time: Some({fixed_frame_time})), events: [({screenshot_frame}, Screenshot), ({stop_frame}, AppExit)])").as_bytes(),

0 commit comments

Comments
 (0)