Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_dev_tools/src/ci_testing/mod.rs
9325 views
1
//! Utilities for testing in CI environments.
2
3
mod config;
4
mod systems;
5
6
use crate::EasyCameraMovementPlugin;
7
#[cfg(feature = "screenrecording")]
8
use crate::EasyScreenRecordPlugin;
9
10
pub use self::config::*;
11
12
use bevy_app::prelude::*;
13
use bevy_ecs::prelude::*;
14
use bevy_render::view::screenshot::trigger_screenshots;
15
use bevy_time::TimeUpdateStrategy;
16
use core::time::Duration;
17
18
/// A plugin that instruments continuous integration testing by automatically executing user-defined actions.
19
///
20
/// This plugin reads a [`ron`] file specified with the `CI_TESTING_CONFIG` environmental variable
21
/// (`ci_testing_config.ron` by default) and executes its specified actions. For a reference of the
22
/// allowed configuration, see [`CiTestingConfig`].
23
///
24
/// This plugin is included within `DefaultPlugins` and `MinimalPlugins`
25
/// when the `bevy_ci_testing` feature is enabled.
26
/// It is recommended to only used this plugin during testing (manual or
27
/// automatic), and disable it during regular development and for production builds.
28
#[derive(Default)]
29
pub struct CiTestingPlugin;
30
31
impl Plugin for CiTestingPlugin {
32
fn build(&self, app: &mut App) {
33
let config = if !app.world().is_resource_added::<CiTestingConfig>() {
34
// Load configuration from file if not already setup
35
#[cfg(not(target_arch = "wasm32"))]
36
let config: CiTestingConfig = {
37
let filename = std::env::var("CI_TESTING_CONFIG")
38
.unwrap_or_else(|_| "ci_testing_config.ron".to_string());
39
std::fs::read_to_string(filename)
40
.map(|content| {
41
ron::from_str(&content)
42
.expect("error deserializing CI testing configuration file")
43
})
44
.unwrap_or_default()
45
};
46
47
#[cfg(target_arch = "wasm32")]
48
let config: CiTestingConfig = {
49
let config = include_str!("../../../../ci_testing_config.ron");
50
ron::from_str(config).expect("error deserializing CI testing configuration file")
51
};
52
53
config
54
} else {
55
app.world().resource::<CiTestingConfig>().clone()
56
};
57
58
// Add the `EasyCameraMovementPlugin` to the app if it's not already added.
59
// To configure the movement speed, add the plugin first.
60
if !app.is_plugin_added::<EasyCameraMovementPlugin>() {
61
app.add_plugins(EasyCameraMovementPlugin::default());
62
}
63
// Add the `EasyScreenRecordPlugin` to the app if it's not already added and one of the event is starting screenrecording.
64
// To configure the recording quality, add the plugin first.
65
#[cfg(feature = "screenrecording")]
66
if !app.is_plugin_added::<EasyScreenRecordPlugin>()
67
&& config
68
.events
69
.iter()
70
.any(|e| matches!(e.1, CiTestingEvent::StartScreenRecording))
71
{
72
app.add_plugins(EasyScreenRecordPlugin::default());
73
}
74
75
// Configure a fixed frame time if specified.
76
if let Some(fixed_frame_time) = config.setup.fixed_frame_time {
77
app.insert_resource(TimeUpdateStrategy::ManualDuration(Duration::from_secs_f32(
78
fixed_frame_time,
79
)));
80
}
81
app.add_message::<CiTestingCustomEvent>()
82
.insert_resource(config)
83
.add_systems(
84
Update,
85
systems::send_events
86
.before(trigger_screenshots)
87
.before(bevy_window::close_when_requested)
88
.in_set(EventSenderSystems)
89
.ambiguous_with_all(),
90
);
91
92
// The offending system does not exist in the wasm32 target.
93
// As a result, we must conditionally order the two systems using a system set.
94
#[cfg(any(unix, windows))]
95
app.configure_sets(
96
Update,
97
EventSenderSystems.before(bevy_app::TerminalCtrlCHandlerPlugin::exit_on_flag),
98
);
99
}
100
}
101
102
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
103
struct EventSenderSystems;
104
105