| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! End-to-end tests of the `braid` binary. |
| 5 | |
| 6 | use std::io::Write; |
| 7 | use std::process::{Command, Stdio}; |
| 8 | |
| 9 | use braid_format::{Kind, ObjectId}; |
| 10 | |
| 11 | fn braid() -> Command { |
| 12 | Command::new(env!("CARGO_BIN_EXE_braid")) |
| 13 | } |
| 14 | |
| 15 | #[test] |
| 16 | fn version_prints_name_and_version() { |
| 17 | let out = braid().arg("--version").output().unwrap(); |
| 18 | assert!(out.status.success()); |
| 19 | let text = String::from_utf8(out.stdout).unwrap(); |
| 20 | assert_eq!(text.trim(), format!("braid {}", env!("CARGO_PKG_VERSION"))); |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn hash_object_matches_the_library() { |
| 25 | let dir = tempfile::tempdir().unwrap(); |
| 26 | let path = dir.path().join("hello.txt"); |
| 27 | std::fs::write(&path, b"hello\n").unwrap(); |
| 28 | |
| 29 | let out = braid().arg("hash-object").arg(&path).output().unwrap(); |
| 30 | assert!(out.status.success()); |
| 31 | let text = String::from_utf8(out.stdout).unwrap(); |
| 32 | let expected = ObjectId::hash(Kind::Blob, b"hello\n"); |
| 33 | assert_eq!(text, format!("{expected} {}\n", path.display())); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn hash_object_json() { |
| 38 | let dir = tempfile::tempdir().unwrap(); |
| 39 | let path = dir.path().join("big.bin"); |
| 40 | let payload: Vec<u8> = (0..1_000_000u32).map(|i| (i % 253) as u8).collect(); |
| 41 | std::fs::write(&path, &payload).unwrap(); |
| 42 | |
| 43 | let out = braid() |
| 44 | .args(["hash-object", "--json"]) |
| 45 | .arg(&path) |
| 46 | .output() |
| 47 | .unwrap(); |
| 48 | assert!(out.status.success()); |
| 49 | let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap(); |
| 50 | let item = &value[0]; |
| 51 | assert_eq!(item["kind"], "blob"); |
| 52 | assert_eq!(item["size"], payload.len() as u64); |
| 53 | assert_eq!( |
| 54 | item["id"], |
| 55 | ObjectId::hash(Kind::Blob, &payload).to_hex().as_str() |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn hash_object_reads_standard_input() { |
| 61 | let mut child = braid() |
| 62 | .args(["hash-object", "-"]) |
| 63 | .stdin(Stdio::piped()) |
| 64 | .stdout(Stdio::piped()) |
| 65 | .spawn() |
| 66 | .unwrap(); |
| 67 | child.stdin.take().unwrap().write_all(b"hello\n").unwrap(); |
| 68 | let out = child.wait_with_output().unwrap(); |
| 69 | assert!(out.status.success()); |
| 70 | let text = String::from_utf8(out.stdout).unwrap(); |
| 71 | let expected = ObjectId::hash(Kind::Blob, b"hello\n"); |
| 72 | assert_eq!(text, format!("{expected} -\n")); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn missing_file_is_an_error() { |
| 77 | let dir = tempfile::tempdir().unwrap(); |
| 78 | let out = braid() |
| 79 | .arg("hash-object") |
| 80 | .arg(dir.path().join("does-not-exist")) |
| 81 | .output() |
| 82 | .unwrap(); |
| 83 | assert!(!out.status.success()); |
| 84 | let err = String::from_utf8(out.stderr).unwrap(); |
| 85 | assert!(err.starts_with("braid: error: cannot open"), "{err}"); |
| 86 | } |
| 87 | |
| 88 | fn braid_in(dir: &std::path::Path) -> Command { |
| 89 | let mut command = braid(); |
| 90 | command.arg("--repo").arg(dir).env("BRAID_AGENT", "tester"); |
| 91 | command |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn large_files_are_stored_in_chunks_and_read_back_whole() { |
| 96 | let dir = tempfile::tempdir().unwrap(); |
| 97 | assert!( |
| 98 | braid() |
| 99 | .arg("init") |
| 100 | .arg(dir.path()) |
| 101 | .status() |
| 102 | .unwrap() |
| 103 | .success() |
| 104 | ); |
| 105 | let mut state = 1u64; |
| 106 | let payload: Vec<u8> = (0..(5 << 20) + 77) |
| 107 | .map(|_| { |
| 108 | state = state |
| 109 | .wrapping_mul(6_364_136_223_846_793_005) |
| 110 | .wrapping_add(1); |
| 111 | (state >> 33) as u8 |
| 112 | }) |
| 113 | .collect(); |
| 114 | let path = dir.path().join("model.bin"); |
| 115 | std::fs::write(&path, &payload).unwrap(); |
| 116 | let id = ObjectId::hash(Kind::Blob, &payload); |
| 117 | |
| 118 | let stored = run_json(braid_in(dir.path()).args(["hash-object", "-w"]).arg(&path)); |
| 119 | assert_eq!(stored[0]["id"], id.to_hex().as_str()); |
| 120 | assert_eq!(stored[0]["size"], payload.len() as u64); |
| 121 | // Standard input too, with a different file. |
| 122 | let mut child = braid_in(dir.path()) |
| 123 | .args(["hash-object", "-w", "-"]) |
| 124 | .stdin(Stdio::piped()) |
| 125 | .stdout(Stdio::piped()) |
| 126 | .spawn() |
| 127 | .unwrap(); |
| 128 | let shorter = &payload[..(4 << 20) + 5]; |
| 129 | child.stdin.take().unwrap().write_all(shorter).unwrap(); |
| 130 | let out = child.wait_with_output().unwrap(); |
| 131 | assert!(out.status.success()); |
| 132 | let from_stdin = ObjectId::hash(Kind::Blob, shorter); |
| 133 | assert_eq!( |
| 134 | String::from_utf8(out.stdout).unwrap(), |
| 135 | format!("{from_stdin} -\n") |
| 136 | ); |
| 137 | |
| 138 | let out = braid_in(dir.path()) |
| 139 | .args(["cat-object", &id.to_hex()]) |
| 140 | .output() |
| 141 | .unwrap(); |
| 142 | assert!(out.status.success()); |
| 143 | assert!(out.stdout == payload); |
| 144 | let described = run_json(braid_in(dir.path()).args(["cat-object", &id.to_hex()])); |
| 145 | assert_eq!(described["size"], payload.len() as u64); |
| 146 | |
| 147 | let fsck = run_json(braid_in(dir.path()).arg("fsck")); |
| 148 | assert_eq!(fsck["clean"], true); |
| 149 | assert_eq!(fsck["chunked_records"], 2); |
| 150 | assert_eq!(fsck["delta_records"], 0); |
| 151 | let stats = run_json(braid_in(dir.path()).arg("stats")); |
| 152 | assert_eq!(stats["codecs"]["chunked"]["objects"], 2); |
| 153 | assert_eq!( |
| 154 | stats["codecs"]["chunked"]["raw_bytes"], |
| 155 | (payload.len() + shorter.len()) as u64 |
| 156 | ); |
| 157 | assert_eq!(stats["kinds"]["blob"]["objects"], stats["objects"]); |
| 158 | // The two files share their first chunks: distinct content is a |
| 159 | // little over the longer one. |
| 160 | let raw = stats["raw_bytes"].as_u64().unwrap(); |
| 161 | assert!(raw >= payload.len() as u64 && raw < (payload.len() + (1 << 20)) as u64); |
| 162 | let text = braid_in(dir.path()).arg("stats").output().unwrap(); |
| 163 | assert!(text.status.success()); |
| 164 | assert!(String::from_utf8(text.stdout).unwrap().contains("chunked")); |
| 165 | } |
| 166 | |
| 167 | fn run_json(command: &mut Command) -> serde_json::Value { |
| 168 | let out = command.arg("--json").output().unwrap(); |
| 169 | assert!( |
| 170 | out.status.success(), |
| 171 | "{}", |
| 172 | String::from_utf8_lossy(&out.stderr) |
| 173 | ); |
| 174 | serde_json::from_slice(&out.stdout).unwrap() |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn repository_round_trip() { |
| 179 | let dir = tempfile::tempdir().unwrap(); |
| 180 | let init = braid().arg("init").arg(dir.path()).output().unwrap(); |
| 181 | assert!(init.status.success()); |
| 182 | // A second init in the same place is refused. |
| 183 | assert!( |
| 184 | !braid() |
| 185 | .arg("init") |
| 186 | .arg(dir.path()) |
| 187 | .output() |
| 188 | .unwrap() |
| 189 | .status |
| 190 | .success() |
| 191 | ); |
| 192 | |
| 193 | let file = dir.path().join("note.txt"); |
| 194 | std::fs::write(&file, b"hello\n").unwrap(); |
| 195 | let written = run_json(braid_in(dir.path()).args(["hash-object", "-w"]).arg(&file)); |
| 196 | let id = written[0]["id"].as_str().unwrap().to_owned(); |
| 197 | assert_eq!(id, ObjectId::hash(Kind::Blob, b"hello\n").to_hex()); |
| 198 | |
| 199 | let cat = braid_in(dir.path()) |
| 200 | .args(["cat-object", &id]) |
| 201 | .output() |
| 202 | .unwrap(); |
| 203 | assert_eq!(cat.stdout, b"hello\n"); |
| 204 | |
| 205 | // Refs: create with a compare-and-swap, refuse a stale expectation. |
| 206 | let set = run_json(braid_in(dir.path()).args(["ref", "set", "main", &id, "--expect", "none"])); |
| 207 | assert_eq!(set["agent"], "tester"); |
| 208 | assert_eq!(set["updates"][0]["new"], id.as_str()); |
| 209 | let stale = braid_in(dir.path()) |
| 210 | .args(["ref", "set", "main", &id, "--expect", "none"]) |
| 211 | .output() |
| 212 | .unwrap(); |
| 213 | assert!(!stale.status.success()); |
| 214 | |
| 215 | let refs = run_json(braid_in(dir.path()).arg("refs")); |
| 216 | assert_eq!(refs["refs"]["main"], id.as_str()); |
| 217 | let by_name = braid_in(dir.path()) |
| 218 | .args(["cat-object", "main"]) |
| 219 | .output() |
| 220 | .unwrap(); |
| 221 | assert_eq!(by_name.stdout, b"hello\n"); |
| 222 | |
| 223 | // The op log shows it, undo reverts it, and the store stays clean. |
| 224 | let log = run_json(braid_in(dir.path()).args(["op", "log"])); |
| 225 | assert_eq!(log.as_array().unwrap().len(), 1); |
| 226 | let undone = run_json(braid_in(dir.path()).arg("undo")); |
| 227 | assert_eq!(undone["undoes"], 1); |
| 228 | let refs = run_json(braid_in(dir.path()).arg("refs")); |
| 229 | assert!(refs["refs"].as_object().unwrap().is_empty()); |
| 230 | let nothing = braid_in(dir.path()).arg("undo").output().unwrap(); |
| 231 | assert!(!nothing.status.success()); |
| 232 | |
| 233 | let fsck = run_json(braid_in(dir.path()).arg("fsck")); |
| 234 | assert_eq!(fsck["clean"], true); |
| 235 | assert_eq!(fsck["objects"], 1); |
| 236 | } |
| 237 | |
| 238 | /// Two blob contents whose ids start with the same six hex digits or more. |
| 239 | fn blob_twins() -> (Vec<u8>, Vec<u8>) { |
| 240 | let mut seen = std::collections::HashMap::new(); |
| 241 | for n in 0u64.. { |
| 242 | let content = format!("twin {n}\n").into_bytes(); |
| 243 | let id = ObjectId::hash(Kind::Blob, &content); |
| 244 | let start = id.to_hex()[..6].to_owned(); |
| 245 | if let Some(other) = seen.insert(start, content.clone()) { |
| 246 | return (other, content); |
| 247 | } |
| 248 | } |
| 249 | unreachable!("the search ends") |
| 250 | } |
| 251 | |
| 252 | #[test] |
| 253 | fn short_ids_and_an_ambiguous_one() { |
| 254 | let dir = tempfile::tempdir().unwrap(); |
| 255 | assert!( |
| 256 | braid() |
| 257 | .arg("init") |
| 258 | .arg(dir.path()) |
| 259 | .output() |
| 260 | .unwrap() |
| 261 | .status |
| 262 | .success() |
| 263 | ); |
| 264 | let (one, two) = blob_twins(); |
| 265 | let files = [dir.path().join("one"), dir.path().join("two")]; |
| 266 | std::fs::write(&files[0], &one).unwrap(); |
| 267 | std::fs::write(&files[1], &two).unwrap(); |
| 268 | let written = run_json( |
| 269 | braid_in(dir.path()) |
| 270 | .args(["hash-object", "-w"]) |
| 271 | .args(&files), |
| 272 | ); |
| 273 | let ids: Vec<String> = (0..2) |
| 274 | .map(|i| written[i]["id"].as_str().unwrap().to_owned()) |
| 275 | .collect(); |
| 276 | let shared = ids[0] |
| 277 | .bytes() |
| 278 | .zip(ids[1].bytes()) |
| 279 | .take_while(|(a, b)| a == b) |
| 280 | .count(); |
| 281 | assert!(shared >= 6); |
| 282 | |
| 283 | // Six digits name both: an error that lists them, and nothing printed. |
| 284 | let out = braid_in(dir.path()) |
| 285 | .args(["cat-object", &ids[0][..6]]) |
| 286 | .output() |
| 287 | .unwrap(); |
| 288 | assert_eq!(out.status.code(), Some(1)); |
| 289 | assert!(out.stdout.is_empty()); |
| 290 | let err = String::from_utf8(out.stderr).unwrap(); |
| 291 | assert!(err.contains("ambiguous"), "{err}"); |
| 292 | for id in &ids { |
| 293 | assert!(err.contains(&id[..12]), "{err}"); |
| 294 | } |
| 295 | |
| 296 | // One digit more names one of them, wherever a revision is taken. |
| 297 | let unique: Vec<&str> = ids.iter().map(|id| &id[..shared + 1]).collect(); |
| 298 | let cat = braid_in(dir.path()) |
| 299 | .args(["cat-object", unique[1]]) |
| 300 | .output() |
| 301 | .unwrap(); |
| 302 | assert_eq!(cat.stdout, two); |
| 303 | let cat = run_json(braid_in(dir.path()).args(["cat-object", unique[0]])); |
| 304 | assert_eq!(cat["id"], ids[0].as_str()); |
| 305 | let set = run_json(braid_in(dir.path()).args(["ref", "set", "pinned", unique[0]])); |
| 306 | assert_eq!(set["updates"][0]["new"], ids[0].as_str()); |
| 307 | // A ref spelled like a short id wins over it. |
| 308 | run_json(braid_in(dir.path()).args(["ref", "set", &ids[1][..6], unique[1]])); |
| 309 | let cat = braid_in(dir.path()) |
| 310 | .args(["cat-object", &ids[1][..6]]) |
| 311 | .output() |
| 312 | .unwrap(); |
| 313 | assert_eq!(cat.stdout, two); |
| 314 | // A short id is no changeset: show says what it is. |
| 315 | let out = braid_in(dir.path()) |
| 316 | .args(["show", unique[0]]) |
| 317 | .output() |
| 318 | .unwrap(); |
| 319 | let err = String::from_utf8(out.stderr).unwrap(); |
| 320 | assert!(err.contains("is a blob, not a changeset"), "{err}"); |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn signatures_carry_the_local_offset() { |
| 325 | let dir = tempfile::tempdir().unwrap(); |
| 326 | assert!( |
| 327 | braid() |
| 328 | .arg("init") |
| 329 | .arg(dir.path()) |
| 330 | .output() |
| 331 | .unwrap() |
| 332 | .status |
| 333 | .success() |
| 334 | ); |
| 335 | let file = dir.path().join("a.txt"); |
| 336 | std::fs::write(&file, b"a\n").unwrap(); |
| 337 | let set = format!("a.txt=@{}", file.display()); |
| 338 | // POSIX rules, UTC, and a zone that cannot be found, which reads as UTC. |
| 339 | for (zone, minutes, shown) in [ |
| 340 | ("<+0530>-05:30", 330, "+0530"), |
| 341 | ("<-03>3", -180, "-0300"), |
| 342 | ("UTC0", 0, "+0000"), |
| 343 | ("Nowhere/Nothing", 0, "+0000"), |
| 344 | ] { |
| 345 | let made = run_json( |
| 346 | braid_in(dir.path()) |
| 347 | .env("TZ", zone) |
| 348 | .env("BRAID_AUTHOR", "Ada <ada@example.org>") |
| 349 | .args(["change", "create", "--base", "none", "--set", &set]) |
| 350 | .args(["-m", zone]), |
| 351 | ); |
| 352 | let id = made["change"].as_str().unwrap(); |
| 353 | // JSON keeps the seconds and the offset. |
| 354 | let show = run_json(braid_in(dir.path()).args(["show", id])); |
| 355 | for who in ["author", "committer"] { |
| 356 | assert_eq!(show[who]["offset_minutes"], minutes, "{zone}"); |
| 357 | assert!(show[who]["seconds"].as_i64().unwrap() > 1_700_000_000); |
| 358 | let time = show[who]["time"].as_str().unwrap(); |
| 359 | assert!(time.ends_with(shown), "{zone}: {time}"); |
| 360 | } |
| 361 | // The log shows minutes, in the signature's own offset. |
| 362 | let log = braid_in(dir.path()) |
| 363 | .args(["log", id, "-n", "1"]) |
| 364 | .output() |
| 365 | .unwrap(); |
| 366 | let text = String::from_utf8(log.stdout).unwrap(); |
| 367 | let time = text.split(" ").nth(1).unwrap(); |
| 368 | assert_eq!(time.len(), "2026-10-15 21:41 +0530".len(), "{text}"); |
| 369 | assert!(time.ends_with(shown), "{zone}: {text}"); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | #[test] |
| 374 | fn commands_outside_a_repository_fail_clearly() { |
| 375 | let dir = tempfile::tempdir().unwrap(); |
| 376 | let out = braid() |
| 377 | .arg("--repo") |
| 378 | .arg(dir.path()) |
| 379 | .arg("refs") |
| 380 | .output() |
| 381 | .unwrap(); |
| 382 | assert!(!out.status.success()); |
| 383 | let err = String::from_utf8(out.stderr).unwrap(); |
| 384 | assert!(err.contains("not inside a braid repository"), "{err}"); |
| 385 | } |
| 386 | |
| 387 | /// Runs git with a fixed identity and dates, ignoring the user's config. |
| 388 | fn git(dir: &std::path::Path, args: &[&str]) -> String { |
| 389 | let out = Command::new("git") |
| 390 | .current_dir(dir) |
| 391 | .args(args) |
| 392 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 393 | .env("GIT_CONFIG_NOSYSTEM", "1") |
| 394 | .env("GIT_AUTHOR_NAME", "Ada") |
| 395 | .env("GIT_AUTHOR_EMAIL", "ada@example.org") |
| 396 | .env("GIT_COMMITTER_NAME", "Ada") |
| 397 | .env("GIT_COMMITTER_EMAIL", "ada@example.org") |
| 398 | .env("GIT_AUTHOR_DATE", "1700000000 +0100") |
| 399 | .env("GIT_COMMITTER_DATE", "1700000000 +0100") |
| 400 | .output() |
| 401 | .unwrap(); |
| 402 | assert!( |
| 403 | out.status.success(), |
| 404 | "git {args:?}: {}", |
| 405 | String::from_utf8_lossy(&out.stderr) |
| 406 | ); |
| 407 | String::from_utf8(out.stdout).unwrap() |
| 408 | } |
| 409 | |
| 410 | #[test] |
| 411 | fn git_import_log_show_export() { |
| 412 | let work = tempfile::tempdir().unwrap(); |
| 413 | let src = work.path().join("src"); |
| 414 | std::fs::create_dir(&src).unwrap(); |
| 415 | git(&src, &["init", "-q", "-b", "main"]); |
| 416 | std::fs::write(src.join("README.md"), "hello\n").unwrap(); |
| 417 | git(&src, &["add", "README.md"]); |
| 418 | git(&src, &["commit", "-q", "-m", "First"]); |
| 419 | std::fs::create_dir(src.join("src")).unwrap(); |
| 420 | std::fs::write(src.join("src/lib.rs"), "pub fn f() {}\n").unwrap(); |
| 421 | git(&src, &["add", "src/lib.rs"]); |
| 422 | git(&src, &["commit", "-q", "-m", "Second\n\nWith a body."]); |
| 423 | git(&src, &["tag", "-a", "v1", "-m", "Version 1"]); |
| 424 | |
| 425 | let repo = work.path().join("braid"); |
| 426 | assert!( |
| 427 | braid() |
| 428 | .arg("init") |
| 429 | .arg(&repo) |
| 430 | .output() |
| 431 | .unwrap() |
| 432 | .status |
| 433 | .success() |
| 434 | ); |
| 435 | let import = run_json(braid_in(&repo).args(["git", "import"]).arg(&src)); |
| 436 | assert_eq!(import["commits"], 2); |
| 437 | assert_eq!(import["tags"], 1); |
| 438 | // Importing again finds nothing new and moves no ref. |
| 439 | let again = run_json(braid_in(&repo).args(["git", "import"]).arg(&src)); |
| 440 | assert_eq!(again["commits"], 0); |
| 441 | assert!(again["operation"].is_null()); |
| 442 | |
| 443 | let log = run_json(braid_in(&repo).args(["log", "main"])); |
| 444 | let log = log.as_array().unwrap(); |
| 445 | assert_eq!(log.len(), 2); |
| 446 | assert_eq!(log[0]["message"], "Second\n\nWith a body.\n"); |
| 447 | assert_eq!(log[0]["author"]["email"], "ada@example.org"); |
| 448 | assert_eq!(log[0]["author"]["offset_minutes"], 60); |
| 449 | |
| 450 | let show = run_json(braid_in(&repo).args(["show", "main"])); |
| 451 | assert_eq!(show["changes"][0]["path"], "src/lib.rs"); |
| 452 | assert_eq!(show["changes"][0]["op"], "file"); |
| 453 | assert!(show["git_origin"].is_string()); |
| 454 | |
| 455 | // Export into an empty repository: the same commit and tag ids. |
| 456 | let mirror = work.path().join("mirror.git"); |
| 457 | std::fs::create_dir(&mirror).unwrap(); |
| 458 | git(&mirror, &["init", "-q", "--bare"]); |
| 459 | run_json(braid_in(&repo).args(["git", "export"]).arg(&mirror)); |
| 460 | for name in ["refs/heads/main", "refs/tags/v1"] { |
| 461 | assert_eq!( |
| 462 | git(&src, &["rev-parse", name]), |
| 463 | git(&mirror, &["rev-parse", name]), |
| 464 | "{name}" |
| 465 | ); |
| 466 | } |
| 467 | |
| 468 | // The import is one operation: undo takes every ref back. |
| 469 | run_json(braid_in(&repo).arg("undo")); |
| 470 | let refs = run_json(braid_in(&repo).arg("refs")); |
| 471 | assert!(refs["refs"].as_object().unwrap().is_empty()); |
| 472 | assert_eq!(run_json(braid_in(&repo).arg("fsck"))["clean"], true); |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn importing_again_never_takes_a_braid_branch_backwards() { |
| 477 | let work = tempfile::tempdir().unwrap(); |
| 478 | let src = work.path().join("src"); |
| 479 | std::fs::create_dir(&src).unwrap(); |
| 480 | git(&src, &["init", "-q", "-b", "main"]); |
| 481 | std::fs::write(src.join("a.txt"), "a\n").unwrap(); |
| 482 | git(&src, &["add", "a.txt"]); |
| 483 | git(&src, &["commit", "-q", "-m", "A"]); |
| 484 | git(&src, &["branch", "feature"]); |
| 485 | |
| 486 | let repo = work.path().join("braid"); |
| 487 | assert!( |
| 488 | braid() |
| 489 | .arg("init") |
| 490 | .arg(&repo) |
| 491 | .output() |
| 492 | .unwrap() |
| 493 | .status |
| 494 | .success() |
| 495 | ); |
| 496 | run_json(braid_in(&repo).args(["git", "import"]).arg(&src)); |
| 497 | let imported = run_json(braid_in(&repo).arg("refs"))["refs"]["main"] |
| 498 | .as_str() |
| 499 | .unwrap() |
| 500 | .to_owned(); |
| 501 | |
| 502 | // A change lands on main in braid, without a working copy. |
| 503 | let file = work.path().join("b.txt"); |
| 504 | std::fs::write(&file, "b\n").unwrap(); |
| 505 | let set = format!("b.txt=@{}", file.display()); |
| 506 | run_json( |
| 507 | braid_in(&repo) |
| 508 | .env("BRAID_AUTHOR", "Ada <ada@example.org>") |
| 509 | .args([ |
| 510 | "change", "create", "--base", "main", "--set", &set, "-m", "B", "--land", "main", |
| 511 | ]), |
| 512 | ); |
| 513 | let ahead = run_json(braid_in(&repo).arg("refs"))["refs"]["main"] |
| 514 | .as_str() |
| 515 | .unwrap() |
| 516 | .to_owned(); |
| 517 | assert_ne!(ahead, imported); |
| 518 | |
| 519 | // Importing again with nothing new in git keeps braid's main where it is. |
| 520 | let again = run_json(braid_in(&repo).args(["git", "import"]).arg(&src)); |
| 521 | assert_eq!(again["kept"][0]["name"], "main"); |
| 522 | assert_eq!(again["kept"][0]["reason"], "ahead"); |
| 523 | assert_eq!( |
| 524 | run_json(braid_in(&repo).arg("refs"))["refs"]["main"], |
| 525 | ahead.as_str() |
| 526 | ); |
| 527 | |
| 528 | // Git moves on too: main diverges and stays in braid, feature only moved |
| 529 | // forward and follows git. |
| 530 | std::fs::write(src.join("c.txt"), "c\n").unwrap(); |
| 531 | git(&src, &["add", "c.txt"]); |
| 532 | git(&src, &["commit", "-q", "-m", "C"]); |
| 533 | git(&src, &["checkout", "-q", "feature"]); |
| 534 | std::fs::write(src.join("d.txt"), "d\n").unwrap(); |
| 535 | git(&src, &["add", "d.txt"]); |
| 536 | git(&src, &["commit", "-q", "-m", "D"]); |
| 537 | let third = run_json(braid_in(&repo).args(["git", "import"]).arg(&src)); |
| 538 | let kept = third["kept"].as_array().unwrap(); |
| 539 | assert_eq!(kept.len(), 1, "{third}"); |
| 540 | assert_eq!(kept[0]["name"], "main"); |
| 541 | assert_eq!(kept[0]["reason"], "diverged"); |
| 542 | let refs = run_json(braid_in(&repo).arg("refs")); |
| 543 | assert_eq!(refs["refs"]["main"], ahead.as_str()); |
| 544 | assert_ne!(refs["refs"]["feature"], imported.as_str()); |
| 545 | assert_eq!(run_json(braid_in(&repo).arg("fsck"))["clean"], true); |
| 546 | } |
| 547 | |
| 548 | /// A reader that stops early, as `braid log | head` does, is no failure: |
| 549 | /// braid exits quietly with 141, as a shell reports a process that SIGPIPE |
| 550 | /// ended, in text and in JSON alike. |
| 551 | #[cfg(unix)] |
| 552 | #[test] |
| 553 | fn a_reader_that_stops_early_gets_a_quiet_141() { |
| 554 | use std::io::Read; |
| 555 | let dir = tempfile::tempdir().unwrap(); |
| 556 | let source = dir.path().join("source"); |
| 557 | let git = |args: &[&str], cwd: &std::path::Path| { |
| 558 | let output = Command::new("git") |
| 559 | .args(args) |
| 560 | .current_dir(cwd) |
| 561 | .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 562 | .env("GIT_CONFIG_NOSYSTEM", "1") |
| 563 | .output() |
| 564 | .unwrap(); |
| 565 | assert!(output.status.success(), "{output:?}"); |
| 566 | }; |
| 567 | git(&["init", "--quiet", "-b", "main", "source"], dir.path()); |
| 568 | // A history long enough that its log fills any pipe buffer. |
| 569 | let mut stream = String::new(); |
| 570 | for i in 1..=800 { |
| 571 | let message = format!("Commit number {i}, with a message long enough to fill the pipe\n"); |
| 572 | stream.push_str(&format!( |
| 573 | "commit refs/heads/main\nmark :{i}\ncommitter A <a@example.org> {} +0000\ndata {}\n{message}", |
| 574 | 1_700_000_000 + i, |
| 575 | message.len() |
| 576 | )); |
| 577 | if i > 1 { |
| 578 | stream.push_str(&format!("from :{}\n", i - 1)); |
| 579 | } |
| 580 | stream.push('\n'); |
| 581 | } |
| 582 | let mut import = Command::new("git") |
| 583 | .args(["fast-import", "--quiet"]) |
| 584 | .current_dir(&source) |
| 585 | .stdin(Stdio::piped()) |
| 586 | .spawn() |
| 587 | .unwrap(); |
| 588 | import |
| 589 | .stdin |
| 590 | .take() |
| 591 | .unwrap() |
| 592 | .write_all(stream.as_bytes()) |
| 593 | .unwrap(); |
| 594 | assert!(import.wait().unwrap().success()); |
| 595 | let repo = dir.path().join("repo"); |
| 596 | assert!(braid().arg("init").arg(&repo).status().unwrap().success()); |
| 597 | assert!( |
| 598 | braid() |
| 599 | .args(["git", "import"]) |
| 600 | .arg(&source) |
| 601 | .arg("--repo") |
| 602 | .arg(&repo) |
| 603 | .output() |
| 604 | .unwrap() |
| 605 | .status |
| 606 | .success() |
| 607 | ); |
| 608 | for json in [false, true] { |
| 609 | let mut command = braid(); |
| 610 | command |
| 611 | .args(["log", "main", "-n", "100000", "--repo"]) |
| 612 | .arg(&repo); |
| 613 | if json { |
| 614 | command.arg("--json"); |
| 615 | } |
| 616 | let mut child = command |
| 617 | .stdout(Stdio::piped()) |
| 618 | .stderr(Stdio::piped()) |
| 619 | .spawn() |
| 620 | .unwrap(); |
| 621 | let mut start = [0u8; 10]; |
| 622 | child.stdout.take().unwrap().read_exact(&mut start).unwrap(); |
| 623 | // The reader goes away here, as head does. |
| 624 | let output = child.wait_with_output().unwrap(); |
| 625 | assert_eq!(output.status.code(), Some(141), "json {json}: {output:?}"); |
| 626 | assert!(output.stderr.is_empty(), "json {json}: {output:?}"); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | #[test] |
| 631 | fn durability_full_opens_and_writes() { |
| 632 | let dir = tempfile::tempdir().unwrap(); |
| 633 | let repo = dir.path().join("repo"); |
| 634 | let init = braid() |
| 635 | .args(["--durability", "full", "init"]) |
| 636 | .arg(&repo) |
| 637 | .output() |
| 638 | .unwrap(); |
| 639 | assert!(init.status.success(), "{init:?}"); |
| 640 | let file = dir.path().join("a.txt"); |
| 641 | std::fs::write(&file, "durable\n").unwrap(); |
| 642 | let created = braid() |
| 643 | .env("BRAID_DURABILITY", "full") |
| 644 | .env("BRAID_AUTHOR", "Ada <ada@example.org>") |
| 645 | .args([ |
| 646 | "change", "create", "--base", "none", "-m", "First", "--land", "main", |
| 647 | ]) |
| 648 | .arg("--set") |
| 649 | .arg(format!("a.txt=@{}", file.display())) |
| 650 | .arg("--repo") |
| 651 | .arg(&repo) |
| 652 | .output() |
| 653 | .unwrap(); |
| 654 | assert!(created.status.success(), "{created:?}"); |
| 655 | let refused = braid() |
| 656 | .args(["--durability", "sometimes", "refs", "--repo"]) |
| 657 | .arg(&repo) |
| 658 | .output() |
| 659 | .unwrap(); |
| 660 | assert_eq!(refused.status.code(), Some(2)); |
| 661 | } |