Skip to main content

cargo_test_filter/
cli.rs

1use clap::{Parser, Subcommand};
2
3#[derive(Parser, Debug)]
4#[command(name = "cargo")]
5#[command(bin_name = "cargo")]
6pub struct CargoCli {
7    #[command(subcommand)]
8    pub command: CargoSubcommand,
9}
10
11#[derive(Subcommand, Debug)]
12pub enum CargoSubcommand {
13    #[command(name = "test-filter")]
14    TestFilter(TestFilterArgs),
15}
16
17/// A cargo subcommand for intelligent test filtering and compilation
18#[derive(Parser, Debug)]
19#[command(author, version, about, long_about = None)]
20pub struct TestFilterArgs {
21    /// Run only integration tests (tests in tests/ directory)
22    #[arg(long, conflicts_with = "unit")]
23    pub integration: bool,
24
25    /// Run only unit tests (tests in src/ files)
26    #[arg(long, conflicts_with = "integration")]
27    pub unit: bool,
28
29    /// Filter tests by tag (e.g., --tag fast or --tag "slow,integration")
30    #[arg(long, value_delimiter = ',')]
31    pub tag: Vec<String>,
32
33    /// Exclude tests with these tags
34    #[arg(long, value_delimiter = ',')]
35    pub exclude_tag: Vec<String>,
36
37    /// Filter tests by name pattern
38    #[arg(long)]
39    pub name: Option<String>,
40
41    /// Filter tests by path pattern
42    #[arg(long)]
43    pub path: Option<String>,
44
45    /// Test timeout in seconds (per test)
46    #[arg(long)]
47    pub timeout: Option<u64>,
48
49    /// List matching tests without running them
50    #[arg(long)]
51    pub list: bool,
52
53    /// Show verbose output
54    #[arg(short, long)]
55    pub verbose: bool,
56
57    /// Arguments to pass through to cargo test
58    #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
59    pub test_args: Vec<String>,
60}
61
62impl TestFilterArgs {
63    /// Check if any filters are active
64    pub fn has_filters(&self) -> bool {
65        self.integration
66            || self.unit
67            || !self.tag.is_empty()
68            || !self.exclude_tag.is_empty()
69            || self.name.is_some()
70            || self.path.is_some()
71    }
72}