//! `git.lineChanges` through the engine against real temp repositories: //! added, modified and deleted lines of an edited buffer, a CRLF buffer //! against an LF index blob, an untracked file, the cache dropped by //! `gitChanged` after staging, a project outside any repository, and an //! unknown buffer. 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, 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]) { let out = Command::new("git") .args(args) .current_dir(dir) .output() .unwrap(); assert!( out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr) ); } /// A repository with `files` committed; `autocrlf` sets `core.autocrlf`. fn repo(dir: &Path, autocrlf: &str, files: &[(&str, &str)]) { git(dir, &["init", "-q", "-b", "main"]); for (key, value) in [ ("user.name", "Lutra Test"), ("user.email", "test@lutra.invalid"), ("commit.gpgsign", "false"), ("core.autocrlf", autocrlf), ("core.hooksPath", ".git/hooks"), ] { git(dir, &["config", key, value]); } for (name, text) in files { std::fs::write(dir.join(name), text).unwrap(); } git(dir, &["add", "-A"]); git(dir, &["commit", "-q", "-m", "base"]); } /// An engine behind a lock, like a host. struct Host { /// The engine. engine: 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()), }; 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, |_| {}).map(|o| o.value) } /// Opens `path` and returns its buffer id. fn open_file(&self, path: &str) -> u64 { self.run("file.open", json!({"path": path})).unwrap()["id"] .as_u64() .unwrap() } /// Replaces the whole text of buffer `id`. fn set_text(&self, id: u64, text: &str) { let current = self.run("buffer.get", json!({"id": id})).unwrap(); let len = current["text"].as_str().unwrap().encode_utf16().count(); self.run( "buffer.edit", json!({ "id": id, "baseVersion": current["version"], "edits": [{"from": 0, "to": len, "insert": text}], }), ) .unwrap(); } /// The ranges of `git.lineChanges` for buffer `id`. fn ranges(&self, id: u64) -> Value { self.run("git.lineChanges", json!({"id": id})).unwrap()["ranges"].clone() } } #[test] fn reports_added_modified_and_deleted_lines() { if !have_git() { return; } let dir = tempfile::tempdir().unwrap(); repo( dir.path(), "false", &[("a.php", "