gitignore_template_generator/
test_helper.rs1use std::{ffi::OsString, fs};
2
3use crate::{
4 constant::{cli_options, error_messages, help_messages, parser_infos, path, template_manager},
5 printer::{Data, pp, ppg},
6};
7
8pub struct EnvTestContext {
9 original_value: Option<String>,
10}
11
12impl EnvTestContext {
13 fn new(original_value: &str) -> Self {
14 Self {
15 original_value: Some(original_value.to_string()),
16 }
17 }
18
19 fn empty() -> Self {
20 Self {
21 original_value: None,
22 }
23 }
24
25 fn handle_env_var_reset(original_value: &str) {
26 pp(&Data::EnvVarReset(original_value));
27 set_env_var(template_manager::HOME_ENV_VAR, original_value);
28 pp(&Data::Reset());
29 }
30
31 fn handle_env_var_removal() {
32 pp(&Data::EnvVarRemovalAfter());
33 remove_env_var(template_manager::HOME_ENV_VAR);
34 pp(&Data::Removed());
35 }
36}
37
38impl Drop for EnvTestContext {
39 fn drop(&mut self) {
40 match &self.original_value {
41 Some(original_value) => Self::handle_env_var_reset(original_value),
42 None => Self::handle_env_var_removal(),
43 }
44
45 pp(&Data::TestContextDropped());
46 }
47}
48
49pub fn create_env_test_context() -> EnvTestContext {
51 let ctx = match std::env::var(template_manager::HOME_ENV_VAR) {
52 Ok(result) => EnvTestContext::new(&result),
53 Err(_) => EnvTestContext::empty(),
54 };
55
56 pp(&Data::TestContextCreated());
57 ctx.original_value.is_some().then(handle_env_var_removal);
58 ctx
59}
60
61fn handle_env_var_removal() {
62 pp(&Data::EnvVarRemovalBefore());
63 remove_env_var(template_manager::HOME_ENV_VAR);
64 pp(&Data::Removed());
65}
66
67pub fn remove_env_var<T: AsRef<std::ffi::OsStr>>(name: T) {
68 unsafe {
69 std::env::remove_var(name);
70 }
71}
72
73pub fn set_env_var<T: AsRef<std::ffi::OsStr>, V: AsRef<std::ffi::OsStr>>(name: T, value: V) {
74 unsafe {
75 std::env::set_var(name, value);
76 }
77}
78
79pub fn load_expectation_file(expectation_file_name: &str) -> String {
80 let expect_filepath = get_expectation_file_path(expectation_file_name);
81
82 fs::read_to_string(expect_filepath).expect(error_messages::FILE_READ_TO_STRING_FAILURE)
83}
84
85pub fn load_resource_file(resource_file_name: &str) -> String {
86 let res_filepath = get_resource_file_path(resource_file_name);
87
88 fs::read_to_string(res_filepath).expect(error_messages::FILE_READ_TO_STRING_FAILURE)
89}
90
91pub fn get_expectation_file_path(expectation_file_name: &str) -> String {
92 format!(
93 "{}/{}/{expectation_file_name}.txt",
94 env!("CARGO_MANIFEST_DIR"),
95 path::TEST_EXPECTATIONS
96 )
97}
98
99pub fn get_resource_file_path(resource_name: &str) -> String {
100 format!(
101 "{}/{}/{resource_name}",
102 env!("CARGO_MANIFEST_DIR"),
103 path::TEST_RESOURCES
104 )
105}
106
107pub fn parse_cli_args(cli_args: &str) -> Vec<&str> {
108 cli_args.split_whitespace().collect()
109}
110
111pub fn parse_and_map_cli_args<B, F>(cli_args: &str, mapper: F) -> Vec<B>
112where
113 F: FnMut(&str) -> B,
114{
115 format!("{} {cli_args}", env!("CARGO_PKG_NAME"))
116 .split_whitespace()
117 .map(mapper)
118 .collect()
119}
120
121pub fn to_os_string(value: &str) -> OsString {
122 OsString::from(value)
123}
124
125pub fn to_string_list(values: &str) -> Vec<String> {
126 values.split_whitespace().map(String::from).collect()
127}
128
129pub fn get_help_message() -> String {
130 parse_expectation_file_to_help_message("help_message")
131}
132
133pub fn get_ansi_help_message() -> String {
134 parse_expectation_file_to_help_message("ansi_help_message")
135}
136
137fn parse_expectation_file_to_help_message(template_name: &str) -> String {
138 load_expectation_file(template_name)
139 .replace("{pkg_name}", env!("CARGO_PKG_NAME"))
140 .replace("{about}", parser_infos::ABOUT)
141 .replace("{template_names_desc}", help_messages::TEMPLATE_NAMES)
142 .replace("{author_desc}", help_messages::AUTHOR)
143 .replace("{server_url_desc}", help_messages::SERVER_URL)
144 .replace("{help_desc}", help_messages::HELP)
145 .replace("{version_desc}", help_messages::VERSION)
146 .replace("{version}", env!("CARGO_PKG_VERSION"))
147 .replace("{author}", env!("CARGO_PKG_AUTHORS"))
148 .replace("{author_short}", cli_options::AUTHOR.short)
149 .replace("{author_long}", cli_options::AUTHOR.long)
150 .replace("{server_url_short}", cli_options::SERVER_URL.short)
151 .replace("{server_url_long}", cli_options::SERVER_URL.long)
152 .replace("{help_short}", cli_options::HELP.short)
153 .replace("{help_long}", cli_options::HELP.long)
154 .replace("{version_short}", cli_options::VERSION.short)
155 .replace("{version_long}", cli_options::VERSION.long)
156 .replace("{server_url_default}", template_manager::BASE_URL)
157 .replace("{generator_uri_short}", cli_options::GENERATOR_URI.short)
158 .replace("{generator_uri_long}", cli_options::GENERATOR_URI.long)
159 .replace("{generator_uri_desc}", help_messages::GENERATOR_URI)
160 .replace("{generator_uri_default}", template_manager::GENERATOR_URI)
161 .replace("{list_short}", cli_options::LIST.short)
162 .replace("{list_long}", cli_options::LIST.long)
163 .replace("{list_desc}", help_messages::LIST)
164 .replace("{lister_uri_short}", cli_options::LISTER_URI.short)
165 .replace("{lister_uri_long}", cli_options::LISTER_URI.long)
166 .replace("{lister_uri_desc}", help_messages::LISTER_URI)
167 .replace("{lister_uri_default}", template_manager::LISTER_URI)
168 .replace("{check_short}", cli_options::CHECK.short)
169 .replace("{check_long}", cli_options::CHECK.long)
170 .replace("{check_desc}", help_messages::CHECK)
171 .replace("{timeout_short}", cli_options::TIMEOUT.short)
172 .replace("{timeout_long}", cli_options::TIMEOUT.long)
173 .replace("{timeout_desc}", help_messages::TIMEOUT)
174 .replace("{timeout_default}", &ppg(&Data::DefaultTimeout()))
175 .replace("{timeout_unit_short}", cli_options::TIMEOUT_UNIT.short)
176 .replace("{timeout_unit_long}", cli_options::TIMEOUT_UNIT.long)
177 .replace("{timeout_unit_desc}", help_messages::TIMEOUT_UNIT)
178 .replace("{timeout_unit_default}", template_manager::TIMEOUT_UNIT)
179 .replace("{timeout_unit_values}", "millisecond, second")
180}