use std::fs;
use std::path::PathBuf;
use reference_query::index::index_path;
use reference_query::store::Store;
fn scratch_dir() -> PathBuf {
let dir = std::env::temp_dir().join(format!("rq-it-{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
dir
}
#[test]
fn indexes_a_directory_of_ruby_end_to_end() {
let dir = scratch_dir();
fs::write(
dir.join("refund.rb"),
"module Billing\n class RefundProcessor\n def perform\n end\n end\nend\n",
)
.unwrap();
fs::write(dir.join("notes.txt"), "ignore me").unwrap();
let mut store = Store::open_in_memory().unwrap();
let stats = index_path(&mut store, &dir).unwrap();
assert_eq!(stats.files_seen, 1, "only the .rb file is a known language");
assert_eq!(stats.files_indexed, 1);
assert_eq!(stats.symbols, 3);
let overview = store.coverage_overview().unwrap();
assert_eq!(overview.len(), 1);
assert_eq!(overview[0].symbols, 3);
assert_eq!(overview[0].status, "complete");
fs::remove_dir_all(&dir).ok();
}
fn git(dir: &PathBuf, args: &[&str]) -> String {
let out = std::process::Command::new("git")
.arg("-C")
.arg(dir)
.args(args)
.output()
.unwrap();
assert!(out.status.success(), "git {args:?} failed");
String::from_utf8(out.stdout).unwrap().trim().to_string()
}
#[test]
fn commit_times_capture_survives_incremental_reindex() {
let dir = std::env::temp_dir().join(format!("rq-it-git-{}", std::process::id()));
fs::remove_dir_all(&dir).ok();
fs::create_dir_all(&dir).unwrap();
git(&dir, &["init", "-q"]);
git(&dir, &["config", "user.email", "t@example.com"]);
git(&dir, &["config", "user.name", "t"]);
fs::write(dir.join("alpha.rb"), "class Alpha\nend\n").unwrap();
git(&dir, &["add", "."]);
git(&dir, &["commit", "-qm", "one"]);
let mut store = Store::open_in_memory().unwrap();
index_path(&mut store, &dir).unwrap();
fs::write(dir.join("beta.rb"), "class Beta\nend\n").unwrap();
git(&dir, &["add", "."]);
git(&dir, &["commit", "-qm", "two"]);
index_path(&mut store, &dir).unwrap();
let ts_of = |name: &str| {
store
.search_candidates(name, 10, false)
.unwrap()
.into_iter()
.find(|c| c.name.to_lowercase() == name)
.unwrap()
.git_ts
};
let alpha = ts_of("alpha").expect("alpha has a commit time");
let beta = ts_of("beta").expect("beta has a commit time");
assert!(beta >= alpha);
let identity = store.coverage_overview().unwrap()[0].identity.clone();
let repo_id = store.repository_id(&identity).unwrap().unwrap();
assert_eq!(
store.git_ts_head(repo_id).unwrap().as_deref(),
Some(git(&dir, &["rev-parse", "HEAD"]).as_str())
);
fs::remove_dir_all(&dir).ok();
}