cargo-test-filter 0.1.1

A cargo subcommand for intelligent test filtering and compilation
Documentation
use anyhow::Result;
use cargo_test_filter::{
    CargoCli, CargoSubcommand, TestDiscovery, TestFilter, TestRunner,
};
use clap::Parser;
use std::time::Instant;

fn main() -> Result<()> {
    // Parse command line arguments
    let cli = CargoCli::parse();

    match cli.command {
        CargoSubcommand::TestFilter(args) => {
            let start = Instant::now();

            if args.verbose {
                println!("cargo-test-filter v{}", env!("CARGO_PKG_VERSION"));
                println!("Filters: {}\n", TestFilter::new(&args).get_filter_summary());
            }

            // Find the project root
            let project_root = TestDiscovery::find_project_root()?;
            if args.verbose {
                println!("Project root: {}\n", project_root.display());
            }

            // Discover individual test functions
            let discovery = TestDiscovery::new(project_root);
            let all_functions = discovery.discover_test_functions()?;

            if args.verbose {
                println!("Discovered {} test function(s) in {:.2?}", all_functions.len(), start.elapsed());
                for func in &all_functions {
                    println!("  - {}::{} (tags: {:?})", func.target_name, func.name, func.tags);
                }
                println!();
            }

            // Filter test functions based on criteria
            let filter = TestFilter::new(&args);
            let filtered_functions = if args.has_filters() {
                filter.filter_functions(all_functions)
            } else {
                all_functions
            };

            if args.verbose && args.has_filters() {
                println!("Filtered to {} test function(s)\n", filtered_functions.len());
            }

            // If --list flag is set, just list the tests and exit
            if args.list {
                let runner = TestRunner::new(&args);
                runner.list_test_functions(&filtered_functions);
                return Ok(());
            }

            // Run the filtered test functions
            let runner = TestRunner::new(&args);

            // If no filters, run all tests efficiently
            if !args.has_filters() {
                runner.run_all_tests()?;
            } else {
                runner.run_test_functions(&filtered_functions)?;
            }

            if args.verbose {
                println!("\nTotal time: {:.2?}", start.elapsed());
            }

            Ok(())
        }
    }
}