//! 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 state = push[0]["state"].as_str().unwrap(); assert!( state.len() == 64 && state.chars().all(|c| c.is_ascii_hexdigit()), "the state the push moved the bookmark to: {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)) ); } /// `oxy commit -F`, `oxy describe` naming and tags as changes (feedback /// 4 and 6). #[test] fn commit_from_a_file_and_describe_for_builds() { let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let dir = tmp.path().join("r"); std::fs::create_dir_all(&dir).unwrap(); assert_eq!(oxy(&dir, &home, &["init"]).0, 0); write(&dir, "a.txt", "a\n"); write( tmp.path(), "msg.txt", "Subject from a file\n\nA body\nof two lines.\n", ); let msg = tmp.path().join("msg.txt"); let (code, _, err) = oxy(&dir, &home, &["commit", "-F", msg.to_str().unwrap()]); assert_eq!(code, 0, "{err}"); let log = json(&dir, &home, &["log", "--json", "-n", "2"]); assert_eq!( log["changes"][1]["message"], "Subject from a file\n\nA body\nof two lines." ); assert_ne!(oxy(&dir, &home, &["commit"]).0, 0, "a message is required"); // Untagged: bookmark and change; a tag names it; later work counts. let d = json(&dir, &home, &["describe", "--json"]); assert_eq!( (d["tag"].clone(), d["dirty"].clone()), (Value::Null, Value::Bool(false)) ); let change = d["change"].as_str().unwrap().to_string(); assert_eq!(d["describe"], format!("main-{}", &change[..8])); assert_eq!( oxy(&dir, &home, &["tag", "create", "v0.1.0", "-m", "first"]).0, 0 ); let (_, out, _) = oxy(&dir, &home, &["describe"]); assert_eq!(String::from_utf8_lossy(&out).trim(), "v0.1.0"); let (code, out, err) = oxy(&dir, &home, &["show", "v0.1.0"]); assert_eq!(code, 0, "a tag names its change: {err}"); assert!(String::from_utf8_lossy(&out).contains("Subject from a file")); write(&dir, "b.txt", "b\n"); let d = json(&dir, &home, &["describe", "--json"]); assert_eq!(d["describe"], "v0.1.0-dirty"); assert_eq!(oxy(&dir, &home, &["commit", "-m", "b"]).0, 0); write(&dir, "c.txt", "c\n"); assert_eq!(oxy(&dir, &home, &["commit", "-m", "c"]).0, 0); let d = json(&dir, &home, &["describe", "--json"]); let change = d["change"].as_str().unwrap(); assert_eq!(d["distance"], 2); assert_eq!(d["describe"], format!("v0.1.0-2-{}", &change[..8])); } /// `oxy log -L START,END:PATH`: the history of a range of lines. #[test] fn the_history_of_a_function() { let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let dir = tmp.path().join("r"); std::fs::create_dir_all(&dir).unwrap(); assert_eq!(oxy(&dir, &home, &["init"]).0, 0); write(&dir, "f.rs", "use x;\nfn a() {\n one();\n}\n"); assert_eq!(oxy(&dir, &home, &["commit", "-m", "add f"]).0, 0); write(&dir, "f.rs", "use y;\nfn a() {\n one();\n}\n"); assert_eq!(oxy(&dir, &home, &["commit", "-m", "import"]).0, 0); write(&dir, "f.rs", "use y;\nfn a() {\n two();\n}\n"); assert_eq!(oxy(&dir, &home, &["commit", "-m", "body"]).0, 0); let v = json(&dir, &home, &["log", "--json", "-L", "2,4:f.rs"]); let messages: Vec<&str> = v["changes"] .as_array() .unwrap() .iter() .map(|c| c["message"].as_str().unwrap()) .collect(); assert_eq!(messages, ["body", "add f"], "not the import"); let (code, _, err) = oxy(&dir, &home, &["log", "-L", "2:f.rs"]); assert_ne!(code, 0); assert!(err.contains("START,END:PATH"), "{err}"); } #[test] fn receipts_verify_import_list_show_and_export() { use oxy_core::hash::{ObjectRef, ObjectType}; use oxy_core::identity::SecretKey; use oxy_core::objects::StateObject; use oxy_core::receipt::{Receipt, SignedReceipt}; let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let r = tmp.path().join("repo"); assert_eq!(oxy(tmp.path(), &home, &["init", r.to_str().unwrap()]).0, 0); write(&r, "a.txt", "a\n"); assert_eq!(oxy(&r, &home, &["commit", "-m", "A"]).0, 0); let repo = oxy_core::repo::Repo::open(&r).unwrap(); let heads = repo.bookmark_heads().unwrap(); let state = oxy_core::hash::hash_object(ObjectType::State, &StateObject::new(heads).encode()); let receipt = Receipt { job_name: "test".into(), pipeline_hash: ObjectRef([1; 32]), scope_tree_hash: ObjectRef([2; 32]), inputs: vec![], environment: ObjectRef([3; 32]), outputs: vec![], exit_code: 0, duration_ms: 2500, }; let sk = SecretKey::generate(); let runner = sk.identity("earth-1", "runner@earth", 1); let sr = SignedReceipt::sign(receipt, repo.id().unwrap(), state, "demo#7", &runner, &sk); let file = tmp.path().join("r.receipt"); std::fs::write(&file, sr.encode()).unwrap(); let f = file.to_str().unwrap(); let v = json(&r, &home, &["receipt", "verify", f, "--json"]); assert_eq!( (v["valid"].as_bool(), v["job"].as_str(), v["run"].as_str()), (Some(true), Some("test"), Some("demo#7")) ); assert!(v["attest"].is_null()); assert!(json(&r, &home, &["receipt", "list", "--json"]) .as_array() .unwrap() .is_empty()); assert_eq!(oxy(&r, &home, &["receipt", "import", f]).0, 0); let list = json(&r, &home, &["receipt", "list", "--json"]); let list = list.as_array().unwrap(); assert_eq!(list.len(), 1); assert_eq!(list[0]["runner_name"], "earth-1"); assert_eq!(list[0]["state"], state.to_hex()); let attest = list[0]["attest"].as_str().unwrap().to_string(); let shown = json(&r, &home, &["receipt", "show", &attest[..10], "--json"]); assert_eq!(shown["duration_ms"], 2500); let out = tmp.path().join("out.receipt"); assert_eq!( oxy( &r, &home, &["receipt", "export", &attest[..10], out.to_str().unwrap()] ) .0, 0 ); assert_eq!(std::fs::read(&out).unwrap(), sr.encode()); // SLSA provenance is signed with the exporter's key: none yet, so it is // refused and says what to do. let prov = tmp.path().join("prov.json"); let p = prov.to_str().unwrap(); let slsa = ["receipt", "export", &attest[..10], p, "--format", "slsa"]; let (code, _, err) = oxy(&r, &home, &slsa); assert_ne!(code, 0); assert!(err.contains("oxy identity create"), "{err}"); let ident = [ "identity", "create", "--name", "Ada", "--email", "ada@example.org", ]; assert_eq!(oxy(&r, &home, &ident).0, 0); assert_eq!(oxy(&r, &home, &slsa).0, 0); let env: serde_json::Value = serde_json::from_slice(&std::fs::read(&prov).unwrap()).unwrap(); assert_eq!(env["payloadType"], oxy_core::dsse::IN_TOTO); let payload = oxy_core::dsse::base64_decode(env["payload"].as_str().unwrap()).unwrap(); let st: serde_json::Value = serde_json::from_slice(&payload).unwrap(); assert_eq!(st["predicateType"], "https://slsa.dev/provenance/v1"); assert_eq!(st["subject"][0]["digest"]["oxygen-state"], state.to_hex()); let run = &st["predicate"]["runDetails"]; assert_eq!(run["metadata"]["invocationId"], "demo#7"); let kept = oxy_core::dsse::base64_decode(run["byproducts"][0]["content"].as_str().unwrap()); assert_eq!( kept.unwrap(), sr.encode(), "the runner's receipt rides along" ); // It verifies with the exporter's key and its PEM holds that key. let repo = oxy_core::repo::Repo::open(&r) .unwrap() .with_home(home.clone()); let key = repo.signing_key().unwrap(); assert_eq!(env["signatures"][0]["keyid"], key.to_hex()); let me = repo.identity(&key).unwrap(); let sig = oxy_core::dsse::base64_decode(env["signatures"][0]["sig"].as_str().unwrap()).unwrap(); assert!(oxy_core::dsse::verify(&me, oxy_core::dsse::IN_TOTO, &payload, &sig).is_ok()); assert!(oxy_core::dsse::verify(&runner, oxy_core::dsse::IN_TOTO, &payload, &sig).is_err()); let pem = tmp.path().join("key.pem"); let pem_args = ["identity", "export", "--pem", pem.to_str().unwrap()]; assert_eq!(oxy(&r, &home, &pem_args).0, 0); assert_eq!( std::fs::read_to_string(&pem).unwrap(), oxy_core::dsse::public_pem(&me) ); // A receipt whose result was changed after signing does not verify. let mut forged = sr.clone(); forged.receipt.exit_code = 3; std::fs::write(&file, forged.encode()).unwrap(); let (code, out, _) = oxy(&r, &home, &["receipt", "verify", f]); assert_eq!(code, 1); assert!(String::from_utf8_lossy(&out).contains("different receipt")); let (code, _, err) = oxy(&r, &home, &["receipt", "import", f]); assert_ne!(code, 0); assert!(err.contains("does not verify"), "{err}"); } /// Metalgear, 2026-09-29 (oxygen#37): a bookmark created at one of three /// independent heads held only that change and what it depends on, and /// switching to it silently wrote a much older tree. #[test] fn a_bookmark_at_one_head_of_several_is_refused_unless_asked_for() { let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let r = tmp.path().join("repo"); assert_eq!(oxy(tmp.path(), &home, &["init", r.to_str().unwrap()]).0, 0); for f in ["a", "b", "c"] { write(&r, &format!("{f}.txt"), &format!("{f}\n")); assert_eq!(oxy(&r, &home, &["commit", "-m", f]).0, 0); } let bms = json(&r, &home, &["bookmark", "list", "--json"]); assert_eq!( bms[0]["heads"].as_array().unwrap().len(), 3, "independent: {bms}" ); let log = json(&r, &home, &["log", "--json"]); let hash = |m: &str| { let c = log["changes"] .as_array() .unwrap() .iter() .find(|c| c["message"] == m) .unwrap(); format!("#{}", &c["hash"].as_str().unwrap()[..12]) }; let b = hash("b"); let (code, _, err) = oxy(&r, &home, &["bookmark", "create", "x", &b]); assert_ne!(code, 0); assert!( err.contains("would lack 1 change") && err.contains("--upto") && err.contains("--only"), "{err}" ); assert_eq!( oxy(&r, &home, &["bookmark", "create", "upto", &b, "--upto"]).0, 0 ); assert_eq!( oxy(&r, &home, &["bookmark", "create", "only", &b, "--only"]).0, 0 ); let bms = json(&r, &home, &["bookmark", "list", "--json"]); let heads = |n: &str| { bms.as_array() .unwrap() .iter() .find(|x| x["name"] == n) .unwrap()["heads"] .as_array() .unwrap() .len() }; assert_eq!(heads("upto"), 2, "a and b: {bms}"); assert_eq!(heads("only"), 1); // Switching to the smaller state says what it leaves out. let (code, out, err) = oxy(&r, &home, &["bookmark", "switch", "only"]); assert_eq!(code, 0, "{err}"); let out = String::from_utf8_lossy(&out); assert!(out.contains("only lacks 2 changes that main has"), "{out}"); assert!(!r.join("a.txt").exists() && r.join("b.txt").exists()); // Moving a bookmark asks the same (metalgear, oxygen#42): main at b // alone would lose a, which it holds and was recorded before b. let (code, _, err) = oxy(&r, &home, &["bookmark", "set", "main", &b]); assert_ne!(code, 0, "b is a head main holds, not a move forward"); assert!( err.contains("would lose 1 change") && err.contains("--upto"), "{err}" ); assert_eq!( oxy(&r, &home, &["bookmark", "set", "main", &b, "--upto"]).0, 0 ); let bms = json(&r, &home, &["bookmark", "list", "--json"]); let main = bms .as_array() .unwrap() .iter() .find(|x| x["name"] == "main") .unwrap(); assert_eq!(main["heads"].as_array().unwrap().len(), 2, "a and b: {bms}"); // A fast-forward (oxygen#78): y holds a and b; b2 is built on b, its // newest head. Moving y to b2 keeps a, which b2 does not depend on. assert_eq!( oxy(&r, &home, &["bookmark", "switch", "main", "--discard"]).0, 0 ); assert_eq!(oxy(&r, &home, &["bookmark", "create", "y"]).0, 0); write(&r, "b.txt", "b2\n"); assert_eq!(oxy(&r, &home, &["commit", "-m", "b2"]).0, 0); let log = json(&r, &home, &["log", "--json"]); let b2 = log["changes"] .as_array() .unwrap() .iter() .find(|c| c["message"] == "b2") .unwrap()["hash"] .as_str() .unwrap()[..12] .to_string(); let (code, out, err) = oxy(&r, &home, &["bookmark", "set", "y", &format!("#{b2}")]); assert_eq!(code, 0, "{err}"); assert!( String::from_utf8_lossy(&out).contains("builds on y's newest head"), "says why" ); let bms = json(&r, &home, &["bookmark", "list", "--json"]); let y = bms .as_array() .unwrap() .iter() .find(|x| x["name"] == "y") .unwrap(); assert_eq!(y["heads"].as_array().unwrap().len(), 2, "a and b2: {bms}"); } /// oxygen#29: a pull on a feature bookmark said only "Already up to date" /// while main was behind. Since oxygen#48 it updates main too, and says so. #[test] fn pull_names_its_bookmark_and_the_others_it_moved() { 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 (a, b) = (tmp.path().join("a"), tmp.path().join("b")); for d in [&a, &b] { assert_eq!( oxy( tmp.path(), &home, &["clone", origin.to_str().unwrap(), d.to_str().unwrap()] ) .0, 0 ); } write(&a, "one.txt", "one\n"); assert_eq!(oxy(&a, &home, &["commit", "-m", "one"]).0, 0); assert_eq!(oxy(&a, &home, &["push"]).0, 0); assert_eq!(oxy(&b, &home, &["pull"]).0, 0); assert_eq!(oxy(&b, &home, &["bookmark", "create", "feat"]).0, 0); assert_eq!(oxy(&b, &home, &["bookmark", "switch", "feat"]).0, 0); assert_eq!(oxy(&b, &home, &["push", "-b", "feat"]).0, 0); write(&a, "two.txt", "two\n"); assert_eq!(oxy(&a, &home, &["commit", "-m", "two"]).0, 0); assert_eq!(oxy(&a, &home, &["push"]).0, 0); let (code, out, err) = oxy(&b, &home, &["pull"]); assert_eq!(code, 0, "{err}"); let out = String::from_utf8_lossy(&out); assert!(out.contains("feat is up to date with origin"), "{out}"); assert!(out.contains("Also updated main"), "{out}"); assert!(!out.contains("behind"), "{out}"); } /// oxygen#35: a clean status is answered from the manifest, loading no /// history; any change, a mode change included, falls back to a snapshot. #[test] fn a_clean_status_needs_no_history_and_misses_no_change() { use std::os::unix::fs::PermissionsExt; let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let r = tmp.path().join("repo"); assert_eq!(oxy(tmp.path(), &home, &["init", r.to_str().unwrap()]).0, 0); let old = std::time::SystemTime::now() - std::time::Duration::from_secs(30); for f in ["a.txt", "b.txt"] { write(&r, f, &format!("{f}\n")); std::fs::File::options() .write(true) .open(r.join(f)) .unwrap() .set_modified(old) .unwrap(); } assert_eq!(oxy(&r, &home, &["commit", "-m", "two"]).0, 0); let status = |r: &Path| { let out = Command::new(env!("CARGO_BIN_EXE_oxy")) .args(["--plain", "status"]) .current_dir(r) .env("OXY_HOME", &home) .env("NO_COLOR", "1") .env("OXY_TIMINGS", "1") .output() .unwrap(); ( String::from_utf8_lossy(&out.stdout).to_string(), String::from_utf8_lossy(&out.stderr).to_string(), ) }; let (out, err) = status(&r); assert!(out.contains("working copy clean"), "{out}"); assert!(!err.contains("snapshot"), "no snapshot was needed: {err}"); // A mode change alters neither size nor time. std::fs::set_permissions(r.join("a.txt"), std::fs::Permissions::from_mode(0o755)).unwrap(); let (out, err) = status(&r); assert!(out.contains("a.txt"), "{out}"); assert!(err.contains("snapshot"), "{err}"); std::fs::set_permissions(r.join("a.txt"), std::fs::Permissions::from_mode(0o644)).unwrap(); // A new file. write(&r, "c.txt", "c\n"); let (out, _) = status(&r); assert!(out.contains("c.txt"), "{out}"); } #[test] fn a_nested_git_repository_is_named_not_silently_skipped() { let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let r = tmp.path().join("repo"); std::fs::create_dir_all(&r).unwrap(); assert_eq!(oxy(&r, &home, &["init", "."]).0, 0); write(&r, "a.txt", "a\n"); write(&r, "rust-core/src/lib.rs", "pub fn f() {}\n"); write(&r, "rust-core/.git/HEAD", "ref: refs/heads/main\n"); let st = json(&r, &home, &["status", "--json"]); assert_eq!(st["untracked_nested"], serde_json::json!(["rust-core"])); let (code, out, err) = oxy(&r, &home, &["commit", "-m", "first"]); assert_eq!(code, 0, "{err}"); let out = String::from_utf8_lossy(&out); assert!( out.contains("rust-core/") && out.contains("nested Git repository"), "{out}" ); // The clean status says so too. let (_, out, _) = oxy(&r, &home, &["status"]); let out = String::from_utf8_lossy(&out); assert!( out.contains("working copy clean") && out.contains("rust-core/"), "{out}" ); } /// oxygen#50: the log is a graph. Two changes that build on the same one /// take two lanes that join above it; each change shows its ID, author, /// time and hash on one line and its subject on the next. #[test] fn the_log_draws_lanes_that_join() { let tmp = tempfile::tempdir().unwrap(); let home = tmp.path().join("home"); std::fs::create_dir_all(&home).unwrap(); let repo = tmp.path().join("r"); assert_eq!( oxy(tmp.path(), &home, &["init", repo.to_str().unwrap()]).0, 0 ); write(&repo, "a.txt", "a\n"); write(&repo, "b.txt", "b\n"); assert_eq!(oxy(&repo, &home, &["commit", "-m", "both files"]).0, 0); write(&repo, "a.txt", "a\na2\n"); assert_eq!(oxy(&repo, &home, &["commit", "-m", "edit a"]).0, 0); write(&repo, "b.txt", "b\nb2\n"); assert_eq!(oxy(&repo, &home, &["commit", "-m", "edit b"]).0, 0); let (code, out, err) = oxy(&repo, &home, &["log"]); assert_eq!(code, 0, "{err}"); let out = String::from_utf8_lossy(&out); let lines: Vec<&str> = out.lines().collect(); // The working copy, then "edit b", "edit a" beside it, then their base. assert!(lines[0].starts_with('@'), "{out}"); assert!( lines .iter() .any(|l| l.starts_with("|-'") && l.contains("edit a")), "{out}" ); assert!( lines .iter() .any(|l| l.starts_with("| *") || l.starts_with("| o")), "{out}" ); let base = lines.iter().position(|l| l.contains("both files")).unwrap(); assert!( lines[base - 1].contains('#'), "the header line carries the hash: {out}" ); }