Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Test

on:
pull_request:
paths:
- src/*.rs
- Cargo.toml
- jakefile.toml

env:
CARGO_TERM_COLOR: always

jobs:
test:
name: Test on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Jake (also sets up Rust)
uses: AstraBert/setup-jake@v0.1.0

- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-test-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-

- name: Run tests
run: jake test
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ clap = { version = "4.6.1", features = ["derive"] }
ignore = "0.4.25"
serde_json = "1.0.149"
tokio = { version = "1.52.1", features = ["full"] }

[dev-dependencies]
serial_test = "3.4.0"
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ S3-backed, git-versioned object storage for agents.
## Installation

```bash
cargo install aggit@0.1.0-alpha
cargo install aggit@0.2.0-alpha
```


> [!NOTE]
>
> `aggit` is not yet compatible with Windows.

### As an Agent Skill

You can use `aggit` as an agent skill, downloading it with the `skills` CLI tool:
Expand Down
2 changes: 1 addition & 1 deletion jakefile.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build = "cargo build"
test = "echo 'No task yet for test'"
test = "cargo test"
clippy-fix = "cargo clippy --fix --bin aggit --allow-dirty"
clippy = "cargo clippy"
format = "cargo fmt"
Expand Down
199 changes: 199 additions & 0 deletions src/gitops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,3 +1127,202 @@ pub fn checkout_commit(commit_hash: String) -> anyhow::Result<()> {
restore_working_tree(commit_hash)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;

fn setup_test_repo() {
let _ = fs::remove_dir_all(".aggit");
init(PathBuf::from(".")).unwrap();
}

fn cleanup_test_repo() {
let _ = fs::remove_dir_all(".aggit");
}

#[test]
#[serial_test::serial]
fn test_init_creates_directories() {
cleanup_test_repo();
let repo = PathBuf::from(".aggit_test_repo");
let _ = fs::remove_dir_all(&repo);
init(repo.clone()).unwrap();
assert!(repo.join(".aggit").exists());
assert!(repo.join(".aggit/objects").exists());
assert!(repo.join(".aggit/refs/heads").exists());
assert!(repo.join(".aggit/refs/index").exists());
let head = fs::read_to_string(repo.join(".aggit/HEAD")).unwrap();
assert_eq!(head, "ref: refs/heads/main");
let _ = fs::remove_dir_all(&repo);
}

#[test]
#[serial_test::serial]
fn test_hash_object_without_write() {
cleanup_test_repo();
setup_test_repo();
let mut data = b"hello world".to_vec();
let hash = hash_object(&mut data, ObjectType::Blob, false).unwrap();
assert_eq!(hash.len(), 40);
let path = PathBuf::from(".aggit/objects")
.join(&hash[..2])
.join(&hash[2..]);
assert!(!path.exists());
cleanup_test_repo();
}

#[test]
#[serial_test::serial]
fn test_hash_object_with_write() {
cleanup_test_repo();
setup_test_repo();
let mut data = b"hello world".to_vec();
let hash = hash_object(&mut data, ObjectType::Blob, true).unwrap();
let path = PathBuf::from(".aggit/objects")
.join(&hash[..2])
.join(&hash[2..]);
assert!(path.exists());
cleanup_test_repo();
}

#[test]
#[serial_test::serial]
fn test_find_object_success() {
cleanup_test_repo();
setup_test_repo();
let mut data = b"find me".to_vec();
let hash = hash_object(&mut data, ObjectType::Blob, true).unwrap();
let found = find_object(&hash).unwrap();
assert!(found.to_string_lossy().contains(&hash[..2]));
cleanup_test_repo();
}

#[test]
#[serial_test::serial]
fn test_find_object_not_found() {
cleanup_test_repo();
setup_test_repo();
let result = find_object("0000000000000000000000000000000000000000");
assert!(result.is_err());
cleanup_test_repo();
}

#[test]
fn test_read_object_roundtrip() {
let data = b"roundtrip data".to_vec();
let hash = hash_object(&mut data.clone(), ObjectType::Blob, true).unwrap();
let (obj_type, read_data) = read_object(&hash).unwrap();
assert_eq!(obj_type, ObjectType::Blob);
assert_eq!(read_data, b"roundtrip data");
}

#[test]
#[serial_test::serial]
fn test_get_current_branch() {
cleanup_test_repo();
setup_test_repo();
let branch = get_current_branch().unwrap();
assert_eq!(branch, "main");
cleanup_test_repo();
}

#[test]
fn test_index_path_for_branch() {
let path = index_path_for_branch("main");
assert_eq!(path, PathBuf::from(".aggit/refs/index/main"));
}

#[test]
fn test_head_path_for_branch() {
let path = head_path_for_branch("main");
assert_eq!(path, PathBuf::from(".aggit/refs/heads/main"));
}

#[test]
fn test_object_type_from_str() {
assert_eq!(ObjectType::from_str("blob").unwrap(), ObjectType::Blob);
assert_eq!(ObjectType::from_str("commit").unwrap(), ObjectType::Commit);
assert_eq!(ObjectType::from_str("tree").unwrap(), ObjectType::Tree);
assert!(ObjectType::from_str("unknown").is_err());
}

#[test]
fn test_cat_file_mode_from_str() {
assert!(CatFileMode::from_str("blob").is_ok());
assert!(CatFileMode::from_str("size").is_ok());
assert!(CatFileMode::from_str("type").is_ok());
assert!(CatFileMode::from_str("pretty").is_ok());
assert!(CatFileMode::from_str("invalid").is_err());
}

#[test]
#[serial_test::serial]
fn test_write_and_read_index() {
cleanup_test_repo();
setup_test_repo();
let entries = vec![IndexEntry {
ctime_s: 0,
ctime_n: 0,
mtime_s: 0,
mtime_n: 0,
dev: 0,
ino: 0,
mode: 0o100644,
uid: 0,
gid: 0,
size: 5,
sha1: [0u8; 20],
flags: 4,
path: "test.txt".to_string(),
}];
write_index(&entries).unwrap();
let read = read_index().unwrap();
assert_eq!(read.len(), 1);
assert_eq!(read[0].path, "test.txt");
cleanup_test_repo();
}

#[test]
fn test_read_tree_with_data() {
let mut tree_data = Vec::new();
tree_data.extend_from_slice(b"100644 file.txt\x00");
tree_data.extend_from_slice(&[0u8; 20]);
let entries = read_tree(None, Some(tree_data)).unwrap();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].1, "file.txt");
}

#[test]
#[serial_test::serial]
fn test_get_local_current_hash_no_commits() {
cleanup_test_repo();
setup_test_repo();
let (hash, branch) = get_local_current_hash().unwrap();
assert!(hash.is_none());
assert_eq!(branch, "main");
cleanup_test_repo();
}

#[test]
#[serial_test::serial]
fn test_list_branches_no_error() {
cleanup_test_repo();
setup_test_repo();
list_branches().unwrap();
cleanup_test_repo();
}

#[test]
#[serial_test::serial]
fn test_collect_reachable_objects_blob_only() {
cleanup_test_repo();
setup_test_repo();
let mut data = b"blob content".to_vec();
let hash = hash_object(&mut data, ObjectType::Blob, true).unwrap();
let objects = collect_reachable_objects(&hash, None).unwrap();
assert!(objects.contains(&hash));
cleanup_test_repo();
}
}
Loading
Loading