use anyhow::Result;
use cargo_test_filter::{
CargoCli, CargoSubcommand, TestDiscovery, TestFilter, TestRunner,
};
use clap::Parser;
use std::time::Instant;
fn main() -> Result<()> {
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());
}
let project_root = TestDiscovery::find_project_root()?;
if args.verbose {
println!("Project root: {}\n", project_root.display());
}
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!();
}
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 args.list {
let runner = TestRunner::new(&args);
runner.list_test_functions(&filtered_functions);
return Ok(());
}
let runner = TestRunner::new(&args);
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(())
}
}
}