cli-testing-specialist 1.0.10

Comprehensive testing framework for CLI tools - automated analysis, test generation, and security validation
Documentation
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use crate::analyzer::cli_parser::CliParser;
use crate::analyzer::option_inferrer::OptionInferrer;
use crate::error::Result;
use crate::types::analysis::Subcommand;
use crate::utils::{execute_with_timeout, ResourceLimits};
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashSet;
use std::path::Path;

lazy_static! {
    /// Regex pattern for subcommand lines in help output
    /// Matches lines like:
    /// - "  help      Show help information" (standard format)
    /// - "  config    Manage configuration" (standard format)
    /// - "  publish [options] [project-path]  Publish package to registry" (Commander.js format)
    ///
    /// Pattern breakdown:
    /// - `^\s{2,}` - Line starts with 2+ spaces (indentation)
    /// - `([a-z][a-z0-9-]+)` - Subcommand name (lowercase, alphanumeric, hyphens)
    /// - `(?:\s+\[[^\]]+\])*` - Optional argument specifications like [options], [path] (Commander.js)
    /// - `\s{2,}` - 2+ spaces separating command from description
    /// - `(.+)$` - Description text
    static ref SUBCOMMAND_PATTERN: Regex = Regex::new(r"^\s{2,}([a-z][a-z0-9-]+)(?:\s+\[[^\]]+\])*\s{2,}(.+)$").unwrap();

    /// Common section headers that indicate subcommands section
    static ref SUBCOMMAND_HEADERS: Vec<&'static str> = vec![
        "Commands:",
        "Available Commands:",
        "Subcommands:",
        "Available subcommands:",
        "COMMANDS:",
        "SUBCOMMANDS:",
        "positional arguments:",  // Python argparse
    ];
}

/// Maximum recursion depth for subcommand detection
const MAX_RECURSION_DEPTH: u8 = 3;

/// Subcommand Detector - Recursively detects CLI subcommands
pub struct SubcommandDetector {
    resource_limits: ResourceLimits,
    option_inferrer: OptionInferrer,
    max_depth: u8,
}

impl SubcommandDetector {
    /// Create a new subcommand detector with default settings
    pub fn new() -> Result<Self> {
        Ok(Self {
            resource_limits: ResourceLimits::default(),
            option_inferrer: OptionInferrer::new()?,
            max_depth: MAX_RECURSION_DEPTH,
        })
    }

    /// Create a new subcommand detector with custom max depth
    pub fn with_max_depth(max_depth: u8) -> Result<Self> {
        Ok(Self {
            resource_limits: ResourceLimits::default(),
            option_inferrer: OptionInferrer::new()?,
            max_depth,
        })
    }

    /// Detect subcommands from help output
    pub fn detect(&self, binary: &Path, help_output: &str) -> Result<Vec<Subcommand>> {
        log::info!("Detecting subcommands for {}", binary.display());
        self.detect_recursive(binary, help_output, 0, &mut HashSet::new())
    }

    /// Recursively detect subcommands
    fn detect_recursive(
        &self,
        binary: &Path,
        help_output: &str,
        current_depth: u8,
        visited: &mut HashSet<String>,
    ) -> Result<Vec<Subcommand>> {
        // Stop if max depth reached
        if current_depth >= self.max_depth {
            log::debug!("Max recursion depth {} reached", self.max_depth);
            return Ok(vec![]);
        }

        // Parse subcommand names from help output
        let subcommand_candidates = self.parse_subcommands(help_output);

        if subcommand_candidates.is_empty() {
            return Ok(vec![]);
        }

        log::debug!(
            "Found {} subcommand candidates at depth {}",
            subcommand_candidates.len(),
            current_depth
        );

        let mut subcommands = Vec::new();

        for (name, description) in subcommand_candidates {
            // Skip if already visited (prevent circular references)
            let visit_key = format!("{}-{}", binary.display(), name);
            if visited.contains(&visit_key) {
                log::debug!("Skipping already visited subcommand: {}", name);
                continue;
            }
            visited.insert(visit_key);

            // Get help output for this subcommand
            let subcommand_help = match self.get_subcommand_help(binary, &name) {
                Ok(help) => help,
                Err(e) => {
                    log::warn!("Failed to get help for subcommand '{}': {}", name, e);
                    continue;
                }
            };

            // Parse options for this subcommand
            let cli_parser = CliParser::new();
            let mut options = cli_parser.parse_options(&subcommand_help);

            // Infer option types
            self.option_inferrer.infer_types(&mut options);

            // Parse required positional arguments
            let required_args = cli_parser.parse_required_args(&subcommand_help);

            // Recursively detect nested subcommands
            let nested_subcommands =
                self.detect_recursive(binary, &subcommand_help, current_depth + 1, visited)?;

            subcommands.push(Subcommand {
                name,
                description: Some(description),
                options,
                required_args,
                subcommands: nested_subcommands,
                depth: current_depth,
            });
        }

        log::info!(
            "Detected {} subcommands at depth {}",
            subcommands.len(),
            current_depth
        );

        Ok(subcommands)
    }

