//! Every `--json` output of the CLI parses as JSON and carries the fields //! the VS Code extension relies on (oxygen-vscode/docs/spec.md section 4). use serde_json::Value; use std::path::Path; use std::process::Command; fn oxy(dir: &Path, home: &Path, args: &[&str]) -> (i32, Vec, String) { let out = Command::new(env!("CARGO_BIN_EXE_oxy")).arg("--plain").args(args).current_dir(dir).env("OXY_HOME", home).env("NO_COLOR", "1").output().unwrap(); (out.status.code().unwrap_or(-1), out.stdout, String::from_utf8_lossy(&out.stderr).to_string()) } fn json(dir: &Path, home: &Path, args: &[&str]) -> Value { let (code, out, err) = oxy(dir, home, args); assert!(code == 0 || code == 2 || code == 1, "oxy {args:?} exited {code}: {err}"); serde_json::from_slice(&out).unwrap_or_else(|e| panic!("oxy {args:?} is not JSON: {e}\n{}", String::from_utf8_lossy(&out))) } fn write(dir: &Path, rel: &str, content: &str) { let p = dir.join(rel); std::fs::create_dir_all(p.parent().unwrap()).unwrap(); std::fs::write(p, content).unwrap(); } #[test] fn machine_interface_round_trips() { let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let origin = tmp.path().join("origin"); assert_eq!(oxy(tmp.path(), &home, &["init", origin.to_str().unwrap()]).0, 0); let repo = tmp.path().join("repo"); assert_eq!(oxy(tmp.path(), &home, &["clone", origin.to_str().unwrap(), repo.to_str().unwrap()]).0, 0); let r = repo.as_path(); assert_eq!(oxy(r, &home, &["identity", "create", "--name", "Ada", "--email", "ada@example.org"]).0, 0); assert_eq!(oxy(r, &home, &["config", "--user", "core.sign", "change"]).0, 0); let v = json(r, &home, &["version", "--json"]); assert_eq!(v["protocol"], 1); let root = json(r, &home, &["root", "--json"]); assert_eq!(root["bookmark"], "main"); assert!(root["root"].as_str().unwrap().ends_with("repo")); write(r, "a.txt", "1\n2\n3\n"); write(r, "b.txt", "b\n"); let st = json(r, &home, &["status", "--json"]); assert_eq!(st["paths"].as_array().unwrap().len(), 2); assert_eq!(oxy(r, &home, &["commit", "-m", "base"]).0, 0); write(r, "a.txt", "0\n1\n2\n3\n"); write(r, "b.txt", "b\nbb\n"); write(r, "c.txt", "c\n"); // diff and hunks agree on the working copy. let d = json(r, &home, &["diff", "--json"]); let files = d["files"].as_array().unwrap(); assert_eq!(files.len(), 3, "{d}"); let a = files.iter().find(|f| f["path"] == "a.txt").unwrap(); assert_eq!(a["kind"], "modified"); assert_eq!(a["hunks"][0]["base_line"], 0); assert_eq!(a["hunks"][0]["added"][0], "0\n"); let c = files.iter().find(|f| f["path"] == "c.txt").unwrap(); assert_eq!(c["kind"], "added"); let hunks = json(r, &home, &["hunks", "--json"]); let hunks = hunks.as_array().unwrap(); assert!(hunks.len() >= 3, "{hunks:?}"); let b_hunk = hunks.iter().find(|h| h["path"] == "b.txt").unwrap()["index"].as_u64().unwrap(); // Commit only b's hunk; discard c by path; then discard a's remaining hunk. assert_eq!(oxy(r, &home, &["commit", "-m", "b only", "--hunks", &b_hunk.to_string()]).0, 0); assert_eq!(oxy(r, &home, &["discard", "c.txt"]).0, 0); assert!(!r.join("c.txt").exists()); let hunks = json(r, &home, &["hunks", "--json"]); let a_hunk = hunks.as_array().unwrap().iter().find(|h| h["path"] == "a.txt").unwrap()["index"].as_u64().unwrap(); assert_eq!(oxy(r, &home, &["discard", "--hunks", &a_hunk.to_string()]).0, 0); assert_eq!(std::fs::read_to_string(r.join("a.txt")).unwrap(), "1\n2\n3\n"); let st = json(r, &home, &["status", "--json"]); assert_eq!(st["clean"], true, "{st}"); // cat serves file content at states. let (code, out, _) = oxy(r, &home, &["cat", "b.txt"]); assert_eq!((code, out.as_slice()), (0, b"b\nbb\n".as_slice())); let (code, out, _) = oxy(r, &home, &["cat", "b.txt", "-r", "main"]); assert_eq!((code, out.as_slice()), (0, b"b\nbb\n".as_slice())); // log, show, verify, op log, bookmarks, remotes, sync. let log = json(r, &home, &["log", "--json"]); let changes = log["changes"].as_array().unwrap(); assert_eq!(changes.len(), 3, "working copy plus two changes: {log}"); let head = changes.iter().find(|c| c["message"] == "b only").unwrap(); assert_eq!(head["signature"]["status"], "signed"); assert_eq!(head["author"], "Ada"); let hash = head["hash"].as_str().unwrap().to_string(); let by_path = json(r, &home, &["log", "--json", "--path", "b.txt"]); assert_eq!(by_path["changes"].as_array().unwrap().len(), 2, "{by_path}"); let show = json(r, &home, &["show", "--json", &format!("#{}", &hash[..12])]); assert_eq!(show["files"][0]["path"], "b.txt"); let verify = json(r, &home, &["verify", "--json"]); assert!(verify.as_array().unwrap().iter().all(|v| v["status"] == "signed")); let ops = json(r, &home, &["op", "log", "--json"]); assert!(ops[0]["current"] == true && ops.as_array().unwrap().len() > 3); assert_eq!(oxy(r, &home, &["bookmark", "create", "feature"]).0, 0); let bms = json(r, &home, &["bookmark", "list", "--json"]); assert_eq!(bms.as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect::>(), vec!["feature", "main"]); let remotes = json(r, &home, &["remote", "list", "--json"]); assert_eq!(remotes[0]["name"], "origin"); let push = json(r, &home, &["push", "--json"]); assert_eq!(push[0]["ok"], true, "{push}"); let fetch = json(r, &home, &["fetch", "--json"]); assert_eq!(fetch["remote"], "origin"); let pull = json(r, &home, &["pull", "--json"]); assert_eq!(pull["changed"], false); assert_eq!(json(r, &home, &["resolve", "--json"])["conflicted"], false); assert!(json(r, &home, &["submodule", "status", "--json"]).as_array().unwrap().is_empty()); assert!(json(r, &home, &["bisect", "status", "--json"]).is_null()); assert!(json(r, &home, &["policy", "show", "--json"]).is_null()); assert_eq!(oxy(r, &home, &["policy", "init"]).0, 0); let policy = json(r, &home, &["policy", "show", "--json"]); assert_eq!(policy["roots"][0]["name"], "Ada"); assert_eq!(oxy(r, &home, &["tag", "create", "v1", "-m", "first tag"]).0, 0); let tags = json(r, &home, &["tag", "list", "--json"]); assert_eq!((tags[0]["name"].as_str(), tags[0]["message"].as_str()), (Some("v1"), Some("first tag"))); let ws = json(r, &home, &["workspace", "list", "--json"]); assert_eq!((ws.as_array().unwrap().len(), ws[0]["current"].as_bool()), (1, Some(true))); }