use pretty_assertions::assert_eq; use std::path::Path; use std::process::Command; use super::CodeIndex; fn git(dir: &Path, args: &[&str]) { let status = Command::new("git") .arg("-C") .arg(dir) .args(["-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgSign=false"]) .args(args) .output() .expect("git"); assert!(status.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&status.stderr)); } fn write(dir: &Path, path: &str, content: &str) { let full = dir.join(path); std::fs::create_dir_all(full.parent().expect("parent")).expect("mkdir"); std::fs::write(full, content).expect("write"); } #[test] fn incremental_updates_from_git() { let repo = tempfile::tempdir().expect("repo"); let home = tempfile::tempdir().expect("home"); let root = repo.path().canonicalize().expect("canonical"); git(&root, &["init", "-q"]); write(&root, "src/lib.rs", "pub fn alpha() { beta(); }\npub fn beta() {}\n"); write(&root, "src/other.rs", "use crate::alpha;\nfn gamma() { alpha(); }\n"); write(&root, "README.md", "# not indexed\n"); git(&root, &["add", "."]); git(&root, &["commit", "-q", "-m", "one"]); let mut index = CodeIndex::open_for_dir(home.path(), &root.join("src")).expect("open"); let report = index.update().expect("first"); assert_eq!((report.mode, report.parsed, report.files, report.symbols), ("full", 2, 2, 3)); let report = index.update().expect("no change"); assert_eq!((report.mode, report.parsed, report.checked), ("status", 0, 0)); // Working tree change, then untracked file. write(&root, "src/lib.rs", "pub fn alpha() { beta(); delta(); }\npub fn beta() {}\nfn delta() {}\n"); write(&root, "src/new.rs", "fn epsilon() { delta(); }\n"); let report = index.update().expect("dirty"); assert_eq!((report.mode, report.parsed, report.files, report.symbols), ("status", 2, 3, 5)); // Commit: the dirty files become clean with the same blob ids, nothing to parse. git(&root, &["add", "."]); git(&root, &["commit", "-q", "-m", "two"]); let report = index.update().expect("commit"); assert_eq!((report.mode, report.parsed), ("diff", 0)); // A file deleted in a commit disappears from the index. git(&root, &["rm", "-q", "src/new.rs"]); git(&root, &["commit", "-q", "-m", "three"]); let report = index.update().expect("delete"); assert_eq!((report.mode, report.removed, report.files), ("diff", 1, 2)); let callers = index.callers("delta", 500).expect("callers"); assert!(callers.contains("src/lib.rs:1 in function alpha"), "{callers}"); assert!(!callers.contains("new.rs"), "{callers}"); let callees = index.callees("alpha", 500).expect("callees"); assert!(callees.contains("- beta (line 1) → function src/lib.rs:2"), "{callees}"); let related = index.related("src/other.rs", "", 500).expect("related"); assert!(related.contains("- src/lib.rs (2: alpha)"), "{related}"); let map = index.module_map("src", "", 500).expect("map"); assert!(map.contains("src/lib.rs (rust, 3 lines): function alpha, function beta, function delta"), "{map}"); let definition = index.definition("beta", 5, 500).expect("definition"); assert!(definition.contains("function beta — src/lib.rs:2"), "{definition}"); let search = index.search("alp", None, None, 10, 500).expect("search"); assert!(search.contains("function alpha — src/lib.rs:1"), "{search}"); assert!(index.repo_summary(200).expect("summary").starts_with("Code map: 2 indexed files (rust 2)")); }