dampen_cli/commands/
test.rs1#![allow(clippy::print_stderr, clippy::print_stdout)]
2
3use std::path::Path;
9use std::process::Command;
10
11#[derive(clap::Args)]
13pub struct TestArgs {
14 #[arg(short, long)]
16 package: Option<String>,
17
18 #[arg(value_name = "TESTNAME")]
20 test_filter: Option<String>,
21
22 #[arg(last = true)]
24 test_args: Vec<String>,
25
26 #[arg(long)]
28 release: bool,
29
30 #[arg(long)]
32 ignored: bool,
33
34 #[arg(long)]
36 only_ignored: bool,
37
38 #[arg(long)]
40 quiet: bool,
41
42 #[arg(short, long)]
44 verbose: bool,
45
46 #[arg(long, value_delimiter = ',')]
48 features: Vec<String>,
49}
50
51pub fn execute(args: &TestArgs) -> Result<(), String> {
80 if !Path::new("Cargo.toml").exists() {
82 return Err("Cargo.toml not found. Are you in a Rust project directory?".to_string());
83 }
84
85 if args.verbose {
86 eprintln!("Running tests...");
87 }
88
89 let mut cmd = Command::new("cargo");
91 cmd.arg("test");
92
93 if let Some(ref package) = args.package {
95 cmd.arg("-p").arg(package);
96 }
97
98 if args.release {
100 cmd.arg("--release");
101 }
102
103 if args.verbose {
105 cmd.arg("--verbose");
106 }
107
108 if args.quiet {
110 cmd.arg("--quiet");
111 }
112
113 if !args.features.is_empty() {
115 cmd.arg("--features").arg(args.features.join(","));
116 }
117
118 if let Some(ref filter) = args.test_filter {
120 cmd.arg(filter);
121 }
122
123 if !args.test_args.is_empty() || args.ignored || args.only_ignored {
125 cmd.arg("--");
126
127 if args.only_ignored {
128 cmd.arg("--ignored");
129 } else if args.ignored {
130 cmd.arg("--include-ignored");
131 }
132
133 cmd.args(&args.test_args);
134 }
135
136 if args.verbose {
138 let mut command_str = String::from("cargo test");
139 if let Some(ref package) = args.package {
140 command_str.push_str(&format!(" -p {}", package));
141 }
142 if args.release {
143 command_str.push_str(" --release");
144 }
145 if !args.features.is_empty() {
146 command_str.push_str(&format!(" --features {}", args.features.join(",")));
147 }
148 if let Some(ref filter) = args.test_filter {
149 command_str.push_str(&format!(" {}", filter));
150 }
151 eprintln!("Executing: {}", command_str);
152 }
153
154 let status = cmd
155 .status()
156 .map_err(|e| format!("Failed to execute cargo: {}", e))?;
157
158 if !status.success() {
159 return Err("Tests failed".to_string());
160 }
161
162 Ok(())
163}