use std::process::Command;
use tempfile::TempDir;
#[test]
fn test_validate_parallel_enabled_by_default() {
let temp_dir = TempDir::new().unwrap();
std::fs::write(
temp_dir.path().join("test.rs"),
r#"
fn simple_function() -> i32 {
42
}
fn another_function(x: i32) -> i32 {
x * 2
}
"#,
)
.unwrap();
let output = Command::new("cargo")
.args(["run", "--bin", "debtmap", "--", "validate"])
.arg(temp_dir.path())
.output()
.expect("Failed to execute validate command");
assert!(
output.status.success(),
"Validate command failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn test_validate_no_parallel_flag() {
let temp_dir = TempDir::new().unwrap();
std::fs::write(
temp_dir.path().join("test.rs"),
r#"
fn simple_function() -> i32 {
42
}
"#,
)
.unwrap();
let output = Command::new("cargo")
.args(["run", "--bin", "debtmap", "--", "validate", "--no-parallel"])
.arg(temp_dir.path())
.output()
.expect("Failed to execute validate command");
assert!(
output.status.success(),
"Validate command with --no-parallel failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn test_validate_jobs_parameter() {
let temp_dir = TempDir::new().unwrap();
std::fs::write(
temp_dir.path().join("test.rs"),
r#"
fn simple_function() -> i32 {
42
}
"#,
)
.unwrap();
let output = Command::new("cargo")
.args(["run", "--bin", "debtmap", "--", "validate", "--jobs", "4"])
.arg(temp_dir.path())
.output()
.expect("Failed to execute validate command");
assert!(
output.status.success(),
"Validate command with --jobs failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn test_parallel_sequential_equivalence() {
let temp_dir = TempDir::new().unwrap();
std::fs::write(
temp_dir.path().join("test.rs"),
r#"
fn complex_function(a: i32, b: i32, c: i32) -> i32 {
if a > 0 {
if b > 0 {
if c > 0 {
return a + b + c;
} else {
return a + b - c;
}
} else {
return a - b;
}
} else {
return 0;
}
}
fn simple_function() -> i32 {
42
}
fn another_complex(x: i32) -> i32 {
let mut result = 0;
for i in 0..x {
if i % 2 == 0 {
result += i;
} else {
result -= i;
}
}
result
}
"#,
)
.unwrap();
let parallel_output = Command::new("cargo")
.args(["run", "--bin", "debtmap", "--", "validate"])
.arg(temp_dir.path())
.output()
.expect("Failed to execute parallel validate");
let sequential_output = Command::new("cargo")
.args(["run", "--bin", "debtmap", "--", "validate", "--no-parallel"])
.arg(temp_dir.path())
.output()
.expect("Failed to execute sequential validate");
assert!(
parallel_output.status.success(),
"Parallel validation failed: {}",
String::from_utf8_lossy(¶llel_output.stderr)
);
assert!(
sequential_output.status.success(),
"Sequential validation failed: {}",
String::from_utf8_lossy(&sequential_output.stderr)
);
assert_eq!(
parallel_output.status.code(),
sequential_output.status.code(),
"Parallel and sequential validation produced different exit codes"
);
}
#[test]
fn test_debtmap_jobs_env_var() {
let temp_dir = TempDir::new().unwrap();
std::fs::write(
temp_dir.path().join("test.rs"),
r#"
fn simple_function() -> i32 {
42
}
"#,
)
.unwrap();
let output = Command::new("cargo")
.args(["run", "--bin", "debtmap", "--", "validate"])
.arg(temp_dir.path())
.env("DEBTMAP_JOBS", "2")
.output()
.expect("Failed to execute validate command");
assert!(
output.status.success(),
"Validate command with DEBTMAP_JOBS env var failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn test_validate_parallel_large_project() {
let temp_dir = TempDir::new().unwrap();
for i in 0..10 {
std::fs::write(
temp_dir.path().join(format!("module_{}.rs", i)),
format!(
r#"
pub fn function_{}(x: i32) -> i32 {{
if x > 0 {{
x * 2
}} else {{
x / 2
}}
}}
pub fn another_function_{}(a: i32, b: i32) -> i32 {{
a + b
}}
"#,
i, i
),
)
.unwrap();
}
let output = Command::new("cargo")
.args(["run", "--bin", "debtmap", "--", "validate"])
.arg(temp_dir.path())
.output()
.expect("Failed to execute validate command");
assert!(
output.status.success(),
"Validate command on large project failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
!stderr.is_empty(),
"Expected some output from validate command"
);
}