OXYGENHUB
# e02bf0cd0533 oxygen / tests / cli_json.rs
6.1 KiB · Rust · last changed Machine interface for editors (spec 0.8b): oxy root, cat, version, hunks, discard by path or hunk; --json on log (with --path), show, diff, resolve, op log, bookmark list, remote list, fetch, pull, push, submodule status, bisect status, verify, policy show; commit --hunks; integration test over every JSON command 1 month ago source blame history raw
1
//! Every `--json` output of the CLI parses as JSON and carries the fields
2
//! the VS Code extension relies on (oxygen-vscode/docs/spec.md section 4).
3
4
use serde_json::Value;
5
use std::path::Path;
6
use std::process::Command;
7
8
fn oxy(dir: &Path, home: &Path, args: &[&str]) -> (i32, Vec<u8>, String) {
9
    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();
10
    (out.status.code().unwrap_or(-1), out.stdout, String::from_utf8_lossy(&out.stderr).to_string())
11
}
12
13
fn json(dir: &Path, home: &Path, args: &[&str]) -> Value {
14
    let (code, out, err) = oxy(dir, home, args);
15
    assert!(code == 0 || code == 2 || code == 1, "oxy {args:?} exited {code}: {err}");
16
    serde_json::from_slice(&out).unwrap_or_else(|e| panic!("oxy {args:?} is not JSON: {e}\n{}", String::from_utf8_lossy(&out)))
17
}
18
19
fn write(dir: &Path, rel: &str, content: &str) {
20
    let p = dir.join(rel);
21
    std::fs::create_dir_all(p.parent().unwrap()).unwrap();
22
    std::fs::write(p, content).unwrap();
23
}
24
25
#[test]
26
fn machine_interface_round_trips() {
27
    let tmp = tempfile::tempdir().unwrap();
28
    let home = tmp.path().join("home");
29
    std::fs::create_dir_all(&home).unwrap();
30
    let origin = tmp.path().join("origin");
31
    assert_eq!(oxy(tmp.path(), &home, &["init", origin.to_str().unwrap()]).0, 0);
32
    let repo = tmp.path().join("repo");
33
    assert_eq!(oxy(tmp.path(), &home, &["clone", origin.to_str().unwrap(), repo.to_str().unwrap()]).0, 0);
34
    let r = repo.as_path();
35
    assert_eq!(oxy(r, &home, &["identity", "create", "--name", "Ada", "--email", "[email protected]"]).0, 0);
36
    assert_eq!(oxy(r, &home, &["config", "--user", "core.sign", "change"]).0, 0);
37
38
    let v = json(r, &home, &["version", "--json"]);
39
    assert_eq!(v["protocol"], 1);
40
    let root = json(r, &home, &["root", "--json"]);
41
    assert_eq!(root["bookmark"], "main");
42
    assert!(root["root"].as_str().unwrap().ends_with("repo"));
43
44
    write(r, "a.txt", "1\n2\n3\n");
45
    write(r, "b.txt", "b\n");
46
    let st = json(r, &home, &["status", "--json"]);
47
    assert_eq!(st["paths"].as_array().unwrap().len(), 2);
48
    assert_eq!(oxy(r, &home, &["commit", "-m", "base"]).0, 0);
49
    write(r, "a.txt", "0\n1\n2\n3\n");
50
    write(r, "b.txt", "b\nbb\n");
51
    write(r, "c.txt", "c\n");
52
53
    // diff and hunks agree on the working copy.
54
    let d = json(r, &home, &["diff", "--json"]);
55
    let files = d["files"].as_array().unwrap();
56
    assert_eq!(files.len(), 3, "{d}");
57
    let a = files.iter().find(|f| f["path"] == "a.txt").unwrap();
58
    assert_eq!(a["kind"], "modified");
59
    assert_eq!(a["hunks"][0]["base_line"], 0);
60
    assert_eq!(a["hunks"][0]["added"][0], "0\n");
61
    let c = files.iter().find(|f| f["path"] == "c.txt").unwrap();
62
    assert_eq!(c["kind"], "added");
63
    let hunks = json(r, &home, &["hunks", "--json"]);
64
    let hunks = hunks.as_array().unwrap();
65
    assert!(hunks.len() >= 3, "{hunks:?}");
66
    let b_hunk = hunks.iter().find(|h| h["path"] == "b.txt").unwrap()["index"].as_u64().unwrap();
67
    // Commit only b's hunk; discard c by path; then discard a's remaining hunk.
68
    assert_eq!(oxy(r, &home, &["commit", "-m", "b only", "--hunks", &b_hunk.to_string()]).0, 0);
69
    assert_eq!(oxy(r, &home, &["discard", "c.txt"]).0, 0);
70
    assert!(!r.join("c.txt").exists());
71
    let hunks = json(r, &home, &["hunks", "--json"]);
72
    let a_hunk = hunks.as_array().unwrap().iter().find(|h| h["path"] == "a.txt").unwrap()["index"].as_u64().unwrap();
73
    assert_eq!(oxy(r, &home, &["discard", "--hunks", &a_hunk.to_string()]).0, 0);
74
    assert_eq!(std::fs::read_to_string(r.join("a.txt")).unwrap(), "1\n2\n3\n");
75
    let st = json(r, &home, &["status", "--json"]);
76
    assert_eq!(st["clean"], true, "{st}");
77
78
    // cat serves file content at states.
79
    let (code, out, _) = oxy(r, &home, &["cat", "b.txt"]);
80
    assert_eq!((code, out.as_slice()), (0, b"b\nbb\n".as_slice()));
81
    let (code, out, _) = oxy(r, &home, &["cat", "b.txt", "-r", "main"]);
82
    assert_eq!((code, out.as_slice()), (0, b"b\nbb\n".as_slice()));
83
84
    // log, show, verify, op log, bookmarks, remotes, sync.
85
    let log = json(r, &home, &["log", "--json"]);
86
    let changes = log["changes"].as_array().unwrap();
87
    assert_eq!(changes.len(), 3, "working copy plus two changes: {log}");
88
    let head = changes.iter().find(|c| c["message"] == "b only").unwrap();
89
    assert_eq!(head["signature"]["status"], "signed");
90
    assert_eq!(head["author"], "Ada");
91
    let hash = head["hash"].as_str().unwrap().to_string();
92
    let by_path = json(r, &home, &["log", "--json", "--path", "b.txt"]);
93
    assert_eq!(by_path["changes"].as_array().unwrap().len(), 2, "{by_path}");
94
    let show = json(r, &home, &["show", "--json", &format!("#{}", &hash[..12])]);
95
    assert_eq!(show["files"][0]["path"], "b.txt");
96
    let verify = json(r, &home, &["verify", "--json"]);
97
    assert!(verify.as_array().unwrap().iter().all(|v| v["status"] == "signed"));
98
    let ops = json(r, &home, &["op", "log", "--json"]);
99
    assert!(ops[0]["current"] == true && ops.as_array().unwrap().len() > 3);
100
    assert_eq!(oxy(r, &home, &["bookmark", "create", "feature"]).0, 0);
101
    let bms = json(r, &home, &["bookmark", "list", "--json"]);
102
    assert_eq!(bms.as_array().unwrap().iter().map(|b| b["name"].as_str().unwrap()).collect::<Vec<_>>(), vec!["feature", "main"]);
103
    let remotes = json(r, &home, &["remote", "list", "--json"]);
104
    assert_eq!(remotes[0]["name"], "origin");
105
    let push = json(r, &home, &["push", "--json"]);
106
    assert_eq!(push[0]["ok"], true, "{push}");
107
    let fetch = json(r, &home, &["fetch", "--json"]);
108
    assert_eq!(fetch["remote"], "origin");
109
    let pull = json(r, &home, &["pull", "--json"]);
110
    assert_eq!(pull["changed"], false);
111
    assert_eq!(json(r, &home, &["resolve", "--json"])["conflicted"], false);
112
    assert!(json(r, &home, &["submodule", "status", "--json"]).as_array().unwrap().is_empty());
113
    assert!(json(r, &home, &["bisect", "status", "--json"]).is_null());
114
    assert!(json(r, &home, &["policy", "show", "--json"]).is_null());
115
    assert_eq!(oxy(r, &home, &["policy", "init"]).0, 0);
116
    let policy = json(r, &home, &["policy", "show", "--json"]);
117
    assert_eq!(policy["roots"][0]["name"], "Ada");
118
}