Skip to content

Commit

Permalink
fix: use temp file to write config
Browse files Browse the repository at this point in the history
  • Loading branch information
ryota-sakamoto committed Oct 8, 2023
1 parent 4013e2b commit 67f2034
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
42 changes: 21 additions & 21 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use std::{
path,
str::FromStr,
};
use tempfile::NamedTempFile;

use super::control;

Expand Down Expand Up @@ -378,10 +379,8 @@ pub fn load_or_touch_config_file(first_try: bool) -> Result<Config, DyneinConfig
..Default::default()
})
.unwrap();
fs::write(
retrieve_dynein_file_path(DyneinFileType::ConfigFile)?,
yaml_string,
)?;

write_dynein_file(DyneinFileType::ConfigFile, yaml_string)?;
load_or_touch_config_file(false) // set fisrt_try flag to false in order to avoid infinite loop.
}
}
Expand All @@ -404,17 +403,14 @@ pub fn load_or_touch_cache_file(first_try: bool) -> Result<Cache, DyneinConfigEr
return Err(DyneinConfigError::from(e));
};
info!(
"Config file doesn't exist in the path, hence creating a blank file: {}",
"Cache file doesn't exist in the path, hence creating a blank file: {}",
e
);
let yaml_string = serde_yaml::to_string(&Cache {
..Default::default()
})
.unwrap();
fs::write(
retrieve_dynein_file_path(DyneinFileType::CacheFile)?,
yaml_string,
)?;
})?;

write_dynein_file(DyneinFileType::CacheFile, yaml_string)?;
load_or_touch_cache_file(false) // set fisrt_try flag to false in order to avoid infinite loop.
}
}
Expand Down Expand Up @@ -498,10 +494,7 @@ pub fn insert_to_table_cache(
"this YAML will be written to the cache file: {:#?}",
&cache_yaml_string
);
fs::write(
retrieve_dynein_file_path(DyneinFileType::CacheFile)?,
cache_yaml_string,
)?;
write_dynein_file(DyneinFileType::CacheFile, cache_yaml_string)?;

Ok(())
}
Expand Down Expand Up @@ -643,8 +636,8 @@ fn region_dynamodb_local(port: u32) -> Region {
}
}

fn retrieve_dynein_file_path(dft: DyneinFileType) -> Result<String, DyneinConfigError> {
let filename = match dft {
fn retrieve_dynein_file_path(file_type: DyneinFileType) -> Result<String, DyneinConfigError> {
let filename = match file_type {
DyneinFileType::ConfigFile => CONFIG_FILE_NAME,
DyneinFileType::CacheFile => CACHE_FILE_NAME,
};
Expand Down Expand Up @@ -691,17 +684,24 @@ fn save_using_target(cx: &mut Context, desc: TableDescription) -> Result<(), Dyn

// write to config file
let config_yaml_string = serde_yaml::to_string(config)?;
fs::write(
retrieve_dynein_file_path(DyneinFileType::ConfigFile)?,
config_yaml_string,
)?;
write_dynein_file(DyneinFileType::ConfigFile, config_yaml_string)?;

// save target table info into cache.
insert_to_table_cache(cx, desc)?;

Ok(())
}

fn write_dynein_file(file_type: DyneinFileType, content: String) -> Result<(), DyneinConfigError> {
let temp_file = NamedTempFile::new_in(retrieve_or_create_dynein_dir()?)?;
let temp_path = temp_file.path();

fs::write(temp_path, content)?;
fs::rename(temp_path, retrieve_dynein_file_path(file_type)?)?;

Ok(())
}

/* =================================================
Unit Tests
================================================= */
Expand Down
11 changes: 2 additions & 9 deletions tests/auxiliary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use assert_cmd::prelude::*; // Add methods on commands
use predicates::prelude::*; // Used for writing assertions
use std::process::Command; // Run programs
// use assert_cmd::cmd::Command; // Run programs - it seems to be equal to "use assert_cmd::prelude::* + use std::process::Command"
use std::path::Path;

#[tokio::test]
async fn test_help() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -41,23 +40,17 @@ async fn test_custom_config_location() -> Result<(), Box<dyn std::error::Error>>

// cleanup config folder in case it was already there
util::cleanup_config(dummy_dir).await.ok();
check_file_exist(&config_dir, false);
util::check_dynein_files_existence(&config_dir, false);

// run any dy command to generate default config
let mut c = tm.command()?;
c.env("DYNEIN_CONFIG_DIR", dummy_dir).assert();

// check config folder created at our desired location
check_file_exist(&config_dir, true);
util::check_dynein_files_existence(&config_dir, true);

// cleanup config folder
util::cleanup_config(dummy_dir).await.ok();

Ok(())
}

fn check_file_exist(dir: &str, exist: bool) {
assert_eq!(Path::new(&dir).exists(), exist);
assert_eq!(Path::new(&format!("{}/config.yml", dir)).exists(), exist);
assert_eq!(Path::new(&format!("{}/cache.yml", dir)).exists(), exist);
}
7 changes: 7 additions & 0 deletions tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use regex::bytes::Regex;
use rusoto_core::Region;
use rusoto_dynamodb::{DynamoDb, DynamoDbClient};
use std::io::{self, Write}; // Used when check results by printing to stdout
use std::path::Path;
use std::sync::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::time::Duration;
use tokio::time::sleep;
Expand Down Expand Up @@ -300,6 +301,12 @@ impl TemporaryItem {
}
}

pub fn check_dynein_files_existence(dir: &str, exist: bool) {
assert_eq!(Path::new(&dir).exists(), exist);
assert_eq!(Path::new(&format!("{}/config.yml", dir)).exists(), exist);
assert_eq!(Path::new(&format!("{}/cache.yml", dir)).exists(), exist);
}

pub async fn cleanup_config(dummy_dir: &str) -> io::Result<()> {
use std::fs::remove_dir_all;

Expand Down

0 comments on commit 67f2034

Please sign in to comment.