-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
374 lines (323 loc) · 12.4 KB
/
Copy pathmod.rs
File metadata and controls
374 lines (323 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! CLI Module
//!
//! This module provides the command-line interface structure and functionality
//! for the Torrust Tracker Deployer application. It handles CLI argument parsing
//! and provides the CLI data structures.
use clap::Parser;
// Re-export submodules for convenient access
pub mod args;
pub mod commands;
pub use args::GlobalArgs;
pub use commands::{Commands, CreateAction};
/// Command-line interface for Torrust Tracker Deployer
///
/// This struct defines the top-level CLI structure including global arguments
/// and available subcommands. It uses clap for argument parsing and provides
/// comprehensive help documentation.
#[derive(Parser, Debug)]
#[command(name = "torrust-tracker-deployer")]
#[command(about = "Automated deployment infrastructure for Torrust Tracker")]
#[command(version)]
#[allow(clippy::struct_field_names)] // CLI arguments intentionally share 'log_' prefix for clarity
pub struct Cli {
/// Global arguments (logging configuration)
#[command(flatten)]
pub global: GlobalArgs,
/// Subcommand to execute
#[command(subcommand)]
pub command: Option<Commands>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_parse_destroy_subcommand() {
let args = vec!["torrust-tracker-deployer", "destroy", "test-env"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(cli.command.is_some());
match cli.command.unwrap() {
Commands::Destroy { environment } => {
assert_eq!(environment, "test-env");
}
Commands::Create { .. } => panic!("Expected Destroy command"),
}
}
#[test]
fn it_should_parse_destroy_with_different_environment_names() {
let test_cases = vec!["e2e-provision", "production", "test-123", "dev-environment"];
for env_name in test_cases {
let args = vec!["torrust-tracker-deployer", "destroy", env_name];
let cli = Cli::try_parse_from(args).unwrap();
match cli.command.unwrap() {
Commands::Destroy { environment } => {
assert_eq!(environment, env_name);
}
Commands::Create { .. } => panic!("Expected Destroy command"),
}
}
}
#[test]
fn it_should_require_environment_parameter() {
let args = vec!["torrust-tracker-deployer", "destroy"];
let result = Cli::try_parse_from(args);
assert!(result.is_err());
let error = result.unwrap_err();
let error_message = error.to_string();
assert!(
error_message.contains("required") || error_message.contains("argument"),
"Error message should indicate missing required argument: {error_message}"
);
}
#[test]
fn it_should_parse_global_log_options_with_destroy_command() {
let args = vec![
"torrust-tracker-deployer",
"--log-file-format",
"json",
"--log-stderr-format",
"compact",
"--log-output",
"file-and-stderr",
"--log-dir",
"/tmp/logs",
"destroy",
"test-env",
];
let cli = Cli::try_parse_from(args).unwrap();
// Verify the destroy command was parsed correctly
match cli.command.unwrap() {
Commands::Destroy { environment } => {
assert_eq!(environment, "test-env");
}
Commands::Create { .. } => panic!("Expected Destroy command"),
}
// Log options are set but we don't compare them as they don't implement PartialEq
assert_eq!(cli.global.log_dir, std::path::PathBuf::from("/tmp/logs"));
}
#[test]
fn it_should_use_default_log_dir_when_not_specified() {
let args = vec!["torrust-tracker-deployer", "destroy", "test-env"];
let cli = Cli::try_parse_from(args).unwrap();
assert_eq!(cli.global.log_dir, std::path::PathBuf::from("./data/logs"));
}
#[test]
fn it_should_handle_no_command() {
let args = vec!["torrust-tracker-deployer"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(cli.command.is_none());
}
#[test]
fn it_should_show_help_with_help_flag() {
let args = vec!["torrust-tracker-deployer", "--help"];
let result = Cli::try_parse_from(args);
// Help flag causes a "display help" error
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::DisplayHelp);
}
#[test]
fn it_should_show_version_with_version_flag() {
let args = vec!["torrust-tracker-deployer", "--version"];
let result = Cli::try_parse_from(args);
// Version flag causes a "display version" error
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::DisplayVersion);
}
#[test]
fn it_should_show_destroy_help() {
let args = vec!["torrust-tracker-deployer", "destroy", "--help"];
let result = Cli::try_parse_from(args);
// Help flag causes a "display help" error
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::DisplayHelp);
// Verify the help text mentions the environment parameter
let help_text = error.to_string();
assert!(
help_text.contains("environment") || help_text.contains("<ENVIRONMENT>"),
"Help text should mention environment parameter"
);
}
#[test]
fn it_should_parse_create_environment_subcommand() {
let args = vec![
"torrust-tracker-deployer",
"create",
"environment",
"--env-file",
"config.json",
];
let cli = Cli::try_parse_from(args).unwrap();
assert!(cli.command.is_some());
match cli.command.unwrap() {
Commands::Create { action } => match action {
crate::presentation::cli::CreateAction::Environment { env_file } => {
assert_eq!(env_file, std::path::PathBuf::from("config.json"));
}
crate::presentation::cli::CreateAction::Template { .. } => {
panic!("Expected Environment action")
}
},
Commands::Destroy { .. } => panic!("Expected Create command"),
}
}
#[test]
fn it_should_parse_create_environment_with_short_flag() {
let args = vec![
"torrust-tracker-deployer",
"create",
"environment",
"-f",
"env.json",
];
let cli = Cli::try_parse_from(args).unwrap();
match cli.command.unwrap() {
Commands::Create { action } => match action {
crate::presentation::cli::CreateAction::Environment { env_file } => {
assert_eq!(env_file, std::path::PathBuf::from("env.json"));
}
crate::presentation::cli::CreateAction::Template { .. } => {
panic!("Expected Environment action")
}
},
Commands::Destroy { .. } => panic!("Expected Create command"),
}
}
#[test]
fn it_should_require_env_file_parameter_for_create_environment() {
let args = vec!["torrust-tracker-deployer", "create", "environment"];
let result = Cli::try_parse_from(args);
assert!(result.is_err());
let error = result.unwrap_err();
let error_message = error.to_string();
assert!(
error_message.contains("required") || error_message.contains("--env-file"),
"Error message should indicate missing required --env-file: {error_message}"
);
}
#[test]
fn it_should_parse_working_dir_global_option_with_create_environment() {
let args = vec![
"torrust-tracker-deployer",
"--working-dir",
"/tmp/workspace",
"create",
"environment",
"--env-file",
"config.json",
];
let cli = Cli::try_parse_from(args).unwrap();
assert_eq!(
cli.global.working_dir,
std::path::PathBuf::from("/tmp/workspace")
);
match cli.command.unwrap() {
Commands::Create { action } => match action {
crate::presentation::cli::CreateAction::Environment { env_file } => {
assert_eq!(env_file, std::path::PathBuf::from("config.json"));
}
crate::presentation::cli::CreateAction::Template { .. } => {
panic!("Expected Environment action")
}
},
Commands::Destroy { .. } => panic!("Expected Create command"),
}
}
#[test]
fn it_should_use_default_working_dir_when_not_specified() {
let args = vec![
"torrust-tracker-deployer",
"create",
"environment",
"-f",
"config.json",
];
let cli = Cli::try_parse_from(args).unwrap();
assert_eq!(cli.global.working_dir, std::path::PathBuf::from("."));
}
#[test]
fn it_should_show_create_help() {
let args = vec!["torrust-tracker-deployer", "create", "--help"];
let result = Cli::try_parse_from(args);
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::DisplayHelp);
let help_text = error.to_string();
assert!(
help_text.contains("environment") || help_text.contains("template"),
"Help text should mention subcommands: {help_text}"
);
}
#[test]
fn it_should_parse_create_template_without_path() {
let args = vec!["torrust-tracker-deployer", "create", "template"];
let cli = Cli::try_parse_from(args).unwrap();
match cli.command.unwrap() {
Commands::Create { action } => match action {
crate::presentation::cli::CreateAction::Template { output_path } => {
assert!(output_path.is_none());
}
crate::presentation::cli::CreateAction::Environment { .. } => {
panic!("Expected Template action")
}
},
Commands::Destroy { .. } => panic!("Expected Create command"),
}
}
#[test]
fn it_should_parse_create_template_with_custom_path() {
let args = vec![
"torrust-tracker-deployer",
"create",
"template",
"./config/my-env.json",
];
let cli = Cli::try_parse_from(args).unwrap();
match cli.command.unwrap() {
Commands::Create { action } => match action {
crate::presentation::cli::CreateAction::Template { output_path } => {
assert_eq!(
output_path,
Some(std::path::PathBuf::from("./config/my-env.json"))
);
}
crate::presentation::cli::CreateAction::Environment { .. } => {
panic!("Expected Template action")
}
},
Commands::Destroy { .. } => panic!("Expected Create command"),
}
}
#[test]
fn it_should_show_create_environment_help() {
let args = vec![
"torrust-tracker-deployer",
"create",
"environment",
"--help",
];
let result = Cli::try_parse_from(args);
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::DisplayHelp);
let help_text = error.to_string();
assert!(
help_text.contains("env-file") || help_text.contains("configuration"),
"Help text should mention env-file parameter"
);
}
#[test]
fn it_should_show_create_template_help() {
let args = vec!["torrust-tracker-deployer", "create", "template", "--help"];
let result = Cli::try_parse_from(args);
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::DisplayHelp);
let help_text = error.to_string();
assert!(
help_text.contains("template") || help_text.contains("placeholder"),
"Help text should mention template generation"
);
}
}