1use crate::config;
7use crate::types::{CiInfo, EnvValue, VendorConfig};
8
9fn get_env_keys(env_info: &Option<EnvValue>) -> Vec<String> {
10 match env_info {
11 Some(info) => match info {
12 EnvValue::Exists(ref key) => vec![key.to_string()],
13 EnvValue::AllExists(ref keys) => keys.clone(),
14 EnvValue::AnyExists(ref keys) => keys.clone(),
15 EnvValue::Value(ref key, ref _value) => vec![key.to_string()],
16 EnvValue::NotEqual(ref key, ref _value) => vec![key.to_string()],
17 EnvValue::Contains(ref key, ref _value) => vec![key.to_string()],
18 EnvValue::NotEmpty(ref key) => vec![key.to_string()],
19 },
20 None => vec![],
21 }
22}
23
24pub(crate) fn clear_env(vendor_config_list: &Vec<VendorConfig>) {
25 for vendor_config in vendor_config_list.iter() {
26 let mut keys = get_env_keys(&Some(vendor_config.ci_env.clone()));
27 envmnt::remove_all(&keys);
28
29 keys = get_env_keys(&vendor_config.pr_env);
30 envmnt::remove_all(&keys);
31
32 if let Some(ref key) = vendor_config.branch_name_env {
33 envmnt::remove(key)
34 };
35 }
36}
37
38fn set_mock_env_key_value_pairs(env_info: &Option<EnvValue>, test_value: &str) {
39 let key_value_pairs = match env_info {
40 Some(info) => match info {
41 EnvValue::Exists(ref key) => vec![(key.to_string(), test_value.to_string())],
42 EnvValue::AllExists(ref keys) => {
43 let mut key_values = vec![];
44
45 for key in keys {
46 key_values.push((key.to_string(), test_value.to_string()))
47 }
48
49 key_values
50 }
51 EnvValue::AnyExists(ref keys) => vec![(keys[0].to_string(), test_value.to_string())],
52 EnvValue::Value(ref key, ref value) => vec![(key.to_string(), value.to_string())],
53 EnvValue::NotEqual(ref key, ref _value) => {
54 vec![(key.to_string(), test_value.to_string())]
55 }
56 EnvValue::Contains(ref key, ref value) => vec![(key.to_string(), value.to_string())],
57 EnvValue::NotEmpty(ref key) => vec![(key.to_string(), test_value.to_string())],
58 },
59 None => vec![],
60 };
61
62 for key_value_pair in key_value_pairs {
63 envmnt::set(key_value_pair.0, key_value_pair.1);
64 }
65}
66
67pub(crate) fn set_env_for_config(vendor_config: &VendorConfig, branch_name: Option<String>) {
68 set_mock_env_key_value_pairs(&Some(vendor_config.ci_env.clone()), "mock_ci");
69 set_mock_env_key_value_pairs(&vendor_config.pr_env, "mock_pr");
70 if let Some(ref key) = vendor_config.branch_name_env {
71 envmnt::set(key, branch_name.unwrap_or("mock_branch".to_string()))
72 };
73}
74
75fn set_env_for_info(info: &CiInfo, vendor_config_list: Vec<VendorConfig>) {
76 if info.ci {
77 match info.vendor {
78 Some(ref vendor) => {
79 for vendor_config in vendor_config_list.iter() {
80 if vendor_config.vendor == *vendor {
81 let mut mock_vendor_config = vendor_config.clone();
82
83 match info.pr {
84 Some(value) => {
85 if !value {
86 mock_vendor_config.pr_env = None;
87 }
88 }
89 None => mock_vendor_config.pr_env = None,
90 }
91
92 if info.branch_name.is_none() {
93 mock_vendor_config.branch_name_env = None;
94 }
95
96 set_env_for_config(&mock_vendor_config, info.branch_name.clone());
97
98 break;
99 }
100 }
101 }
102 None => (),
103 }
104 }
105}
106
107pub(crate) fn mock_ci_info(info: &CiInfo) {
108 let vendor_config_list = config::create();
109
110 clear_env(&vendor_config_list);
112
113 set_env_for_info(info, vendor_config_list);
114}