|
1 | 1 | use std::error::Error as StdError;
|
| 2 | +use std::fs; |
2 | 3 | use std::sync::Arc;
|
3 | 4 |
|
4 | 5 | use mithril_common::{
|
@@ -44,6 +45,11 @@ impl<'a> ProductionServiceBuilder<'a> {
|
44 | 45 | impl<'a> ServiceBuilder for ProductionServiceBuilder<'a> {
|
45 | 46 | /// Build a Services for the Production environment.
|
46 | 47 | fn build(&self) -> Result<SignerServices, Box<dyn StdError>> {
|
| 48 | + if !self.config.data_stores_directory.exists() { |
| 49 | + fs::create_dir_all(self.config.data_stores_directory.clone()) |
| 50 | + .map_err(|e| format!("Could not create data stores directory: {:?}", e))?; |
| 51 | + } |
| 52 | + |
47 | 53 | let protocol_initializer_store = Arc::new(ProtocolInitializerStore::new(Box::new(
|
48 | 54 | JsonFileStoreAdapter::new(
|
49 | 55 | self.config
|
@@ -112,3 +118,44 @@ pub struct SignerServices {
|
112 | 118 | /// ProtocolInitializer store
|
113 | 119 | pub protocol_initializer_store: ProtocolInitializerStoreService,
|
114 | 120 | }
|
| 121 | + |
| 122 | +#[cfg(test)] |
| 123 | +mod tests { |
| 124 | + use super::*; |
| 125 | + |
| 126 | + use std::path::PathBuf; |
| 127 | + |
| 128 | + fn get_test_dir() -> PathBuf { |
| 129 | + let test_dir = std::env::temp_dir().join("mithril_test"); |
| 130 | + |
| 131 | + if test_dir.exists() { |
| 132 | + fs::remove_dir_all(&test_dir).expect(&*format!("Could not remove dir {:?}", test_dir)); |
| 133 | + } |
| 134 | + fs::create_dir_all(&test_dir).expect(&*format!("Could not create dir {:?}", test_dir)); |
| 135 | + |
| 136 | + test_dir |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_auto_create_stores_directory() { |
| 141 | + let stores_dir = get_test_dir().join("stores"); |
| 142 | + let config = Config { |
| 143 | + cardano_cli_path: PathBuf::new(), |
| 144 | + cardano_node_socket_path: PathBuf::new(), |
| 145 | + network_magic: None, |
| 146 | + network: "preview".to_string(), |
| 147 | + aggregator_endpoint: "".to_string(), |
| 148 | + party_id: "party-123456".to_string(), |
| 149 | + run_interval: 1000, |
| 150 | + db_directory: PathBuf::new(), |
| 151 | + data_stores_directory: stores_dir.clone(), |
| 152 | + }; |
| 153 | + |
| 154 | + assert!(!stores_dir.clone().exists()); |
| 155 | + let service_builder = ProductionServiceBuilder::new(&config); |
| 156 | + service_builder |
| 157 | + .build() |
| 158 | + .expect("service builder build should not fail"); |
| 159 | + assert!(stores_dir.exists()); |
| 160 | + } |
| 161 | +} |
0 commit comments