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.
This commit is contained in:
Brezak 2024-06-04 23:40:40 +02:00 committed by GitHub
parent 16207e4043
commit 1fdddf8992
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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::<Events<AppExit>>() {
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<AppExit>) {
// 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: <https://github.com/bevyengine/bevy/issues/10385>