|
| 1 | +// code2prompt/crates/code2prompt/tests/template_integration_test.rs |
| 2 | + |
| 3 | +use assert_cmd::Command; |
| 4 | +use log::{debug, info}; |
| 5 | +use predicates::prelude::*; |
| 6 | +use predicates::str::contains; |
| 7 | +use std::fs::{self, File}; |
| 8 | +use std::io::Write; |
| 9 | +use std::path::Path; |
| 10 | +use std::sync::Once; |
| 11 | +use tempfile::tempdir; |
| 12 | + |
| 13 | +static INIT: Once = Once::new(); |
| 14 | + |
| 15 | +fn init_logger() { |
| 16 | + INIT.call_once(|| { |
| 17 | + env_logger::builder() |
| 18 | + .is_test(true) |
| 19 | + .filter_level(log::LevelFilter::Debug) |
| 20 | + .try_init() |
| 21 | + .expect("Failed to initialize logger"); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +fn create_temp_file(dir: &Path, name: &str, content: &str) -> std::path::PathBuf { |
| 26 | + let file_path = dir.join(name); |
| 27 | + let parent_dir = file_path.parent().unwrap(); |
| 28 | + fs::create_dir_all(parent_dir).expect(&format!("Failed to create directory: {:?}", parent_dir)); |
| 29 | + let mut file = |
| 30 | + File::create(&file_path).expect(&format!("Failed to create temp file: {:?}", file_path)); |
| 31 | + writeln!(file, "{}", content).expect(&format!("Failed to write to temp file: {:?}", file_path)); |
| 32 | + file_path |
| 33 | +} |
| 34 | + |
| 35 | +fn create_test_codebase(base_path: &Path) { |
| 36 | + // Create a simple code structure for testing templates |
| 37 | + let files = vec![ |
| 38 | + ( |
| 39 | + "src/main.rs", |
| 40 | + "fn main() {\n println!(\"Hello, world!\");\n}", |
| 41 | + ), |
| 42 | + ( |
| 43 | + "src/lib.rs", |
| 44 | + "pub fn add(a: i32, b: i32) -> i32 {\n a + b\n}", |
| 45 | + ), |
| 46 | + ( |
| 47 | + "tests/test.rs", |
| 48 | + "#[test]\nfn test_add() {\n assert_eq!(3, add(1, 2));\n}", |
| 49 | + ), |
| 50 | + ]; |
| 51 | + |
| 52 | + for (file_path, content) in files { |
| 53 | + create_temp_file(base_path, file_path, content); |
| 54 | + } |
| 55 | + info!("Test codebase created"); |
| 56 | +} |
| 57 | + |
| 58 | +fn read_output_file(file_path: &Path) -> String { |
| 59 | + fs::read_to_string(file_path).expect(&format!("Failed to read output file: {:?}", file_path)) |
| 60 | +} |
| 61 | + |
| 62 | +mod template_tests { |
| 63 | + use super::*; |
| 64 | + use tempfile::TempDir; |
| 65 | + |
| 66 | + struct TestEnv { |
| 67 | + dir: TempDir, |
| 68 | + output_file: std::path::PathBuf, |
| 69 | + } |
| 70 | + |
| 71 | + impl TestEnv { |
| 72 | + fn new() -> Self { |
| 73 | + init_logger(); |
| 74 | + let dir = tempdir().unwrap(); |
| 75 | + create_test_codebase(dir.path()); |
| 76 | + let output_file = dir.path().join("output.txt"); |
| 77 | + TestEnv { dir, output_file } |
| 78 | + } |
| 79 | + |
| 80 | + fn command(&self) -> Command { |
| 81 | + let mut cmd = |
| 82 | + Command::cargo_bin("code2prompt").expect("Failed to find code2prompt binary"); |
| 83 | + cmd.arg(&self.dir.path().to_str().unwrap()) |
| 84 | + .arg("--output-file") |
| 85 | + .arg(&self.output_file.to_str().unwrap()) |
| 86 | + .arg("--no-clipboard"); |
| 87 | + cmd |
| 88 | + } |
| 89 | + |
| 90 | + fn read_output(&self) -> String { |
| 91 | + read_output_file(&self.output_file) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_markdown_template() { |
| 97 | + let env = TestEnv::new(); |
| 98 | + let mut cmd = env.command(); |
| 99 | + cmd.arg("--output-format=markdown").assert().success(); |
| 100 | + |
| 101 | + let output = env.read_output(); |
| 102 | + debug!("Markdown template output:\n{}", output); |
| 103 | + |
| 104 | + // Check markdown-specific formatting |
| 105 | + assert!(contains("Source Tree:").eval(&output)); |
| 106 | + assert!(contains("```rs").eval(&output)); |
| 107 | + assert!(contains("fn main()").eval(&output)); |
| 108 | + assert!(contains("Hello, world!").eval(&output)); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_xml_template() { |
| 113 | + let env = TestEnv::new(); |
| 114 | + let mut cmd = env.command(); |
| 115 | + cmd.arg("--output-format=xml").assert().success(); |
| 116 | + |
| 117 | + let output = env.read_output(); |
| 118 | + debug!("XML template output:\n{}", output); |
| 119 | + |
| 120 | + // Check XML-specific formatting |
| 121 | + assert!(contains("<directory>").eval(&output)); |
| 122 | + assert!(contains("</file>").eval(&output)); |
| 123 | + assert!(contains(".rs\"").eval(&output)); |
| 124 | + assert!(contains("fn main()").eval(&output)); |
| 125 | + assert!(contains("Hello, world!").eval(&output)); |
| 126 | + } |
| 127 | + |
| 128 | + // #[test] |
| 129 | + // fn test_custom_template_with_variables() { |
| 130 | + // let env = TestEnv::new(); |
| 131 | + |
| 132 | + // // Create a custom template with variables |
| 133 | + // let template_content = r#" |
| 134 | + // # {{project_name}} Code Review |
| 135 | + |
| 136 | + // Author: {{author_name}} |
| 137 | + // Purpose: {{purpose}} |
| 138 | + |
| 139 | + // ## Files |
| 140 | + |
| 141 | + // {{#each files}} |
| 142 | + // ### {{path}} |
| 143 | + |
| 144 | + // ```{{extension}} |
| 145 | + // {{code}} |
| 146 | + // {{/each}} |
| 147 | + // "#; |
| 148 | + // let template_path = |
| 149 | + // create_temp_file(&env.dir.path(), "custom_template.hbs", template_content); |
| 150 | + |
| 151 | + // // Use command-line arguments to provide variables instead of stdin |
| 152 | + // let mut cmd = env.command(); |
| 153 | + // cmd.arg("--template") |
| 154 | + // .arg(template_path.to_str().unwrap()) |
| 155 | + // // Add user variables as command-line arguments |
| 156 | + // .arg("--var") |
| 157 | + // .arg("project_name=Test Project") |
| 158 | + // .arg("--var") |
| 159 | + // .arg("author_name=John Doe") |
| 160 | + // .arg("--var") |
| 161 | + // .arg("purpose=Demonstrating custom templates") |
| 162 | + // .assert() |
| 163 | + // .success(); |
| 164 | + |
| 165 | + // let output = env.read_output(); |
| 166 | + // debug!("Custom template output:\n{}", output); |
| 167 | + |
| 168 | + // // Check custom template formatting and variables |
| 169 | + // assert!(contains("# Test Project Code Review").eval(&output)); |
| 170 | + // assert!(contains("Author: John Doe").eval(&output)); |
| 171 | + // assert!(contains("Purpose: Demonstrating custom templates").eval(&output)); |
| 172 | + // assert!(contains("### src/main.rs").eval(&output)); |
| 173 | + // assert!(contains("fn main()").eval(&output)); |
| 174 | + // assert!(contains("Hello, world!").eval(&output)); |
| 175 | + // } |
| 176 | + |
| 177 | + // #[test] |
| 178 | + // fn test_json_output_format() { |
| 179 | + // let env = TestEnv::new(); |
| 180 | + // let mut cmd = env.command(); |
| 181 | + // cmd.arg("--output-format=json").assert().success(); |
| 182 | + |
| 183 | + // let output = env.read_output(); |
| 184 | + // debug!("JSON output format:\n{}", output); |
| 185 | + |
| 186 | + // // Even though JSON is an output format flag, the content is still markdown |
| 187 | + // // But the content should be structured to be machine-parseable |
| 188 | + // assert!(contains("# Code Analysis").eval(&output)); |
| 189 | + // assert!(contains("## File Tree").eval(&output)); |
| 190 | + // assert!(contains("```rust").eval(&output)); |
| 191 | + // assert!(contains("fn main()").eval(&output)); |
| 192 | + // } |
| 193 | + |
| 194 | + // #[test] |
| 195 | + // fn test_template_with_empty_variables() { |
| 196 | + // let env = TestEnv::new(); |
| 197 | + |
| 198 | + // // Create a custom template with optional variables |
| 199 | + // let template_content = r#" |
| 200 | + // Code Review {{#if project_name}}for {{project_name}}{{/if}} |
| 201 | + // {{#if notes}} |
| 202 | + // Notes: {{notes}} |
| 203 | + // {{else}} |
| 204 | + // No additional notes provided. |
| 205 | + // {{/if}} |
| 206 | + |
| 207 | + // Files |
| 208 | + // {{#each files}} |
| 209 | + |
| 210 | + // {{path}} |
| 211 | + // {{code}} |
| 212 | + // {{/each}} |
| 213 | + // "#; |
| 214 | + |
| 215 | + // let template_path = create_temp_file( |
| 216 | + // &env.dir.path(), |
| 217 | + // "optional_vars_template.hbs", |
| 218 | + // template_content, |
| 219 | + // ); |
| 220 | + |
| 221 | + // // Use command-line arguments with empty values instead of stdin |
| 222 | + // let mut cmd = env.command(); |
| 223 | + // cmd.arg("--template") |
| 224 | + // .arg(template_path.to_str().unwrap()) |
| 225 | + // .arg("--var") |
| 226 | + // .arg("project_name=") |
| 227 | + // .arg("--var") |
| 228 | + // .arg("notes=") |
| 229 | + // .assert() |
| 230 | + // .success(); |
| 231 | + |
| 232 | + // let output = env.read_output(); |
| 233 | + // debug!("Template with empty variables output:\n{}", output); |
| 234 | + |
| 235 | + // // Fix assertions to match the actual template format |
| 236 | + // assert!(contains("Code Review").eval(&output)); |
| 237 | + // assert!(contains("No additional notes provided.").eval(&output)); |
| 238 | + // assert!(contains("src/main.rs").eval(&output)); |
| 239 | + // } |
| 240 | +} |
0 commit comments