Skip to content

Commit

Permalink
first test for simple file modification detection (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 19, 2022
1 parent 5be96b3 commit a9056fd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 38 deletions.
36 changes: 36 additions & 0 deletions git-repository/tests/object/commit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::cmp::Ordering;

use git_testtools::hex_to_id;

use crate::basic_repo;

#[test]
fn short_id() -> crate::Result {
let repo = basic_repo()?;
let commit = repo.head_commit()?;
assert_eq!(commit.short_id()?.cmp_oid(&commit.id), Ordering::Equal);
Ok(())
}

#[test]
fn tree() -> crate::Result {
let repo = basic_repo()?;
let commit = repo.head_commit()?;

assert_eq!(commit.tree()?.id, commit.tree_id().expect("id present"));
assert_eq!(
commit.tree_id().ok().map(|id| id.detach()),
Some(hex_to_id("21d3ba9a26b790a4858d67754ae05d04dfce4d0c"))
);
Ok(())
}

#[test]
fn decode() -> crate::Result {
let repo = basic_repo()?;
let commit = repo.head_commit()?;
assert_eq!(commit.decode()?.message, commit.message_raw()?);
assert_eq!(commit.decode()?.message(), commit.message()?);
assert_eq!(commit.decode()?.message, "c2\n");
Ok(())
}
40 changes: 2 additions & 38 deletions git-repository/tests/object/mod.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
mod commit {
use std::cmp::Ordering;

use git_testtools::hex_to_id;

use crate::basic_repo;

#[test]
fn short_id() -> crate::Result {
let repo = basic_repo()?;
let commit = repo.head_commit()?;
assert_eq!(commit.short_id()?.cmp_oid(&commit.id), Ordering::Equal);
Ok(())
}

#[test]
fn tree() -> crate::Result {
let repo = basic_repo()?;
let commit = repo.head_commit()?;

assert_eq!(commit.tree()?.id, commit.tree_id().expect("id present"));
assert_eq!(
commit.tree_id().ok().map(|id| id.detach()),
Some(hex_to_id("21d3ba9a26b790a4858d67754ae05d04dfce4d0c"))
);
Ok(())
}

#[test]
fn decode() -> crate::Result {
let repo = basic_repo()?;
let commit = repo.head_commit()?;
assert_eq!(commit.decode()?.message, commit.message_raw()?);
assert_eq!(commit.decode()?.message(), commit.message()?);
assert_eq!(commit.decode()?.message, "c2\n");
Ok(())
}
}
mod commit;
mod tree;

#[test]
fn object_ref_size_in_memory() {
Expand Down
44 changes: 44 additions & 0 deletions git-repository/tests/object/tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
mod diff {
use crate::remote;
use git_object::bstr::ByteSlice;
use git_object::tree::EntryMode;
use git_repository as git;
use git_repository::object::tree::diff::Event;
use std::convert::Infallible;

#[test]
fn changes_against_tree_modified() {
let repo = remote::repo("base");
let from = tree_named(&repo, "g");
let to = tree_named(&repo, "h");
from.changes()
.for_each_to_obtain_tree(&to, |event| -> Result<_, Infallible> {
match event {
Event::Modification {
previous_entry_mode,
previous_id,
entry_mode,
id,
} => {
assert_eq!(previous_entry_mode, EntryMode::Blob);
assert_eq!(entry_mode, EntryMode::Blob);
assert_eq!(previous_id.object().unwrap().data.as_bstr(), "g\n");
assert_eq!(id.object().unwrap().data.as_bstr(), "h\n");
Ok(git::diff::tree::visit::Action::Continue)
}
Event::Deletion { .. } | Event::Addition { .. } => unreachable!("only modification is expected"),
}
})
.unwrap();
}

fn tree_named<'repo>(repo: &'repo git::Repository, rev_spec: &str) -> git::Tree<'repo> {
repo.rev_parse_single(rev_spec)
.unwrap()
.object()
.unwrap()
.peel_to_kind(git::object::Kind::Tree)
.unwrap()
.into_tree()
}
}

0 comments on commit a9056fd

Please sign in to comment.