Skip to content

Commit

Permalink
fixing comments from review2
Browse files Browse the repository at this point in the history
  • Loading branch information
czarte committed Jan 4, 2024
1 parent d50c740 commit 2606ebd
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 154 deletions.
1 change: 0 additions & 1 deletion masq_lib/src/multi_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ impl<'a> MultiConfig<'a> {
#[cfg(test)]
pub mod tests {
use super::*;
// use crate::shared_schema::official_chain_names;
use crate::test_utils::environment_guard::EnvironmentGuard;
use crate::test_utils::utils::ensure_node_home_directory_exists;
use clap::Arg;
Expand Down
9 changes: 8 additions & 1 deletion masq_lib/src/test_utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::blockchains::chains::Chain;
use crate::test_utils::environment_guard::EnvironmentGuard;
use std::env::current_dir;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Duration;
Expand All @@ -12,7 +13,13 @@ pub const BASE_TEST_DIR: &str = "generated/test";
const MASQ_SOURCE_CODE_UNAVAILABLE: &str = "MASQ_SOURCE_CODE_UNAVAILABLE";

pub fn node_home_directory(module: &str, name: &str) -> PathBuf {
let home_dir_string = format!("{}/{}/{}/home", BASE_TEST_DIR, module, name);
let home_dir_string = format!(
"{}/{}/{}/{}/home",
current_dir().expect("expected current dir").display(),
BASE_TEST_DIR,
module,
name
);
PathBuf::from(home_dir_string.as_str())
}

Expand Down
93 changes: 33 additions & 60 deletions node/src/node_configurator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ fn get_real_user_from_mc(
multi_config: &MultiConfig,
dirs_wrapper: &dyn DirsWrapper,
) -> FieldPair<RealUser> {
let rel_user = value_m!(multi_config, "real-user", RealUser);
match rel_user {
let real_user = value_m!(multi_config, "real-user", RealUser);
match real_user {
Some(user) => FieldPair::new(user, true),
None => {
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -119,57 +119,49 @@ fn get_data_directory_from_mc(
}
}

fn replace_tilde(config_path: &Path, dirs_wrapper: &dyn DirsWrapper) -> Option<PathBuf> {
fn replace_tilde<'a>(config_path: PathBuf, dirs_wrapper: &'a dyn DirsWrapper) -> PathBuf {
match config_path.starts_with("~") {
true => {
let home_dir_from_wrapper = dirs_wrapper
.home_dir()
.expect("expected users home_dir")
.to_str()
.expect("expected str home_dir")
.to_string();
Some(PathBuf::from(config_path.display().to_string().replacen(
'~',
home_dir_from_wrapper.as_str(),
1,
)))
let home_dir_from_wrapper = dirs_wrapper.home_dir();
PathBuf::from(
config_path.display().to_string().replacen(
'~',
home_dir_from_wrapper
.expect("expected users home_dir")
.to_str()
.expect("expected str home_dir"),
1,
),
)
}
false => None,
false => config_path,
}
}

fn replace_dots(config_path: &Path, panic: &mut bool) -> Option<PathBuf> {
fn replace_dots(config_path: PathBuf) -> PathBuf {
match config_path.starts_with("./") || config_path.starts_with("../") {
true => {
*panic = false;
Some(
current_dir()
.expect("expected current dir")
.join(config_path),
)
}
false => None,
true => current_dir()
.expect("expected current dir")
.join(config_path),
false => config_path,
}
}

fn replace_relative_path(
config_path: &Path,
fn replace_relative_path<'a>(
config_path: PathBuf,
data_directory_def: bool,
data_directory: &Path,
panic: &mut bool,
) -> Option<PathBuf> {
) -> PathBuf {
match config_path.is_relative() {
true => match data_directory_def {
true => {
*panic = false;
Some(data_directory.join(config_path))
}
true => data_directory.join(config_path),
false => {
*panic = true;
Some(config_path.to_path_buf())
config_path
}
},
false => None,
false => config_path,
}
}

Expand All @@ -183,36 +175,17 @@ fn get_config_file_from_mc(
let config_file = value_m!(multi_config, "config-file", PathBuf);
match config_file {
Some(config_path) => {
let config_file_pth_tilde = replace_tilde(&config_path, dirs_wrapper);
let config_file_pth_dot = replace_dots(&config_path, &mut panic);
let config_file_pth_relative =
replace_relative_path(&config_path, data_directory_def, data_directory, &mut panic);
let config_file_pth = match config_file_pth_tilde {
Some(path) => {
panic = false;
path
}
None => match config_file_pth_dot {
Some(config_path_dot) => {
panic = false;
config_path_dot
}
None => match config_file_pth_relative {
Some(path_rel) => path_rel,
None => {
panic = false;
config_path
}
},
},
};
let config_path = replace_tilde(config_path, dirs_wrapper);
let config_path = replace_dots(config_path);
let config_path =
replace_relative_path(config_path, data_directory_def, data_directory, &mut panic);
if panic {
panic!(
"You need to define data-directory to define config file with naked directory {}.",
config_file_pth.to_string_lossy()
"If the config file is given with a naked relative path ({}), the data directory must be given to serve as the root for the config-file path.",
config_path.to_string_lossy()
);
}
FieldPair::new(config_file_pth, true)
FieldPair::new(config_path.to_path_buf(), true)
}
None => {
let path = data_directory.join(PathBuf::from("config.toml"));
Expand Down
Loading

0 comments on commit 2606ebd

Please sign in to comment.