use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
#[test]
fn test_required_terms_with_filename_matching() {
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path();
let file1_path = temp_path.join("file1.rs");
let file2_path = temp_path.join("file2.rs");
let file3_path = temp_path.join("file3.rs");
let load_go_path = temp_path.join("load.go");
let file1_content = r#"
fn main() {
let api = get_api();
api.load();
}
"#;
let file2_content = r#"
fn main() {
let data = load();
process(data);
}
"#;
let file3_content = r#"
fn main() {
let api = get_api();
let data = api.load();
process(data);
}
"#;
let load_go_content = r#"
func main() {
process(data);
}
"#;
File::create(&file1_path)
.unwrap()
.write_all(file1_content.as_bytes())
.unwrap();
File::create(&file2_path)
.unwrap()
.write_all(file2_content.as_bytes())
.unwrap();
File::create(&file3_path)
.unwrap()
.write_all(file3_content.as_bytes())
.unwrap();
File::create(&load_go_path)
.unwrap()
.write_all(load_go_content.as_bytes())
.unwrap();
let output = std::process::Command::new("cargo")
.args([
"run",
"--",
"search",
"api +load +process",
temp_path.to_str().unwrap(),
])
.env("DEBUG", "1")
.env("RUST_BACKTRACE", "1")
.output()
.expect("Failed to execute command");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("STDOUT: {stdout}");
println!("STDERR: {stderr}");
let file_names: Vec<&str> = stdout
.lines()
.filter(|line| line.contains("File:"))
.collect();
assert!(
!file_names.iter().any(|&name| name.contains("file1.rs")),
"Should NOT find file1.rs which contains 'api' and 'load' but not 'process'"
);
assert!(
file_names.iter().any(|&name| name.contains("file2.rs")),
"Should find file2.rs which contains 'load' and 'process' but not 'api'"
);
assert!(
file_names.iter().any(|&name| name.contains("file3.rs")),
"Should find file3.rs which contains all three terms"
);
assert!(
file_names.iter().any(|&name| name.contains("load.go")),
"Should find load.go which has 'process' in content and 'load' in filename"
);
}