From 1fdddf8992a70fa015df332059c09e9f03a4d0f4 Mon Sep 17 00:00:00 2001 From: Brezak Date: Tue, 4 Jun 2024 23:40:40 +0200 Subject: [PATCH] Forward exit codes in default app runner (#13674) # Objective The default app runner fabricates exit codes loosing useful info in the process. ## Solution - Make run_once extract the correct exit code from app. - Add a test to confirm it works. ## Testing - Run the `runner_returns_correct_exit_code` test. - Rejoice when it succeeds. --- crates/bevy_app/src/app.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index b3cf89c308..2e5ba17a0e 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -842,17 +842,7 @@ fn run_once(mut app: App) -> AppExit { app.update(); - let mut exit_code_reader = ManualEventReader::default(); - if let Some(app_exit_events) = app.world().get_resource::>() { - if exit_code_reader - .read(app_exit_events) - .any(AppExit::is_error) - { - return AppExit::error(); - } - } - - AppExit::Success + app.should_exit().unwrap_or(AppExit::Success) } /// An event that indicates the [`App`] should exit. If one or more of these are present at the end of an update, @@ -928,9 +918,9 @@ impl Termination for AppExit { mod tests { use std::{marker::PhantomData, mem}; - use bevy_ecs::{schedule::ScheduleLabel, system::Commands}; + use bevy_ecs::{event::EventWriter, schedule::ScheduleLabel, system::Commands}; - use crate::{App, AppExit, Plugin}; + use crate::{App, AppExit, Plugin, Update}; struct PluginA; impl Plugin for PluginA { @@ -1133,6 +1123,21 @@ mod tests { ); } + #[test] + fn runner_returns_correct_exit_code() { + fn raise_exits(mut exits: EventWriter) { + // Exit codes chosen by a fair dice roll. + // Unlikely to overlap with default values. + exits.send(AppExit::Success); + exits.send(AppExit::from_code(4)); + exits.send(AppExit::from_code(73)); + } + + let exit = App::new().add_systems(Update, raise_exits).run(); + + assert_eq!(exit, AppExit::from_code(4)); + } + /// Custom runners should be in charge of when `app::update` gets called as they may need to /// coordinate some state. /// bug: