use assert_cmd::Command;
use std::fs;
use std::path::Path;
use tempfile::TempDir;
fn create_file(base: &Path, path: &str, content: &str) {
let file_path = base.join(path);
if let Some(parent) = file_path.parent() {
fs::create_dir_all(parent).unwrap();
}
fs::write(file_path, content).unwrap();
}
#[test]
fn test_cli_include_callers_simple() {
let temp_dir = TempDir::new().unwrap();
let root = temp_dir.path();
fs::create_dir_all(root.join(".git")).unwrap();
create_file(
root,
"math.rs",
r#"
/// Calculate a value
pub fn calculate(a: i32, b: i32) -> i32 {
a + b
}
"#,
);
create_file(
root,
"main.rs",
r#"
mod math;
fn main() {
let result = math::calculate(5, 3);
println!("Result: {}", result);
}
"#,
);
create_file(
root,
"other.rs",
r#"
fn other_function() {
println!("Other work");
}
"#,
);
let mut cmd = Command::cargo_bin("context-creator").unwrap();
let output = cmd
.current_dir(root)
.args([
"--include",
"math.rs", "--include-callers", ])
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("STDOUT:\n{stdout}");
eprintln!("STDERR:\n{stderr}");
assert!(
output.status.success(),
"Command failed with exit code: {}",
output.status
);
assert!(stdout.contains("math.rs"), "Output should contain math.rs");
assert!(
stdout.contains("main.rs"),
"Output should contain main.rs as it calls calculate()"
);
assert!(
!stdout.contains("other.rs"),
"Output should not contain other.rs as it doesn't call functions from math.rs"
);
}
#[test]
fn test_cli_include_callers_with_glob() {
let temp_dir = TempDir::new().unwrap();
let root = temp_dir.path();
fs::create_dir_all(root.join(".git")).unwrap();
create_file(
root,
"core/math.rs",
r#"
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn multiply(a: i32, b: i32) -> i32 {
a * b
}
"#,
);
create_file(
root,
"core/utils.rs",
r#"
pub fn format_number(n: i32) -> String {
format!("Number: {}", n)
}
"#,
);
create_file(
root,
"app.rs",
r#"
use crate::core::math::{add, multiply};
use crate::core::utils::format_number;
fn calculate() {
let sum = add(5, 3);
let product = multiply(4, 2);
let formatted = format_number(sum);
println!("{}", formatted);
}
"#,
);
create_file(
root,
"tests.rs",
r#"
use crate::core::math::add;
#[cfg(test)]
mod tests {
use crate::core::math::multiply;
#[test]
fn test_add() {
let result = super::add(2, 2);
assert_eq!(result, 4);
}
#[test]
fn test_multiply() {
let result = multiply(3, 4);
assert_eq!(result, 12);
}
}
// Direct function call outside of test module
pub fn calculate_sum() -> i32 {
add(10, 20)
}
"#,
);
let mut cmd = Command::cargo_bin("context-creator").unwrap();
let output = cmd
.current_dir(root)
.args([
"--include",
"core/*.rs", "--include-callers", ])
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!("STDOUT:\n{stdout}");
eprintln!("STDERR:\n{stderr}");
assert!(stdout.contains("math.rs"));
assert!(stdout.contains("utils.rs"));
assert!(stdout.contains("app.rs"));
assert!(stdout.contains("tests.rs"));
}