    /// Parse subcommand names and descriptions from help output
    fn parse_subcommands(&self, help_output: &str) -> Vec<(String, String)> {
        let mut subcommands = Vec::new();
        let mut in_subcommand_section = false;

        for line in help_output.lines() {
            // Check if we entered subcommands section
            if !in_subcommand_section {
                for header in SUBCOMMAND_HEADERS.iter() {
                    if line.trim().starts_with(header) {
                        in_subcommand_section = true;
                        break;
                    }
                }
                continue;
            }

            // Check if we left subcommands section (empty line or new section)
            if line.trim().is_empty() {
                in_subcommand_section = false;
                continue;
            }

            // Parse subcommand line
            if let Some(captures) = SUBCOMMAND_PATTERN.captures(line) {
                let name = captures.get(1).unwrap().as_str().to_string();
                let description = captures.get(2).unwrap().as_str().trim().to_string();

                subcommands.push((name, description));
            }
        }

        subcommands
    }

    /// Get help output for a specific subcommand
    fn get_subcommand_help(&self, binary: &Path, subcommand: &str) -> Result<String> {
        log::debug!("Getting help for subcommand: {}", subcommand);

        // Try: <binary> <subcommand> --help
        if let Ok(output) = execute_with_timeout(
            binary,
            &[subcommand, "--help"],
            self.resource_limits.timeout(),
        ) {
            if !output.trim().is_empty() {
                return Ok(output);
            }
        }

        // Try: <binary> <subcommand> -h
        if let Ok(output) =
            execute_with_timeout(binary, &[subcommand, "-h"], self.resource_limits.timeout())
        {
            if !output.trim().is_empty() {
                return Ok(output);
            }
        }

        // Try: <binary> help <subcommand>
        if let Ok(output) = execute_with_timeout(
            binary,
            &["help", subcommand],
            self.resource_limits.timeout(),
        ) {
            if !output.trim().is_empty() {
                return Ok(output);
            }
        }

        Err(crate::error::CliTestError::InvalidHelpOutput)
    }
}

