//! The remote `git.*` commands through the engine (M4.10), against local //! bare repositories only: clone without a project, remotes with redacted //! URLs, push with upstream, fetch and pull, `gitProgress` and //! `gitChanged`, cancel of a running push, one network call at a time, //! and 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::{Arc, 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() } /// Gives a work tree a local identity and no line-ending conversion. fn configure(dir: &Path) { for (key, value) in [ ("user.name", "Lutra Test"), ("user.email", "test@lutra.invalid"), ("commit.gpgsign", "false"), ("core.autocrlf", "false"), ] { git(dir, &["config", key, value]); } } /// Writes `name` and commits it. fn commit(dir: &Path, name: &str, text: &str) { std::fs::write(dir.join(name), text).unwrap(); git(dir, &["add", "--", name]); git(dir, &["commit", "-q", "-m", &format!("change {name}")]); } /// A bare repository `remote.git` under `root` with one commit on `main`. fn bare(root: &Path) -> std::path::PathBuf { let bare = root.join("remote.git"); std::fs::create_dir(&bare).unwrap(); git(&bare, &["init", "-q", "--bare", "-b", "main"]); let seed = root.join("seed"); std::fs::create_dir(&seed).unwrap(); git(&seed, &["init", "-q", "-b", "main"]); configure(&seed); commit(&seed, "a.php", ", /// Events published so far. events: Mutex>, } impl Host { /// An engine without a project. fn new() -> Arc { Arc::new(Self { engine: Mutex::new(Engine::new().unwrap()), events: Mutex::new(Vec::new()), }) } /// 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) } /// Pumps once and keeps the events. fn pump(&self) { let events = self.engine.lock().unwrap().pump_vfs().unwrap(); self.events.lock().unwrap().extend(events); } /// The events of `kind` seen so far, as JSON. fn seen(&self, kind: &str) -> Vec { self.events .lock() .unwrap() .iter() .map(|e| serde_json::to_value(e).unwrap()) .filter(|e| e["kind"] == kind) .collect() } } #[test] fn clone_open_remotes_push_fetch_and_pull() { if !have_git() { return; } let tmp = tempfile::tempdir().unwrap(); let bare = bare(tmp.path()); let host = Host::new(); let dir = tmp.path().join("work"); let cloned = host .run( "git.clone", json!({"url": bare.to_str().unwrap(), "dir": dir.to_str().unwrap()}), ) .unwrap(); assert!(cloned["root"].as_str().unwrap().ends_with("work")); let progress = host.seen("gitProgress"); assert_eq!(progress.last().unwrap()["active"], false); assert_eq!(progress.last().unwrap()["operation"], "clone"); configure(&dir); git(&dir, &["checkout-index", "-f", "-u", "-a"]); host.run("project.open", json!({"path": dir})).unwrap(); host.run( "git.remoteAdd", json!({"name": "fork", "url": "https://me:t0ken@example.invalid/x.git"}), ) .unwrap(); let remotes = host.run("git.remotes", json!({})).unwrap(); let list = remotes["remotes"].as_array().unwrap(); assert_eq!(list[0]["name"], "origin"); assert_eq!(list[1]["fetchUrl"], "https://***@example.invalid/x.git"); assert!(!remotes.to_string().contains("t0ken")); host.run( "git.remoteSetUrl", json!({"name": "fork", "url": "git@example.invalid:x.git"}), ) .unwrap(); host.run("git.remoteRemove", json!({"name": "fork"})) .unwrap(); assert_eq!( host.run("git.remotes", json!({})).unwrap()["remotes"] .as_array() .unwrap() .len(), 1 ); git(&dir, &["switch", "-q", "-c", "feature"]); commit(&dir, "b.php", "