From aaea3057822ed6cd8a8e399c833936f9b8bfb9e2 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Sat, 5 Apr 2025 18:34:39 +0300 Subject: [PATCH] tests for `move_resource` + fixed bugs --- .gitignore | 3 --- fyrox-resource/.gitignore | 3 +++ fyrox-resource/src/lib.rs | 38 +++++++++++++++++++++++++++++++++-- fyrox-resource/src/manager.rs | 12 ++++++++++- 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 fyrox-resource/.gitignore diff --git a/.gitignore b/.gitignore index c86acbba3..80fffed35 100644 --- a/.gitignore +++ b/.gitignore @@ -11,9 +11,6 @@ /fyrox-core/test.txt /fyrox-resource/test.txt /fyrox-core-derive/test_output -/fyrox-resource/test_output -/fyrox-resource/test_output1 -/fyrox-resource/test_output2 /fyrox-impl/test_output history.bin settings.ron diff --git a/fyrox-resource/.gitignore b/fyrox-resource/.gitignore new file mode 100644 index 000000000..b6e7fd694 --- /dev/null +++ b/fyrox-resource/.gitignore @@ -0,0 +1,3 @@ +/test_output1 +/test_output2 +/test_output3 \ No newline at end of file diff --git a/fyrox-resource/src/lib.rs b/fyrox-resource/src/lib.rs index 1209a0f8c..69f19fff7 100644 --- a/fyrox-resource/src/lib.rs +++ b/fyrox-resource/src/lib.rs @@ -674,8 +674,8 @@ mod tests { ResourceData, }; use fyrox_core::{ - futures::executor::block_on, io::FileError, parking_lot::Mutex, reflect::prelude::*, - task::TaskPool, uuid, visitor::prelude::*, TypeUuidProvider, Uuid, + append_extension, futures::executor::block_on, io::FileError, parking_lot::Mutex, + reflect::prelude::*, task::TaskPool, uuid, visitor::prelude::*, TypeUuidProvider, Uuid, }; use ron::ser::PrettyConfig; use serde::{Deserialize, Serialize}; @@ -762,6 +762,7 @@ mod tests { const TEST_FOLDER1: &'static str = "./test_output1"; const TEST_FOLDER2: &'static str = "./test_output2"; + const TEST_FOLDER3: &'static str = "./test_output3"; fn make_file_path(root: &str, n: usize) -> PathBuf { Path::new(root).join(format!("test{n}.{}", MyDataLoader::EXT)) @@ -828,4 +829,37 @@ mod tests { assert_eq!(block_on(res1).unwrap().data_ref().data, 2); assert_eq!(block_on(res2).unwrap().data_ref().data, 3); } + + #[test] + fn test_move_resource() { + write_test_resources(TEST_FOLDER3, 0..2); + let resource_manager = ResourceManager::new(Arc::new(TaskPool::new())); + resource_manager.add_loader(MyDataLoader {}); + resource_manager + .update_and_load_registry(Path::new(TEST_FOLDER3).join("resources.registry")); + let path1 = make_file_path(TEST_FOLDER3, 0); + let path2 = make_file_path(TEST_FOLDER3, 1); + let res1 = resource_manager.request::(path1); + let res2 = resource_manager.request::(path2); + assert_eq!(block_on(res1.clone()).unwrap().data_ref().data, 0); + assert_eq!(block_on(res2.clone()).unwrap().data_ref().data, 1); + let new_res1_path = ResourceRegistry::prepare_path(make_file_path(TEST_FOLDER3, 3)); + let new_res2_path = ResourceRegistry::prepare_path(make_file_path(TEST_FOLDER3, 4)); + block_on(resource_manager.move_resource(res1.as_ref(), &new_res1_path)).unwrap(); + block_on(resource_manager.move_resource(res2.as_ref(), &new_res2_path)).unwrap(); + assert_eq!( + resource_manager.resource_path(res1.as_ref()).unwrap(), + new_res1_path + ); + assert!( + std::fs::exists(append_extension(new_res1_path, ResourceMetadata::EXTENSION)).unwrap() + ); + assert_eq!( + resource_manager.resource_path(res2.as_ref()).unwrap(), + new_res2_path + ); + assert!( + std::fs::exists(append_extension(new_res2_path, ResourceMetadata::EXTENSION)).unwrap() + ); + } } diff --git a/fyrox-resource/src/manager.rs b/fyrox-resource/src/manager.rs index ea4a2433f..9f3064031 100644 --- a/fyrox-resource/src/manager.rs +++ b/fyrox-resource/src/manager.rs @@ -418,7 +418,7 @@ impl ResourceManager { /// Attempts to move a resource from its current location to the new path. pub async fn move_resource( &self, - resource: UntypedResource, + resource: &UntypedResource, new_path: impl AsRef, ) -> Result<(), FileError> { let resource_uuid = resource @@ -437,6 +437,14 @@ impl ResourceManager { // Move the file with its optional import options and mandatory metadata. io.move_file(&existing_path, &new_path).await?; + assert_eq!( + registry + .lock() + .register(resource_uuid, new_path.clone()) + .as_ref(), + Some(&existing_path) + ); + let options_path = append_extension(&existing_path, OPTIONS_EXTENSION); if io.exists(&options_path).await { let new_options_path = append_extension(&new_path, OPTIONS_EXTENSION); @@ -487,9 +495,11 @@ impl ResourceManagerState { let resource_io = self.resource_io.clone(); let resource_registry = self.resource_registry.clone(); + #[allow(unused_variables)] let excluded_folders = resource_registry.lock().excluded_folders.clone(); let registry_status = resource_registry.lock().status.clone(); registry_status.mark_as_loading(); + #[allow(unused_variables)] let task_loaders = self.loaders.clone(); self.task_pool.spawn_task(async move { // Try to update the registry first.