use anyhow::Result;
use std::fs;
use std::process::Command;
use tempfile::TempDir;
fn create_test_git_repo() -> Result<TempDir> {
let temp_dir = TempDir::new()?;
let repo_path = temp_dir.path();
Command::new("git")
.args(["init"])
.current_dir(repo_path)
.output()?;
Command::new("git")
.args(["config", "user.email", "test@example.com"])
.current_dir(repo_path)
.output()?;
Command::new("git")
.args(["config", "user.name", "Test User"])
.current_dir(repo_path)
.output()?;
let test_file = repo_path.join("src").join("lib.rs");
fs::create_dir_all(test_file.parent().unwrap())?;
fs::write(
&test_file,
r#"
pub fn complex_function(a: i32, b: i32, c: i32, d: i32) -> i32 {
if a > 0 {
if b > 0 {
if c > 0 {
if d > 0 {
return a + b + c + d;
} else {
return a + b + c;
}
} else {
return a + b;
}
} else {
return a;
}
} else {
return 0;
}
}
"#,
)?;
Command::new("git")
.args(["add", "."])
.current_dir(repo_path)
.output()?;
Command::new("git")
.args(["commit", "-m", "Initial commit"])
.current_dir(repo_path)
.output()?;
fs::write(
&test_file,
r#"
pub fn complex_function(a: i32, b: i32, c: i32, d: i32) -> i32 {
// Fix: handle negative numbers correctly
if a > 0 {
if b > 0 {
if c > 0 {
if d > 0 {
return a + b + c + d;
} else {
return a + b + c;
}
} else {
return a + b;
}
} else {
return a;
}
} else {
return 0;
}
}
"#,
)?;
Command::new("git")
.args(["add", "."])
.current_dir(repo_path)
.output()?;
Command::new("git")
.args(["commit", "-m", "fix: handle negative numbers"])
.current_dir(repo_path)
.output()?;
Ok(temp_dir)
}
#[test]
fn test_context_flag_populates_contextual_risk() -> Result<()> {
let temp_dir = create_test_git_repo()?;
let repo_path = temp_dir.path();
let output = Command::new(env!("CARGO_BIN_EXE_debtmap"))
.args([
"analyze",
repo_path.to_str().unwrap(),
"--context",
"--format",
"json",
])
.current_dir(repo_path)
.output()?;
if !output.status.success() {
eprintln!(
"Command failed with stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
eprintln!(
"Command stdout: {}",
String::from_utf8_lossy(&output.stdout)
);
}
assert!(
output.status.success(),
"debtmap analyze with --context should succeed"
);
let stdout = String::from_utf8(output.stdout)?;
let json_result: Result<serde_json::Value, _> = serde_json::from_str(&stdout);
if let Ok(json) = json_result {
let json_str = serde_json::to_string_pretty(&json)?;
println!("✓ Command succeeded with --context flag");
println!("JSON output contains {} bytes", json_str.len());
} else {
println!("✓ Command succeeded with --context flag (no debt items found)");
}
Ok(())
}
#[test]
fn test_without_context_flag_no_contextual_risk() -> Result<()> {
let temp_dir = create_test_git_repo()?;
let repo_path = temp_dir.path();
let output = Command::new(env!("CARGO_BIN_EXE_debtmap"))
.args(["analyze", repo_path.to_str().unwrap(), "--format", "json"])
.current_dir(repo_path)
.output()?;
assert!(
output.status.success(),
"debtmap analyze without --context should succeed"
);
let stdout = String::from_utf8(output.stdout)?;
println!("✓ Verified command runs successfully without --context flag");
println!("Output length: {} bytes", stdout.len());
Ok(())
}
#[test]
fn test_context_flag_terminal_output() -> Result<()> {
let temp_dir = create_test_git_repo()?;
let repo_path = temp_dir.path();
let output = Command::new(env!("CARGO_BIN_EXE_debtmap"))
.args(["analyze", repo_path.to_str().unwrap(), "--context"])
.current_dir(repo_path)
.output()?;
assert!(
output.status.success(),
"debtmap analyze with --context should succeed"
);
let stdout = String::from_utf8(output.stdout)?;
println!("✓ Command runs successfully with --context flag");
println!("Output length: {} bytes", stdout.len());
Ok(())
}