use std::path::PathBuf;
use std::process::Command;
use std::sync::atomic::{AtomicU32, Ordering};
use rag_rat_core::Config;
static TEMP_COUNTER: AtomicU32 = AtomicU32::new(0);
fn unique_temp_root() -> PathBuf {
let id = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!("rag-rat-init-yes-{}-{id}", std::process::id()))
}
fn git_init(dir: &std::path::Path) {
for args in
[&["init", "-q"][..], &["config", "user.email", "t@e.com"], &["config", "user.name", "t"]]
{
let out = Command::new("git").arg("-C").arg(dir).args(args).output().unwrap();
assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr));
}
}
#[test]
fn init_yes_writes_a_config_that_config_load_accepts() {
let root = unique_temp_root();
let cache = root.join("model-cache");
std::fs::create_dir_all(root.join("src")).unwrap();
std::fs::create_dir_all(&cache).unwrap();
std::fs::write(root.join("src/lib.rs"), "pub fn alpha() -> u32 {\n 1\n}\n").unwrap();
git_init(&root);
let output = Command::new(env!("CARGO_BIN_EXE_rag-rat"))
.args(["init", "--yes"])
.current_dir(&root)
.env("RAG_RAT_HOOK_DISABLE", "1")
.env("RAG_RAT_NO_WATCH", "1")
.env("RAG_RAT_MODEL_CACHE", &cache)
.env("HOME", &root)
.env("XDG_CACHE_HOME", &cache)
.output()
.expect("run rag-rat init --yes");
assert!(
output.status.success(),
"init --yes must succeed; stderr:\n{}",
String::from_utf8_lossy(&output.stderr)
);
let config_path = root.join("rag-rat.toml");
assert!(config_path.exists(), "init --yes must write rag-rat.toml");
let config = Config::load(&config_path).expect("Config::load must accept the written config");
assert!(
config.targets.iter().any(|t| t.language == rag_rat_core::language::Language::Rust),
"the rust source tree must bind a rust target, got: {:?}",
config.targets
);
std::fs::remove_dir_all(&root).ok();
}