1#![cfg(feature = "env")]
2
3use std::env;
4
5use clap::{Arg, ArgAction, Command};
6
7use super::utils;
8
9static HIDE_ENV: &str = "\
10Usage: ctest [OPTIONS]
11
12Options:
13  -c, --cafe <FILE>  A coffeehouse, coffee shop, or café.
14  -h, --help         Print help
15  -V, --version      Print version
16";
17
18static SHOW_ENV: &str = "\
19Usage: ctest [OPTIONS]
20
21Options:
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
27static HIDE_ENV_VALS: &str = "\
28Usage: ctest [OPTIONS]
29
30Options:
31  -c, --cafe <FILE>  A coffeehouse, coffee shop, or café. [env: ENVVAR]
32  -h, --help         Print help
33  -V, --version      Print version
34";
35
36static SHOW_ENV_VALS: &str = "\
37Usage: ctest [OPTIONS]
38
39Options:
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
45static HIDE_ENV_FLAG: &str = "\
46Usage: ctest [OPTIONS]
47
48Options:
49  -c, --cafe     A coffeehouse, coffee shop, or café.
50  -h, --help     Print help
51  -V, --version  Print version
52";
53
54static SHOW_ENV_FLAG: &str = "\
55Usage: ctest [OPTIONS]
56
57Options:
58  -c, --cafe     A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
59  -h, --help     Print help
60  -V, --version  Print version
61";
62
63static HIDE_ENV_VALS_FLAG: &str = "\
64Usage: ctest [OPTIONS]
65
66Options:
67  -c, --cafe     A coffeehouse, coffee shop, or café. [env: ENVVAR]
68  -h, --help     Print help
69  -V, --version  Print version
70";
71
72static SHOW_ENV_VALS_FLAG: &str = "\
73Usage: ctest [OPTIONS]
74
75Options:
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]
82fn 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]
100fn 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]
117fn 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]
135fn 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]
152fn 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]
169fn 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]
185fn 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]
202fn 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