Skip to content

Commit

Permalink
fix tilde for windows - cherry pick from master
Browse files Browse the repository at this point in the history
  • Loading branch information
czarte committed Dec 20, 2024
1 parent 7270bbf commit 9ab9911
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 97 deletions.
53 changes: 22 additions & 31 deletions node/src/daemon/setup_reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,8 +1231,7 @@ mod tests {
make_pre_populated_mocked_directory_wrapper, make_simplified_multi_config,
};
use crate::test_utils::{
assert_string_contains,
make_node_base_dir_and_return_its_absolute_and_relative_path_to_os_home_dir, rate_pack,
assert_string_contains, rate_pack,
};
use core::option::Option;
use masq_lib::blockchains::chains::Chain as Blockchain;
Expand All @@ -1247,6 +1246,7 @@ mod tests {
use std::convert::TryFrom;
#[cfg(not(target_os = "windows"))]
use std::default::Default;
use std::env::current_dir;
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::net::IpAddr;
Expand Down Expand Up @@ -2054,18 +2054,16 @@ mod tests {
}

#[test]
fn get_modified_setup_tilde_in_config_file_path() {
fn get_modified_setup_handles_tilde_in_config_file_and_data_directory_path() {
let _guard = EnvironmentGuard::new();
let (node_base_dir, node_base_dir_relative_to_os_home_dir) =
make_node_base_dir_and_return_its_absolute_and_relative_path_to_os_home_dir(
"setup_reporter",
"get_modified_setup_tilde_in_config_file_path",
);
let existing_data_dir = node_base_dir.join("obsolete_data_dir");
let new_dir_levels = PathBuf::new().join("whatever_dir").join("new_data_dir");
let new_data_dir = node_base_dir.join(new_dir_levels.as_path());
create_dir_all(new_data_dir.as_path()).unwrap();
let mut config_file = File::create(new_data_dir.join("config.toml")).unwrap();
let base_dir = ensure_node_home_directory_exists(
"setup_reporter",
"get_modified_setup_handles_tilde_in_config_file_and_data_directory_path",
);
let data_dir = base_dir.join("data_dir");
std::fs::create_dir_all(base_dir.join("masqhome")).unwrap();
let config_file_path = base_dir.join("masqhome").join("config.toml");
let mut config_file = File::create(&config_file_path).unwrap();
config_file
.write_all(b"blockchain-service-url = \"https://www.mainnet.com\"\n")
.unwrap();
Expand All @@ -2074,38 +2072,31 @@ mod tests {
("chain", DEFAULT_CHAIN.rec().literal_identifier, Default),
(
"data-directory",
&existing_data_dir.to_string_lossy().to_string(),
&data_dir.to_string_lossy().to_string(),
Default,
),
]);
let data_dir_referenced_from_the_home_dir = node_base_dir_relative_to_os_home_dir
.join(new_dir_levels)
.as_os_str()
.to_str()
.unwrap()
.to_string();
let incoming_setup = vec![
(
"data-directory",
&format!("~/{}", data_dir_referenced_from_the_home_dir),
),
(
"config-file",
&format!("~/{}/config.toml", data_dir_referenced_from_the_home_dir),
),
("data-directory", "~/masqhome"),
("config-file", "~/masqhome/config.toml"),
]
.into_iter()
.map(|(name, value)| UiSetupRequestValue::new(name, value))
.collect_vec();
let dirs_wrapper = Box::new(DirsWrapperReal::default());
let subject = SetupReporterReal::new(dirs_wrapper);

let expected_config_file_data = "https://www.mainnet.com";
let dirs_wrapper = DirsWrapperMock {
data_dir_result: Some(PathBuf::from(current_dir().unwrap().join(&data_dir))),
home_dir_result: Some(PathBuf::from(current_dir().unwrap().join(&base_dir))),
};
let subject = SetupReporterReal::new(Box::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, "https://www.mainnet.com");
assert_eq!(actual_config_file_data, expected_config_file_data);
}

#[test]
Expand Down
86 changes: 34 additions & 52 deletions node/src/node_configurator/node_configurator_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ mod tests {
make_pre_populated_mocked_directory_wrapper, make_simplified_multi_config,
};
use crate::test_utils::{
assert_string_contains, main_cryptde,
make_node_base_dir_and_return_its_absolute_and_relative_path_to_os_home_dir, ArgsBuilder,
assert_string_contains, main_cryptde, ArgsBuilder,
};
use masq_lib::blockchains::chains::Chain;
use masq_lib::constants::DEFAULT_CHAIN;
Expand Down Expand Up @@ -1078,25 +1077,17 @@ mod tests {

#[test]
fn tilde_in_config_file_path_from_commandline_and_args_uploaded_from_config_file() {
//server_initializer_collected_params_handle_tilde_in_path_config_file_from_commandline_and_real_user_from_config_file
running_test();
let _guard = EnvironmentGuard::new();
let _clap_guard = ClapGuard::new();
let (node_home_dir, node_home_dir_relative_to_os_home_dir) =
make_node_base_dir_and_return_its_absolute_and_relative_path_to_os_home_dir(
"node_configurator_standard",
"tilde_in_config_file_path_from_commandline_and_real_user_from_config_file",
);
let data_dir = &node_home_dir.join("data_dir");
let node_data_dir_relative_to_os_home_dir_str = node_home_dir_relative_to_os_home_dir
.join("data_dir")
.as_os_str()
.to_str()
.unwrap()
.to_string();
create_dir_all(data_dir).unwrap();
let config_file = File::create(data_dir.join("config.toml")).unwrap();
fill_up_config_file(config_file);
let home_dir = ensure_node_home_directory_exists(
"node_configurator_standard",
"tilde_in_config_file_path_from_commandline_and_args_uploaded_from_config_file",
);
let data_dir = home_dir.join("masqhome");
let _dir = create_dir_all(&data_dir);
let config_file_relative = File::create(data_dir.join("config.toml")).unwrap();
fill_up_config_file(config_file_relative);
let env_vec_array = vec![
("MASQ_BLOCKCHAIN_SERVICE_URL", "https://www.mainnet2.com"),
#[cfg(not(target_os = "windows"))]
Expand All @@ -1109,40 +1100,38 @@ mod tests {
#[cfg(not(target_os = "windows"))]
let args = ArgsBuilder::new()
.param("--blockchain-service-url", "https://www.mainnet1.com")
.param(
"--config-file",
&format!(
"~/{}/config.toml",
node_data_dir_relative_to_os_home_dir_str
),
)
.param(
"--data-directory",
&format!("~/{}", node_data_dir_relative_to_os_home_dir_str),
);
.param("--config-file", "~/masqhome/config.toml")
.param("--data-directory", "~/masqhome");
#[cfg(target_os = "windows")]
let args = ArgsBuilder::new()
.param("--blockchain-service-url", "https://www.mainnet1.com")
.param(
"--config-file",
&format!(
"~\\{}\\config.toml",
node_data_dir_relative_to_os_home_dir_str
),
)
.param(
"--data-directory",
&format!("~\\{}", node_data_dir_relative_to_os_home_dir_str),
);
.param("--config-file", "~\\masqhome\\config.toml")
.param("--data-directory", "~\\masqhome");
let args_vec: Vec<String> = args.into();
let dir_wrapper = DirsWrapperMock {
data_dir_result: Some(PathBuf::from(current_dir().unwrap().join(&data_dir))),
home_dir_result: Some(PathBuf::from(current_dir().unwrap().join(&home_dir))),
};

let multiconfig =
server_initializer_collected_params(&DirsWrapperReal::default(), args_vec.as_slice())
.unwrap();
let result = server_initializer_collected_params(&dir_wrapper, args_vec.as_slice());
let multiconfig = result.unwrap();

assert_eq!(
value_m!(multiconfig, "data-directory", String).unwrap(),
data_dir.as_os_str().to_str().unwrap()
current_dir()
.unwrap()
.join(&data_dir)
.to_string_lossy()
.to_string()
);
assert_eq!(
value_m!(multiconfig, "config-file", String).unwrap(),
current_dir()
.unwrap()
.join(data_dir)
.join(PathBuf::from("config.toml"))
.to_string_lossy()
.to_string()
);
#[cfg(not(target_os = "windows"))]
{
Expand All @@ -1151,13 +1140,6 @@ mod tests {
"9999:9999:booga"
);
}
assert_eq!(
value_m!(multiconfig, "config-file", String).unwrap(),
data_dir
.join(PathBuf::from("config.toml"))
.to_string_lossy()
.to_string()
);
assert_eq!(
value_m!(multiconfig, "blockchain-service-url", String).unwrap(),
"https://www.mainnet1.com"
Expand Down Expand Up @@ -1298,7 +1280,7 @@ mod tests {
#[cfg(target_os = "windows")]
assert_eq!(
value_m!(multiconfig, "config-file", String).unwrap(),
node_home_dir.to_string_lossy().to_string()
node_home_dir.join("booga.toml").to_string_lossy().to_string()
);
}

Expand Down
14 changes: 0 additions & 14 deletions node/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,6 @@ pub struct TestRawTransaction {
pub data: Vec<u8>,
}

pub fn make_node_base_dir_and_return_its_absolute_and_relative_path_to_os_home_dir(
module: &str,
name: &str,
) -> (PathBuf, PathBuf) {
let node_base_dir_relative = ensure_node_home_directory_exists(module, name);
let home_dir_path = home_dir().unwrap();
let current_dir = current_dir().unwrap();
let current_dir_tilde_like_path = current_dir.strip_prefix(home_dir_path).unwrap();
let node_base_dir_tilde_path =
current_dir_tilde_like_path.join(node_home_directory(module, name));
let node_base_dir_absolute = current_dir.join(node_base_dir_relative);
(node_base_dir_absolute, node_base_dir_tilde_path)
}

#[macro_export]
macro_rules! arbitrary_id_stamp_in_trait {
() => {
Expand Down

0 comments on commit 9ab9911

Please sign in to comment.