#![allow(dead_code)] #![allow(clippy::uninlined_format_args)]
use assert_cmd::Command;
use std::path::Path;
pub fn context_creator_cmd() -> Command {
Command::cargo_bin("context-creator").unwrap()
}
pub fn assert_contains_file(output: &str, file_path: &str) {
let unix_path = file_path.replace('\\', "/");
let windows_path = file_path.replace('/', "\\");
let filename = std::path::Path::new(file_path)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(file_path);
let header_check = output.contains(&format!("## {filename}"));
assert!(
output.contains(&unix_path) || output.contains(&windows_path) || header_check,
"Expected output to contain file '{}', but it didn't.\nOutput:\n{}",
file_path,
output
);
}
pub fn assert_not_contains_file(output: &str, file_path: &str) {
let unix_path = file_path.replace('\\', "/");
let windows_path = file_path.replace('/', "\\");
let filename = std::path::Path::new(file_path)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(file_path);
let header_check = output.contains(&format!("## {filename}"));
assert!(
!output.contains(&unix_path) && !output.contains(&windows_path) && !header_check,
"Expected output NOT to contain file '{}', but it did.\nOutput:\n{}",
file_path,
output
);
}
pub fn assert_contains_code(output: &str, code_snippet: &str) {
assert!(
output.contains(code_snippet),
"Expected output to contain code snippet '{}', but it didn't.\nOutput:\n{}",
code_snippet,
output
);
}
pub fn assert_contains_file_header(output: &str, file_name: &str) {
let patterns = [
format!("## {file_name}"),
format!("### {file_name}"),
format!("# {file_name}"),
format!("File: {file_name}"),
];
let found = patterns.iter().any(|pattern| output.contains(pattern));
assert!(
found,
"Expected output to contain header for file '{}', but it didn't.\nOutput:\n{}",
file_name, output
);
}
pub fn run_context_creator(args: &[&str], project_dir: &Path) -> String {
let mut cmd = context_creator_cmd();
cmd.current_dir(project_dir);
let output_file = project_dir.join("test_output.md");
let mut has_output = false;
let mut has_prompt = false;
for arg in args {
if *arg == "--output-file" || *arg == "-o" {
has_output = true;
}
if *arg == "--prompt" || *arg == "-p" {
has_prompt = true;
}
}
for arg in args {
cmd.arg(arg);
}
if !has_output && !has_prompt {
cmd.arg("--output-file").arg(&output_file);
}
let output = cmd.output().expect("Failed to execute context-creator");
assert!(
output.status.success(),
"context-creator failed with status: {}\nstderr: {}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
if !has_output && !has_prompt && output_file.exists() {
let content = std::fs::read_to_string(&output_file).expect("Failed to read output file");
let _ = std::fs::remove_file(&output_file);
content
} else {
String::from_utf8_lossy(&output.stdout).to_string()
}
}
pub fn run_context_creator_expect_failure(args: &[&str], project_dir: &Path) -> String {
let mut cmd = context_creator_cmd();
cmd.current_dir(project_dir);
for arg in args {
cmd.arg(arg);
}
let output = cmd.output().expect("Failed to execute context-creator");
assert!(
!output.status.success(),
"Expected context-creator to fail, but it succeeded.\nstdout: {}",
String::from_utf8_lossy(&output.stdout)
);
String::from_utf8_lossy(&output.stderr).to_string()
}
pub fn count_occurrences(output: &str, pattern: &str) -> usize {
output.matches(pattern).count()
}
pub fn extract_file_content(output: &str, file_name: &str) -> Option<String> {
let file_header = format!("## {file_name}");
if let Some(start_idx) = output.find(&file_header) {
let content_start = start_idx + file_header.len();
let content = if let Some(next_file_idx) = output[content_start..].find("## ") {
&output[content_start..content_start + next_file_idx]
} else {
&output[content_start..]
};
Some(content.trim().to_string())
} else {
None
}
}