Skip to content

Commit 6bcba9d

Browse files
authored
Prevent the transaction log for a bevy_asset test from writing to the file system. (#22159)
# Objective - bevy_asset tests are writing to the filesystem! ## Solution - Share the impl of `create_app` with more tests. - Set the transaction log for this one test to also use the fake transasction log (same thing as #21476).
1 parent d8250d5 commit 6bcba9d

File tree

5 files changed

+102
-71
lines changed

5 files changed

+102
-71
lines changed

crates/bevy_asset/src/asset_changed.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,14 @@ unsafe impl<A: AsAssetId> QueryFilter for AssetChanged<A> {
288288
#[cfg(test)]
289289
#[expect(clippy::print_stdout, reason = "Allowed in tests.")]
290290
mod tests {
291-
use crate::{AssetEventSystems, AssetPlugin, Handle};
291+
use crate::tests::create_app;
292+
use crate::{AssetEventSystems, Handle};
292293
use alloc::{vec, vec::Vec};
293294
use core::num::NonZero;
294295
use std::println;
295296

296297
use crate::{AssetApp, Assets};
297-
use bevy_app::{App, AppExit, PostUpdate, Startup, TaskPoolPlugin, Update};
298+
use bevy_app::{App, AppExit, PostUpdate, Startup, Update};
298299
use bevy_ecs::schedule::IntoScheduleConfigs;
299300
use bevy_ecs::{
300301
component::Component,
@@ -321,10 +322,8 @@ mod tests {
321322
}
322323

323324
fn run_app<Marker>(system: impl IntoSystem<(), (), Marker>) {
324-
let mut app = App::new();
325-
app.add_plugins((TaskPoolPlugin::default(), AssetPlugin::default()))
326-
.init_asset::<MyAsset>()
327-
.add_systems(Update, system);
325+
let mut app = create_app().0;
326+
app.init_asset::<MyAsset>().add_systems(Update, system);
328327
app.update();
329328
}
330329

@@ -405,10 +404,9 @@ mod tests {
405404

406405
#[test]
407406
fn added() {
408-
let mut app = App::new();
407+
let mut app = create_app().0;
409408

410-
app.add_plugins((TaskPoolPlugin::default(), AssetPlugin::default()))
411-
.init_asset::<MyAsset>()
409+
app.init_asset::<MyAsset>()
412410
.insert_resource(Counter(vec![0, 0, 0, 0]))
413411
.add_systems(Update, add_some)
414412
.add_systems(PostUpdate, count_update.after(AssetEventSystems));
@@ -428,10 +426,9 @@ mod tests {
428426

429427
#[test]
430428
fn changed() {
431-
let mut app = App::new();
429+
let mut app = create_app().0;
432430

433-
app.add_plugins((TaskPoolPlugin::default(), AssetPlugin::default()))
434-
.init_asset::<MyAsset>()
431+
app.init_asset::<MyAsset>()
435432
.insert_resource(Counter(vec![0, 0]))
436433
.add_systems(
437434
Startup,

crates/bevy_asset/src/handle.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ mod tests {
542542
use core::hash::BuildHasher;
543543
use uuid::Uuid;
544544

545+
use crate::tests::create_app;
546+
545547
use super::*;
546548

547549
type TestAsset = ();
@@ -646,8 +648,7 @@ mod tests {
646648
/// `PartialReflect::reflect_clone`/`PartialReflect::to_dynamic` should increase the strong count of a strong handle
647649
#[test]
648650
fn strong_handle_reflect_clone() {
649-
use crate::{AssetApp, AssetPlugin, Assets, VisitAssetDependencies};
650-
use bevy_app::App;
651+
use crate::{AssetApp, Assets, VisitAssetDependencies};
651652
use bevy_reflect::FromReflect;
652653

653654
#[derive(Reflect)]
@@ -659,9 +660,8 @@ mod tests {
659660
fn visit_dependencies(&self, _visit: &mut impl FnMut(UntypedAssetId)) {}
660661
}
661662

662-
let mut app = App::new();
663-
app.add_plugins(AssetPlugin::default())
664-
.init_asset::<MyAsset>();
663+
let mut app = create_app().0;
664+
app.init_asset::<MyAsset>();
665665
let mut assets = app.world_mut().resource_mut::<Assets<MyAsset>>();
666666

667667
let handle: Handle<MyAsset> = assets.add(MyAsset { value: 1 });

crates/bevy_asset/src/lib.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ mod tests {
905905
}
906906

907907
/// Creates a basic asset app and an in-memory file system.
908-
fn create_app() -> (App, Dir) {
908+
pub(crate) fn create_app() -> (App, Dir) {
909909
let mut app = App::new();
910910
let dir = Dir::default();
911911
let dir_clone = dir.clone();
@@ -919,7 +919,11 @@ mod tests {
919919
)
920920
.add_plugins((
921921
TaskPoolPlugin::default(),
922-
AssetPlugin::default(),
922+
AssetPlugin {
923+
watch_for_changes_override: Some(false),
924+
use_asset_processor_override: Some(false),
925+
..Default::default()
926+
},
923927
DiagnosticsPlugin,
924928
));
925929
(app, dir)
@@ -934,7 +938,11 @@ mod tests {
934938
)
935939
.add_plugins((
936940
TaskPoolPlugin::default(),
937-
AssetPlugin::default(),
941+
AssetPlugin {
942+
watch_for_changes_override: Some(false),
943+
use_asset_processor_override: Some(false),
944+
..Default::default()
945+
},
938946
DiagnosticsPlugin,
939947
));
940948
(app, gate_opener)
@@ -1868,10 +1876,27 @@ mod tests {
18681876

18691877
let mut app = App::new();
18701878
app.register_asset_source(
1879+
AssetSourceId::Default,
1880+
AssetSourceBuilder::new(move || {
1881+
// This reader is unused, but we set it here so we don't accidentally use the
1882+
// filesystem.
1883+
Box::new(MemoryAssetReader {
1884+
root: Dir::default(),
1885+
})
1886+
}),
1887+
)
1888+
.register_asset_source(
18711889
"unstable",
18721890
AssetSourceBuilder::new(move || Box::new(unstable_reader.clone())),
18731891
)
1874-
.add_plugins((TaskPoolPlugin::default(), AssetPlugin::default()))
1892+
.add_plugins((
1893+
TaskPoolPlugin::default(),
1894+
AssetPlugin {
1895+
watch_for_changes_override: Some(false),
1896+
use_asset_processor_override: Some(false),
1897+
..Default::default()
1898+
},
1899+
))
18751900
.init_asset::<CoolText>()
18761901
.register_asset_loader(CoolTextLoader)
18771902
.init_resource::<ErrorTracker>()
@@ -2060,6 +2085,8 @@ mod tests {
20602085
TaskPoolPlugin::default(),
20612086
AssetPlugin {
20622087
unapproved_path_mode: mode,
2088+
watch_for_changes_override: Some(false),
2089+
use_asset_processor_override: Some(false),
20632090
..Default::default()
20642091
},
20652092
));
@@ -2302,6 +2329,7 @@ mod tests {
23022329
TaskPoolPlugin::default(),
23032330
AssetPlugin {
23042331
watch_for_changes_override: Some(true),
2332+
use_asset_processor_override: Some(false),
23052333
..Default::default()
23062334
},
23072335
));

crates/bevy_asset/src/processor/tests.rs

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,55 @@ fn serialize_as_cool_text(text: &str) -> String {
237237
ron::ser::to_string_pretty(&cool_text_ron, PrettyConfig::new().new_line("\n")).unwrap()
238238
}
239239

240+
/// Sets the transaction log for the app to a fake one to prevent touching the filesystem.
241+
fn set_fake_transaction_log(app: &mut App) {
242+
/// A dummy transaction log factory that just creates [`FakeTransactionLog`].
243+
struct FakeTransactionLogFactory;
244+
245+
impl ProcessorTransactionLogFactory for FakeTransactionLogFactory {
246+
fn read(&self) -> BoxedFuture<'_, Result<Vec<LogEntry>, BevyError>> {
247+
Box::pin(async move { Ok(vec![]) })
248+
}
249+
250+
fn create_new_log(
251+
&self,
252+
) -> BoxedFuture<'_, Result<Box<dyn ProcessorTransactionLog>, BevyError>> {
253+
Box::pin(async move { Ok(Box::new(FakeTransactionLog) as _) })
254+
}
255+
}
256+
257+
/// A dummy transaction log that just drops every log.
258+
// TODO: In the future it's possible for us to have a test of the transaction log, so making
259+
// this more complex may be necessary.
260+
struct FakeTransactionLog;
261+
262+
impl ProcessorTransactionLog for FakeTransactionLog {
263+
fn begin_processing<'a>(
264+
&'a mut self,
265+
_asset: &'a AssetPath<'_>,
266+
) -> BoxedFuture<'a, Result<(), BevyError>> {
267+
Box::pin(async move { Ok(()) })
268+
}
269+
270+
fn end_processing<'a>(
271+
&'a mut self,
272+
_asset: &'a AssetPath<'_>,
273+
) -> BoxedFuture<'a, Result<(), BevyError>> {
274+
Box::pin(async move { Ok(()) })
275+
}
276+
277+
fn unrecoverable(&mut self) -> BoxedFuture<'_, Result<(), BevyError>> {
278+
Box::pin(async move { Ok(()) })
279+
}
280+
}
281+
282+
app.world()
283+
.resource::<AssetProcessor>()
284+
.data()
285+
.set_log_factory(Box::new(FakeTransactionLogFactory))
286+
.unwrap();
287+
}
288+
240289
fn create_app_with_asset_processor(extra_sources: &[String]) -> AppWithProcessor {
241290
let mut app = App::new();
242291
let source_gate = Arc::new(RwLock::new(()));
@@ -332,51 +381,7 @@ fn create_app_with_asset_processor(extra_sources: &[String]) -> AppWithProcessor
332381
},
333382
));
334383

335-
/// A dummy transaction log factory that just creates [`FakeTransactionLog`].
336-
struct FakeTransactionLogFactory;
337-
338-
impl ProcessorTransactionLogFactory for FakeTransactionLogFactory {
339-
fn read(&self) -> BoxedFuture<'_, Result<Vec<LogEntry>, BevyError>> {
340-
Box::pin(async move { Ok(vec![]) })
341-
}
342-
343-
fn create_new_log(
344-
&self,
345-
) -> BoxedFuture<'_, Result<Box<dyn ProcessorTransactionLog>, BevyError>> {
346-
Box::pin(async move { Ok(Box::new(FakeTransactionLog) as _) })
347-
}
348-
}
349-
350-
/// A dummy transaction log that just drops every log.
351-
// TODO: In the future it's possible for us to have a test of the transaction log, so making
352-
// this more complex may be necessary.
353-
struct FakeTransactionLog;
354-
355-
impl ProcessorTransactionLog for FakeTransactionLog {
356-
fn begin_processing<'a>(
357-
&'a mut self,
358-
_asset: &'a AssetPath<'_>,
359-
) -> BoxedFuture<'a, Result<(), BevyError>> {
360-
Box::pin(async move { Ok(()) })
361-
}
362-
363-
fn end_processing<'a>(
364-
&'a mut self,
365-
_asset: &'a AssetPath<'_>,
366-
) -> BoxedFuture<'a, Result<(), BevyError>> {
367-
Box::pin(async move { Ok(()) })
368-
}
369-
370-
fn unrecoverable(&mut self) -> BoxedFuture<'_, Result<(), BevyError>> {
371-
Box::pin(async move { Ok(()) })
372-
}
373-
}
374-
375-
app.world()
376-
.resource::<AssetProcessor>()
377-
.data()
378-
.set_log_factory(Box::new(FakeTransactionLogFactory))
379-
.unwrap();
384+
set_fake_transaction_log(&mut app);
380385

