Elffuss BraidHub

utopia-ia/elffuss-braid public

0cba57aa99aa · 2.6 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! End-to-end tests of the `braid` binary.
5
6use std::io::Write;
7use std::process::{Command, Stdio};
8
9use braid_format::{Kind, ObjectId};
10
11fn braid() -> Command {
12 Command::new(env!("CARGO_BIN_EXE_braid"))
13}
14
15#[test]
16fn 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]
24fn 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]
37fn 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]
60fn 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]
76fn 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}