1 #![cfg(feature = "env")]
2
3 use std::env;
4
5 use clap::{Arg, ArgAction, Command};
6
7 use super::utils;
8
9 static HIDE_ENV: &str = "\
10 Usage: ctest [OPTIONS]
11
12 Options:
13 -c, --cafe <FILE> A coffeehouse, coffee shop, or café.
14 -h, --help Print help
15 -V, --version Print version
16 ";
17
18 static SHOW_ENV: &str = "\
19 Usage: ctest [OPTIONS]
20
21 Options:
22 -c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
23 -h, --help Print help
24 -V, --version Print version
25 ";
26
27 static HIDE_ENV_VALS: &str = "\
28 Usage: ctest [OPTIONS]
29
30 Options:
31 -c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR]
32 -h, --help Print help
33 -V, --version Print version
34 ";
35
36 static SHOW_ENV_VALS: &str = "\
37 Usage: ctest [OPTIONS]
38
39 Options:
40 -c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
41 -h, --help Print help
42 -V, --version Print version
43 ";
44
45 static HIDE_ENV_FLAG: &str = "\
46 Usage: ctest [OPTIONS]
47
48 Options:
49 -c, --cafe A coffeehouse, coffee shop, or café.
50 -h, --help Print help
51 -V, --version Print version
52 ";
53
54 static SHOW_ENV_FLAG: &str = "\
55 Usage: ctest [OPTIONS]
56
57 Options:
58 -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
59 -h, --help Print help
60 -V, --version Print version
61 ";
62
63 static HIDE_ENV_VALS_FLAG: &str = "\
64 Usage: ctest [OPTIONS]
65
66 Options:
67 -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR]
68 -h, --help Print help
69 -V, --version Print version
70 ";
71
72 static SHOW_ENV_VALS_FLAG: &str = "\
73 Usage: ctest [OPTIONS]
74
75 Options:
76 -c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
77 -h, --help Print help
78 -V, --version Print version
79 ";
80
81 #[test]
hide_env()82 fn hide_env() {
83 env::set_var("ENVVAR", "MYVAL");
84
85 let cmd = Command::new("ctest").version("0.1").arg(
86 Arg::new("cafe")
87 .short('c')
88 .long("cafe")
89 .value_name("FILE")
90 .hide_env(true)
91 .env("ENVVAR")
92 .help("A coffeehouse, coffee shop, or café.")
93 .action(ArgAction::Set),
94 );
95
96 utils::assert_output(cmd, "ctest --help", HIDE_ENV, false);
97 }
98
99 #[test]
show_env()100 fn show_env() {
101 env::set_var("ENVVAR", "MYVAL");
102
103 let cmd = Command::new("ctest").version("0.1").arg(
104 Arg::new("cafe")
105 .short('c')
106 .long("cafe")
107 .value_name("FILE")
108 .env("ENVVAR")
109 .help("A coffeehouse, coffee shop, or café.")
110 .action(ArgAction::Set),
111 );
112
113 utils::assert_output(cmd, "ctest --help", SHOW_ENV, false);
114 }
115
116 #[test]
hide_env_vals()117 fn hide_env_vals() {
118 env::set_var("ENVVAR", "MYVAL");
119
120 let cmd = Command::new("ctest").version("0.1").arg(
121 Arg::new("cafe")
122 .short('c')
123 .long("cafe")
124 .value_name("FILE")
125 .hide_env_values(true)
126 .env("ENVVAR")
127 .help("A coffeehouse, coffee shop, or café.")
128 .action(ArgAction::Set),
129 );
130
131 utils::assert_output(cmd, "ctest --help", HIDE_ENV_VALS, false);
132 }
133
134 #[test]
show_env_vals()135 fn show_env_vals() {
136 env::set_var("ENVVAR", "MYVAL");
137
138 let cmd = Command::new("ctest").version("0.1").arg(
139 Arg::new("cafe")
140 .short('c')
141 .long("cafe")
142 .value_name("FILE")
143 .env("ENVVAR")
144 .help("A coffeehouse, coffee shop, or café.")
145 .action(ArgAction::Set),
146 );
147
148 utils::assert_output(cmd, "ctest --help", SHOW_ENV_VALS, false);
149 }
150
151 #[test]
hide_env_flag()152 fn hide_env_flag() {
153 env::set_var("ENVVAR", "MYVAL");
154
155 let cmd = Command::new("ctest").version("0.1").arg(
156 Arg::new("cafe")
157 .short('c')
158 .long("cafe")
159 .action(ArgAction::SetTrue)
160 .hide_env(true)
161 .env("ENVVAR")
162 .help("A coffeehouse, coffee shop, or café."),
163 );
164
165 utils::assert_output(cmd, "ctest --help", HIDE_ENV_FLAG, false);
166 }
167
168 #[test]
show_env_flag()169 fn show_env_flag() {
170 env::set_var("ENVVAR", "MYVAL");
171
172 let cmd = Command::new("ctest").version("0.1").arg(
173 Arg::new("cafe")
174 .short('c')
175 .long("cafe")
176 .action(ArgAction::SetTrue)
177 .env("ENVVAR")
178 .help("A coffeehouse, coffee shop, or café."),
179 );
180
181 utils::assert_output(cmd, "ctest --help", SHOW_ENV_FLAG, false);
182 }
183
184 #[test]
hide_env_vals_flag()185 fn hide_env_vals_flag() {
186 env::set_var("ENVVAR", "MYVAL");
187
188 let cmd = Command::new("ctest").version("0.1").arg(
189 Arg::new("cafe")
190 .short('c')
191 .long("cafe")
192 .action(ArgAction::SetTrue)
193 .hide_env_values(true)
194 .env("ENVVAR")
195 .help("A coffeehouse, coffee shop, or café."),
196 );
197
198 utils::assert_output(cmd, "ctest --help", HIDE_ENV_VALS_FLAG, false);
199 }
200
201 #[test]
show_env_vals_flag()202 fn show_env_vals_flag() {
203 env::set_var("ENVVAR", "MYVAL");
204
205 let cmd = Command::new("ctest").version("0.1").arg(
206 Arg::new("cafe")
207 .short('c')
208 .long("cafe")
209 .action(ArgAction::SetTrue)
210 .env("ENVVAR")
211 .help("A coffeehouse, coffee shop, or café."),
212 );
213
214 utils::assert_output(cmd, "ctest --help", SHOW_ENV_VALS_FLAG, false);
215 }
216