| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Helpers for the workspace tests: a repository in a temporary directory, |
| 5 | //! changesets made straight through the engine, and branches moved by hand. |
| 6 | |
| 7 | #![allow(dead_code)] |
| 8 | |
| 9 | use std::convert::Infallible; |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | |
| 12 | use braid_format::{Change, ChangeOp, Kind, ObjectId, Objects, Signature}; |
| 13 | use braid_repo::Repo; |
| 14 | use braid_store::RefUpdate; |
| 15 | use braid_wc::Identity; |
| 16 | |
| 17 | pub struct Fixture { |
| 18 | pub temp: tempfile::TempDir, |
| 19 | pub repo: Repo, |
| 20 | } |
| 21 | |
| 22 | impl Fixture { |
| 23 | pub fn new() -> Fixture { |
| 24 | let temp = tempfile::tempdir().unwrap(); |
| 25 | let repo = Repo::init(&temp.path().join("repo")).unwrap(); |
| 26 | Fixture { temp, repo } |
| 27 | } |
| 28 | |
| 29 | /// A path in the fixture's temporary directory. |
| 30 | pub fn path(&self, name: &str) -> PathBuf { |
| 31 | self.temp.path().join(name) |
| 32 | } |
| 33 | |
| 34 | pub fn file(&self, content: &str) -> ChangeOp { |
| 35 | ChangeOp::File { |
| 36 | blob: self |
| 37 | .repo |
| 38 | .store() |
| 39 | .put(Kind::Blob, content.as_bytes()) |
| 40 | .unwrap(), |
| 41 | size: content.len() as u64, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub fn exec(&self, content: &str) -> ChangeOp { |
| 46 | ChangeOp::Executable { |
| 47 | blob: self |
| 48 | .repo |
| 49 | .store() |
| 50 | .put(Kind::Blob, content.as_bytes()) |
| 51 | .unwrap(), |
| 52 | size: content.len() as u64, |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub fn symlink(&self, target: &[u8]) -> ChangeOp { |
| 57 | ChangeOp::Symlink { |
| 58 | blob: self.repo.store().put(Kind::Blob, target).unwrap(), |
| 59 | size: target.len() as u64, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// A changeset on `parents` with these changes. |
| 64 | pub fn commit(&self, parents: &[ObjectId], changes: Vec<(&str, ChangeOp)>) -> ObjectId { |
| 65 | let changes = changes |
| 66 | .into_iter() |
| 67 | .map(|(path, op)| Change { |
| 68 | path: path.as_bytes().to_vec(), |
| 69 | op, |
| 70 | }) |
| 71 | .collect(); |
| 72 | let id = braid_engine::commit( |
| 73 | self.repo.store(), |
| 74 | parents.to_vec(), |
| 75 | changes, |
| 76 | signature(), |
| 77 | signature(), |
| 78 | b"test change\n".to_vec(), |
| 79 | Vec::new(), |
| 80 | ) |
| 81 | .unwrap(); |
| 82 | self.repo.store().sync().unwrap(); |
| 83 | id |
| 84 | } |
| 85 | |
| 86 | /// Points `branch` at `id`. |
| 87 | pub fn set_branch(&self, branch: &str, id: ObjectId) { |
| 88 | self.repo |
| 89 | .store() |
| 90 | .transact("test", "move a branch", |refs| { |
| 91 | Ok::<_, Infallible>(vec![RefUpdate::new(branch, refs.get(branch), Some(id))]) |
| 92 | }) |
| 93 | .unwrap(); |
| 94 | } |
| 95 | |
| 96 | pub fn head(&self, branch: &str) -> Option<ObjectId> { |
| 97 | self.repo.store().refs().unwrap().get(branch) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | pub fn signature() -> Signature { |
| 102 | Signature { |
| 103 | name: b"Ada".to_vec(), |
| 104 | email: b"ada@example.org".to_vec(), |
| 105 | seconds: 1_700_000_000, |
| 106 | offset_minutes: 0, |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn ada() -> Identity { |
| 111 | Identity::new("Ada", "ada@example.org").unwrap() |
| 112 | } |
| 113 | |
| 114 | /// Every entry under `dir`, recursively, without following symlinks. |
| 115 | pub fn listing(dir: &Path) -> Vec<String> { |
| 116 | let mut found = Vec::new(); |
| 117 | let mut pending = vec![dir.to_path_buf()]; |
| 118 | while let Some(current) = pending.pop() { |
| 119 | for entry in std::fs::read_dir(¤t).unwrap() { |
| 120 | let entry = entry.unwrap(); |
| 121 | let path = entry.path(); |
| 122 | found.push( |
| 123 | path.strip_prefix(dir) |
| 124 | .unwrap() |
| 125 | .to_string_lossy() |
| 126 | .into_owned(), |
| 127 | ); |
| 128 | if entry.file_type().unwrap().is_dir() { |
| 129 | pending.push(path); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | found.sort(); |
| 134 | found |
| 135 | } |