//! The `git.*` commands through the engine, off the lock with pending //! outcomes: status, stage, commit, show, branches, checkout, branch //! creation, `gitChanged` after changes and from the `.git` watch, a //! project outside any repository and a missing git. Skips with a message //! when git is not installed. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] use std::path::Path; use std::process::Command; use std::sync::Mutex; use std::time::{Duration, Instant}; use lutra_engine::{Engine, EngineError, Event, run_waiting}; use serde_json::{Value, json}; /// Whether git is installed; prints why a test is skipped. fn have_git() -> bool { let ok = Command::new("git") .arg("--version") .output() .is_ok_and(|o| o.status.success()); if !ok { eprintln!("skipped: git is not installed"); } ok } /// Runs git for test setup. fn git(dir: &Path, args: &[&str]) -> String { let out = Command::new("git") .args(args) .current_dir(dir) .output() .unwrap(); assert!( out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr) ); String::from_utf8_lossy(&out.stdout).trim().to_string() } /// A repository with one commit on `main`. fn repo(dir: &Path) { git(dir, &["init", "-q", "-b", "main"]); for (key, value) in [ ("user.name", "Lutra Test"), ("user.email", "test@lutra.invalid"), ("commit.gpgsign", "false"), ("core.autocrlf", "false"), ("core.hooksPath", ".git/hooks"), ] { git(dir, &["config", key, value]); } std::fs::write(dir.join("a.php"), ", /// Events published so far. events: Mutex>, } impl Host { /// An engine with `root` open as the project. fn open(root: &Path) -> Self { let host = Self { engine: Mutex::new(Engine::new().unwrap()), events: Mutex::new(Vec::new()), }; host.run("project.open", json!({"path": root})).unwrap(); host } /// Runs a command the way hosts do, waiting without the lock. fn run(&self, command: &str, params: Value) -> Result { run_waiting( || self.engine.lock().unwrap(), command, params, |events| self.events.lock().unwrap().extend_from_slice(events), ) .map(|o| o.value) } /// Takes the `gitChanged` events seen so far. fn git_changes(&self) -> Vec { let mut events = self.events.lock().unwrap(); let taken: Vec = events .iter() .filter(|e| matches!(e, Event::GitChanged(_))) .map(|e| serde_json::to_value(e).unwrap()) .collect(); events.clear(); taken } /// Pumps until a `gitChanged` arrives or `limit` passes. fn pump_for_change(&self, limit: Duration) -> Option { let deadline = Instant::now() + limit; while Instant::now() < deadline { let events = self.engine.lock().unwrap().pump_vfs().unwrap(); self.events.lock().unwrap().extend(events); if let Some(change) = self.git_changes().into_iter().next() { return Some(change); } std::thread::sleep(Duration::from_millis(20)); } None } } #[test] fn status_stage_commit_show_and_branches() { if !have_git() { return; } let dir = tempfile::tempdir().unwrap(); repo(dir.path()); let host = Host::open(dir.path()); let status = host.run("git.status", Value::Null).unwrap(); assert_eq!(status["available"], true); assert_eq!(status["repo"]["branch"], "main"); assert_eq!(status["repo"]["detached"], false); assert_eq!(status["files"], json!([])); std::fs::write(dir.path().join("a.php"), "