diff --git a/node/src/daemon/setup_reporter.rs b/node/src/daemon/setup_reporter.rs index e83a522bf..b8473b05d 100644 --- a/node/src/daemon/setup_reporter.rs +++ b/node/src/daemon/setup_reporter.rs @@ -1216,6 +1216,7 @@ mod tests { }; use crate::test_utils::{assert_string_contains, rate_pack}; use core::option::Option; + use dirs::home_dir; use masq_lib::blockchains::chains::Chain as Blockchain; use masq_lib::blockchains::chains::Chain::PolyMumbai; use masq_lib::constants::{DEFAULT_CHAIN, DEFAULT_GAS_PRICE}; @@ -2031,6 +2032,58 @@ mod tests { assert_eq!(actual_data_directory, expected_data_directory); } + #[test] + fn get_modified_setup_tilde_in_config_file_path() { + let _guard = EnvironmentGuard::new(); + let base_dir = ensure_node_home_directory_exists( + "setup_reporter", + "get_modified_setup_tilde_in_data_directory", + ); + let data_dir = base_dir.join("data_dir"); + std::fs::create_dir_all(home_dir().expect("expect home dir").join("masqhome")).unwrap(); + let mut config_file = File::create( + home_dir() + .expect("expect home dir") + .join("masqhome") + .join("config.toml"), + ) + .unwrap(); + config_file + .write_all(b"blockchain-service-url = \"https://www.mainnet.com\"\n") + .unwrap(); + let existing_setup = setup_cluster_from(vec![ + ("neighborhood-mode", "zero-hop", Set), + ("chain", DEFAULT_CHAIN.rec().literal_identifier, Default), + ( + "data-directory", + &data_dir.to_string_lossy().to_string(), + Default, + ), + ]); + let incoming_setup = vec![ + ("data-directory", "~/masqhome"), + ("config-file", "~/masqhome/config.toml"), + ] + .into_iter() + .map(|(name, value)| UiSetupRequestValue::new(name, value)) + .collect_vec(); + + let expected_config_file_data = "https://www.mainnet.com"; + let dirs_wrapper = Box::new( + DirsWrapperMock::new() + .data_dir_result(Some(data_dir)) + .home_dir_result(Some(base_dir)), + ); + let subject = SetupReporterReal::new(dirs_wrapper); + + let result = subject + .get_modified_setup(existing_setup, incoming_setup) + .unwrap(); + + let actual_config_file_data = result.get("blockchain-service-url").unwrap().value.as_str(); + assert_eq!(actual_config_file_data, expected_config_file_data); + } + #[test] fn get_modified_setup_user_specified_data_directory_depends_on_new_chain_on_success() { let _guard = EnvironmentGuard::new(); diff --git a/node/src/node_configurator/mod.rs b/node/src/node_configurator/mod.rs index ce002c7b6..a45a2969a 100644 --- a/node/src/node_configurator/mod.rs +++ b/node/src/node_configurator/mod.rs @@ -119,21 +119,18 @@ fn get_data_directory_from_mc( } } -fn replace_tilde(config_path: PathBuf, dirs_wrapper: &dyn DirsWrapper) -> PathBuf { +fn replace_tilde(config_path: PathBuf) -> PathBuf { match config_path.starts_with("~") { - true => { - 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, - ), - ) - } + true => PathBuf::from( + config_path.display().to_string().replacen( + '~', + home_dir() + .expect("expected users home_dir") + .to_str() + .expect("expected str home_dir"), + 1, + ), + ), false => config_path, } } @@ -169,13 +166,12 @@ fn get_config_file_from_mc( multi_config: &MultiConfig, data_directory: &Path, data_directory_def: bool, - dirs_wrapper: &dyn DirsWrapper, ) -> FieldPair { let mut panic: bool = false; let config_file = value_m!(multi_config, "config-file", PathBuf); match config_file { Some(config_path) => { - let config_path = replace_tilde(config_path, dirs_wrapper); + let config_path = replace_tilde(config_path); let config_path = replace_dots(config_path); let config_path = replace_relative_path(config_path, data_directory_def, data_directory, &mut panic); @@ -220,7 +216,6 @@ fn config_file_data_dir_real_user_chain_from_mc( &multi_config, &initialization_data.data_directory.item, initialization_data.data_directory.user_specified, - dirs_wrapper, ); initialization_data } diff --git a/node/src/node_configurator/node_configurator_standard.rs b/node/src/node_configurator/node_configurator_standard.rs index f2b197791..639c83556 100644 --- a/node/src/node_configurator/node_configurator_standard.rs +++ b/node/src/node_configurator/node_configurator_standard.rs @@ -376,6 +376,7 @@ mod tests { make_pre_populated_mocked_directory_wrapper, make_simplified_multi_config, }; use crate::test_utils::{assert_string_contains, main_cryptde, ArgsBuilder}; + use dirs::home_dir; use masq_lib::blockchains::chains::Chain; use masq_lib::constants::DEFAULT_CHAIN; use masq_lib::multi_config::VirtualCommandLine; @@ -1075,7 +1076,7 @@ mod tests { running_test(); let _guard = EnvironmentGuard::new(); let _clap_guard = ClapGuard::new(); - let home_dir = ensure_node_home_directory_exists( "node_configurator_standard","server_initializer_collected_params_handle_tilde_in_path_config_file_from_commandline_and_real_user_from_config_file"); + let home_dir = home_dir().expect("expectexd home dir"); let home_dir = canonicalize(home_dir).unwrap(); let data_dir = &home_dir.join("masqhome"); let _create_data_dir = create_dir_all(data_dir);