//! The merge commands through the engine with pending outcomes: //! `git.merge` up to date, with conflicts and refused; `git.conflictVersions`; //! `git.markResolved` and `git.mergeAbort`, each followed by `gitChanged`; //! bad params. 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 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() } /// Commits everything. fn commit(dir: &Path, file: &str, text: &str, message: &str) { std::fs::write(dir.join(file), text).unwrap(); git(dir, &["add", "-A"]); git(dir, &["commit", "-q", "-m", message]); } /// A repository where `main` and `feature` changed the same line of /// `a.php`. fn conflicting(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]); } commit(dir, "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) } /// How many `gitChanged` events arrived since the last call. fn git_changes(&self) -> usize { let mut events = self.events.lock().unwrap(); let count = events .iter() .filter(|e| matches!(e, Event::GitChanged(_))) .count(); events.clear(); count } } #[test] fn merge_conflict_versions_resolve_and_abort() { if !have_git() { return; } let dir = tempfile::tempdir().unwrap(); let root = dir.path(); conflicting(root); let host = Host::open(root); host.git_changes(); let merged = host.run("git.merge", json!({"branch": "feature"})).unwrap(); assert_eq!( merged, json!({"result": "conflicts", "conflicts": ["a.php"]}) ); assert_eq!(host.git_changes(), 1); let versions = host .run("git.conflictVersions", json!({"path": "a.php"})) .unwrap(); assert_eq!( versions, json!({ "base": "