diff --git a/crates/starknet_class_manager/Cargo.toml b/crates/starknet_class_manager/Cargo.toml index 2a8e9102f7f..1536fd7dbb0 100644 --- a/crates/starknet_class_manager/Cargo.toml +++ b/crates/starknet_class_manager/Cargo.toml @@ -5,6 +5,9 @@ license.workspace = true repository.workspace = true version.workspace = true +[features] +testing = [] + [lints] workspace = true diff --git a/crates/starknet_class_manager/src/lib.rs b/crates/starknet_class_manager/src/lib.rs index dfc2d947a6e..e93b55009e9 100644 --- a/crates/starknet_class_manager/src/lib.rs +++ b/crates/starknet_class_manager/src/lib.rs @@ -21,3 +21,6 @@ impl Clone for FsClassManager { } pub use FsClassManager as ClassManager; + +#[cfg(any(feature = "testing", test))] +pub mod test_utils; diff --git a/crates/starknet_class_manager/src/test_utils.rs b/crates/starknet_class_manager/src/test_utils.rs new file mode 100644 index 00000000000..c34bb3de982 --- /dev/null +++ b/crates/starknet_class_manager/src/test_utils.rs @@ -0,0 +1,52 @@ +use std::path::PathBuf; + +use tempfile::TempDir; + +use crate::class_storage::{ClassHashStorage, FsClassStorage}; +use crate::config::{ClassHashStorageConfig, FsClassStorageConfig}; + +pub type FileHandles = (TempDir, TempDir); + +pub struct FsClassStorageBuilderForTesting { + config: FsClassStorageConfig, + handles: Option, +} + +impl Default for FsClassStorageBuilderForTesting { + fn default() -> Self { + let class_hash_storage_handle = tempfile::tempdir().unwrap(); + let persistent_root_handle = tempfile::tempdir().unwrap(); + let persistent_root = persistent_root_handle.path().to_path_buf(); + let config = FsClassStorageConfig { + persistent_root, + class_hash_storage_config: ClassHashStorageConfig { + path_prefix: class_hash_storage_handle.path().to_path_buf(), + enforce_file_exists: false, + max_size: 1 << 20, // 1MB. + }, + }; + Self { config, handles: Some((class_hash_storage_handle, persistent_root_handle)) } + } +} + +impl FsClassStorageBuilderForTesting { + pub fn with_existing_paths( + mut self, + class_hash_storage_path_prefix: PathBuf, + persistent_path: PathBuf, + ) -> Self { + self.config.class_hash_storage_config.path_prefix = class_hash_storage_path_prefix; + self.config.persistent_root = persistent_path; + self.handles = None; + self + } + + pub fn build(self) -> (FsClassStorage, FsClassStorageConfig, Option) { + let Self { config, handles } = self; + let class_hash_storage = + ClassHashStorage::new(config.class_hash_storage_config.clone()).unwrap(); + let fs_class_storage = + FsClassStorage { persistent_root: config.persistent_root.clone(), class_hash_storage }; + (fs_class_storage, config, handles) + } +}