381386
// Now that we've built the app, finish all the processing dirs.
382387

@@ -1675,10 +1680,13 @@ fn only_reprocesses_wrong_hash_on_startup() {
16751680
AssetPlugin {
16761681
mode: AssetMode::Processed,
16771682
use_asset_processor_override: Some(true),
1683+
watch_for_changes_override: Some(true),
16781684
..Default::default()
16791685
},
16801686
));
16811687

1688+
set_fake_transaction_log(&mut app);
1689+
16821690
app.init_asset::<CoolText>()
16831691
.init_asset::<SubText>()
16841692
.register_asset_loader(CoolTextLoader)

crates/bevy_asset/src/reflect.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ mod tests {
264264
use alloc::{string::String, vec::Vec};
265265
use core::any::TypeId;
266266

267-
use crate::{Asset, AssetApp, AssetPlugin, ReflectAsset};
268-
use bevy_app::App;
267+
use crate::{tests::create_app, Asset, AssetApp, ReflectAsset};
269268
use bevy_ecs::reflect::AppTypeRegistry;
270269
use bevy_reflect::Reflect;
271270

@@ -276,9 +275,8 @@ mod tests {
276275

277276
#[test]
278277
fn test_reflect_asset_operations() {
279-
let mut app = App::new();
280-
app.add_plugins(AssetPlugin::default())
281-
.init_asset::<AssetType>()
278+
let mut app = create_app().0;
279+
app.init_asset::<AssetType>()
282280
.register_asset_reflect::<AssetType>();
283281

284282
let reflect_asset = {

0 commit comments

Comments
 (0)