use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;
#[test]
fn test_coverage_threshold_100_percent() {
let mut temp_file = NamedTempFile::with_suffix(".ruchy").expect("Failed to create temp file");
writeln!(temp_file, r#"println("Hello, World!")"#).expect("Failed to write to temp file");
let output = Command::new("cargo")
.args(["run", "--bin", "ruchy", "--", "test", "--coverage"])
.arg(temp_file.path())
.arg("--threshold")
.arg("100")
.output()
.expect("Failed to run ruchy test command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("STDOUT: {stdout}");
println!("STDERR: {stderr}");
assert!(
stdout.contains("Coverage meets threshold of 100.0%"),
"Expected to see 100.0% threshold but got: {stdout}{stderr}"
);
assert!(output.status.success(), "Command should succeed");
}
#[test]
fn test_coverage_threshold_80_percent() {
let mut temp_file = NamedTempFile::with_suffix(".ruchy").expect("Failed to create temp file");
writeln!(
temp_file,
r#"
let score = 85
let grade = if score >= 90 {{ "A" }} else if score >= 80 {{ "B" }} else {{ "C" }}
println(f"Grade: {{grade}}")
"#
)
.expect("Failed to write to temp file");
let output = Command::new("cargo")
.args(["run", "--bin", "ruchy", "--", "test", "--coverage"])
.arg(temp_file.path())
.arg("--threshold")
.arg("80")
.output()
.expect("Failed to run ruchy test command");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("Coverage meets threshold of 80.0%"),
"Expected 80.0% threshold but got: {stdout}"
);
}
#[test]
fn test_coverage_threshold_default_behavior() {
let mut temp_file = NamedTempFile::with_suffix(".ruchy").expect("Failed to create temp file");
writeln!(temp_file, r#"println("Test without threshold")"#)
.expect("Failed to write to temp file");
let output = Command::new("cargo")
.args(["run", "--bin", "ruchy", "--", "test", "--coverage"])
.arg(temp_file.path())
.output()
.expect("Failed to run ruchy test command");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
!stdout.contains("Coverage meets threshold"),
"Should not mention threshold when none specified: {stdout}"
);
assert!(
stdout.contains("Coverage:") || stdout.contains('%'),
"Should show coverage information: {stdout}"
);
}