impl Default for SubcommandDetector {
    fn default() -> Self {
        Self::new().unwrap_or_else(|_| Self {
            resource_limits: ResourceLimits::default(),
            option_inferrer: OptionInferrer::default(),
            max_depth: MAX_RECURSION_DEPTH,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_subcommand_pattern() {
        assert!(SUBCOMMAND_PATTERN.is_match("  help      Show help information"));
        assert!(SUBCOMMAND_PATTERN.is_match("  config    Manage configuration"));
        assert!(SUBCOMMAND_PATTERN.is_match("    status    Show status"));
        assert!(!SUBCOMMAND_PATTERN.is_match("Options:"));
        assert!(!SUBCOMMAND_PATTERN.is_match("--help"));
    }

    #[test]
    fn test_parse_subcommands_basic() {
        let detector = SubcommandDetector::default();
        let help_output = r#"
Usage: test <command>

Commands:
  help      Show help information
  config    Manage configuration
  status    Show current status

Options:
  -h, --help    Show help
"#;

        let subcommands = detector.parse_subcommands(help_output);

        assert_eq!(subcommands.len(), 3);
        assert!(subcommands.iter().any(|(name, _)| name == "help"));
        assert!(subcommands.iter().any(|(name, _)| name == "config"));
        assert!(subcommands.iter().any(|(name, _)| name == "status"));
    }

    #[test]
    fn test_parse_subcommands_with_description() {
        let detector = SubcommandDetector::default();
        let help_output = r#"
Available Commands:
  init    Initialize a new project
  build   Build the project
"#;

        let subcommands = detector.parse_subcommands(help_output);

        assert_eq!(subcommands.len(), 2);

        let init_cmd = subcommands.iter().find(|(name, _)| name == "init");
        assert!(init_cmd.is_some());
        assert_eq!(init_cmd.unwrap().1, "Initialize a new project");
    }

    #[test]
    fn test_parse_subcommands_empty() {
        let detector = SubcommandDetector::default();
        let help_output = r#"
Usage: test [OPTIONS]

Options:
  -h, --help    Show help
"#;

        let subcommands = detector.parse_subcommands(help_output);

        assert!(subcommands.is_empty());
    }

    #[test]
    fn test_parse_subcommands_multiple_sections() {
        let detector = SubcommandDetector::default();
        let help_output = r#"
Commands:
  help    Show help

Options:
  --verbose    Verbose output

Commands:
  config    Configuration
"#;

        let subcommands = detector.parse_subcommands(help_output);

        // Should find both "help" and "config"
        assert_eq!(subcommands.len(), 2);
    }

    #[cfg(unix)]
    #[test]
    fn test_detect_git_subcommands() {
        // Test with git if available
        let git_path = Path::new("/usr/bin/git");
        if !git_path.exists() {
            return; // Skip if git not available
        }

        let detector = SubcommandDetector::with_max_depth(1).unwrap();

        // Get git help output
        if let Ok(help_output) =
            execute_with_timeout(git_path, &["--help"], ResourceLimits::default().timeout())
        {
            let result = detector.detect(git_path, &help_output);

            // Note: This test may fail if git's help format is different than expected
            // or if parse_subcommands doesn't match git's specific format.
            // That's okay - the test is primarily to verify the detector doesn't panic.
            match result {
                Ok(subcommands) => {
                    // If we found subcommands, log them for debugging
                    if !subcommands.is_empty() {
                        log::debug!("Found {} git subcommands", subcommands.len());
                    }
                }
                Err(e) => {
                    log::warn!("Git subcommand detection failed (expected): {}", e);
                }
            }
        }
    }

    #[test]
    fn test_circular_reference_prevention() {
        let _detector = SubcommandDetector::default();
        let mut visited = HashSet::new();

        // Simulate visiting a subcommand twice
        visited.insert("/bin/test-help".to_string());

        // This should be prevented by the visited set
        // (In real scenario, this is tested via detect_recursive)
        assert!(visited.contains("/bin/test-help"));
    }

    #[test]
    fn test_max_depth_limit() {
        let detector = SubcommandDetector::with_max_depth(2).unwrap();
        assert_eq!(detector.max_depth, 2);

        let detector_default = SubcommandDetector::default();
        assert_eq!(detector_default.max_depth, MAX_RECURSION_DEPTH);
    }

    #[test]
    fn test_parse_subcommands_python_argparse() {
        let detector = SubcommandDetector::default();
        let help_output = r#"
usage: tool.py [-h] [--version] command ...

positional arguments:
  command
    init      Initialize project
    build     Build project
    test      Run tests

optional arguments:
  -h, --help  show this help message and exit
"#;

        let subcommands = detector.parse_subcommands(help_output);

        assert_eq!(subcommands.len(), 3);
        assert!(subcommands.iter().any(|(name, _)| name == "init"));
        assert!(subcommands.iter().any(|(name, _)| name == "build"));
        assert!(subcommands.iter().any(|(name, _)| name == "test"));

        let init_cmd = subcommands.iter().find(|(name, _)| name == "init");
        assert!(init_cmd.is_some());
        assert_eq!(init_cmd.unwrap().1, "Initialize project");
    }
}