Skip to content

Commit 295b7c4

Browse files
authored
Merge pull request #488 from input-output-hk/jpraynaud/fix-signer-auto-create-stores-dir
Fix auto create 'stores' directory in Signer
2 parents d1c90c4 + d9cadca commit 295b7c4

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

mithril-signer/src/runtime/signer_services.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::error::Error as StdError;
2+
use std::fs;
23
use std::sync::Arc;
34

45
use mithril_common::{
@@ -44,6 +45,11 @@ impl<'a> ProductionServiceBuilder<'a> {
4445
impl<'a> ServiceBuilder for ProductionServiceBuilder<'a> {
4546
/// Build a Services for the Production environment.
4647
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+
4753
let protocol_initializer_store = Arc::new(ProtocolInitializerStore::new(Box::new(
4854
JsonFileStoreAdapter::new(
4955
self.config
@@ -112,3 +118,44 @@ pub struct SignerServices {
112118
/// ProtocolInitializer store
113119
pub protocol_initializer_store: ProtocolInitializerStoreService,
114120
}
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

Comments
 (0)