Skip to content

Commit d986d09

Browse files
committed
add tests to verify common inputs would work for try_find_reference(…) (#251)
This shows that despite being possible, it's still relatively cumbersome when names have to be combined due to the need for intermediate storage.
1 parent 0d36641 commit d986d09

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed
Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod find {
2-
use std::convert::TryInto;
2+
use std::convert::{TryFrom, TryInto};
33

44
use git_ref as refs;
55
use git_repository::prelude::ReferenceAccessExt;
@@ -9,35 +9,72 @@ mod find {
99
crate::repo("make_references_repo.sh").map(Into::into)
1010
}
1111

12+
/// Gain an understanding how uses might want to call this function, and see what happens
1213
#[test]
13-
fn and_peel() {
14-
let repo = repo().unwrap();
15-
let mut packed_tag_ref = repo.try_find_reference("dt1").unwrap().expect("tag to exist");
16-
assert_eq!(packed_tag_ref.name(), "refs/tags/dt1".try_into().unwrap());
14+
fn possible_inputs() -> crate::Result {
15+
let repo = repo()?;
16+
repo.find_reference("dt1")?;
17+
repo.find_reference(&String::from("dt1"))?; // Owned Strings don't have an impl for PartialName
18+
19+
struct CustomType(String);
20+
impl<'a> TryFrom<&'a CustomType> for refs::PartialNameRef<'a> {
21+
type Error = refs::name::Error;
22+
23+
fn try_from(value: &'a CustomType) -> Result<Self, Self::Error> {
24+
refs::PartialNameRef::try_from(&value.0)
25+
}
26+
}
27+
repo.find_reference(&CustomType("dt1".into()))?;
28+
29+
struct CustomName {
30+
remote: &'static str,
31+
branch: &'static str,
32+
}
33+
34+
impl CustomName {
35+
fn to_partial_name(&self) -> String {
36+
format!("{}/{}", self.remote, self.branch)
37+
}
38+
}
39+
let name = CustomName {
40+
remote: "origin",
41+
branch: "main",
42+
};
43+
repo.find_reference(&name.to_partial_name())?;
44+
45+
Ok(())
46+
}
47+
48+
#[test]
49+
fn and_peel() -> crate::Result {
50+
let repo = repo()?;
51+
let mut packed_tag_ref = repo.try_find_reference("dt1")?.expect("tag to exist");
52+
assert_eq!(packed_tag_ref.name(), "refs/tags/dt1".try_into()?);
1753

1854
assert_eq!(
1955
packed_tag_ref.inner.target,
2056
refs::Target::Peeled(hex_to_id("4c3f4cce493d7beb45012e478021b5f65295e5a3")),
2157
"it points to a tag object"
2258
);
2359

24-
let object = packed_tag_ref.peel_to_id_in_place().unwrap();
60+
let object = packed_tag_ref.peel_to_id_in_place()?;
2561
let the_commit = hex_to_id("134385f6d781b7e97062102c6a483440bfda2a03");
2662
assert_eq!(object, the_commit, "it is assumed to be fully peeled");
2763
assert_eq!(
2864
object,
29-
packed_tag_ref.peel_to_id_in_place().unwrap(),
65+
packed_tag_ref.peel_to_id_in_place()?,
3066
"peeling again yields the same object"
3167
);
3268

33-
let mut symbolic_ref = repo.find_reference("multi-link-target1").unwrap();
34-
assert_eq!(symbolic_ref.name(), "refs/heads/multi-link-target1".try_into().unwrap());
35-
assert_eq!(symbolic_ref.peel_to_id_in_place().unwrap(), the_commit);
69+
let mut symbolic_ref = repo.find_reference("multi-link-target1")?;
70+
assert_eq!(symbolic_ref.name(), "refs/heads/multi-link-target1".try_into()?);
71+
assert_eq!(symbolic_ref.peel_to_id_in_place()?, the_commit);
3672
assert_eq!(
3773
symbolic_ref.name(),
38-
"refs/remotes/origin/multi-link-target3".try_into().unwrap(),
74+
"refs/remotes/origin/multi-link-target3".try_into()?,
3975
"it follows symbolic refs, too"
4076
);
41-
assert_eq!(symbolic_ref.into_fully_peeled_id().unwrap(), the_commit, "idempotency");
77+
assert_eq!(symbolic_ref.into_fully_peeled_id()?, the_commit, "idempotency");
78+
Ok(())
4279
}
4380
}

0 commit comments

Comments
 (0)