11#include "clar_libgit2.h"
2- #include "git2/sys/odb_backend.h"
32#include "repository.h"
4-
5- typedef struct fake_backend {
6- git_odb_backend parent ;
7-
8- git_error_code error_code ;
9-
10- int exists_calls ;
11- int read_calls ;
12- int read_header_calls ;
13- int read_prefix_calls ;
14- } fake_backend ;
3+ #include "backend_helpers.h"
154
165static git_repository * _repo ;
176static fake_backend * _fake ;
18- static git_oid _oid ;
197
208#define HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
219#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
2210
23- static int fake_backend__exists (git_odb_backend * backend , const git_oid * oid )
24- {
25- fake_backend * fake ;
26-
27- GIT_UNUSED (oid );
28-
29- fake = (fake_backend * )backend ;
30-
31- fake -> exists_calls ++ ;
32-
33- return (fake -> error_code == GIT_OK );
34- }
35-
36- static int fake_backend__read (
37- void * * buffer_p , size_t * len_p , git_otype * type_p ,
38- git_odb_backend * backend , const git_oid * oid )
39- {
40- fake_backend * fake ;
41-
42- GIT_UNUSED (buffer_p );
43- GIT_UNUSED (len_p );
44- GIT_UNUSED (type_p );
45- GIT_UNUSED (oid );
46-
47- fake = (fake_backend * )backend ;
48-
49- fake -> read_calls ++ ;
50-
51- * len_p = 0 ;
52- * buffer_p = NULL ;
53- * type_p = GIT_OBJ_BLOB ;
54-
55- return fake -> error_code ;
56- }
57-
58- static int fake_backend__read_header (
59- size_t * len_p , git_otype * type_p ,
60- git_odb_backend * backend , const git_oid * oid )
61- {
62- fake_backend * fake ;
63-
64- GIT_UNUSED (len_p );
65- GIT_UNUSED (type_p );
66- GIT_UNUSED (oid );
67-
68- fake = (fake_backend * )backend ;
69-
70- fake -> read_header_calls ++ ;
71-
72- * len_p = 0 ;
73- * type_p = GIT_OBJ_BLOB ;
74-
75- return fake -> error_code ;
76- }
77-
78- static int fake_backend__read_prefix (
79- git_oid * out_oid , void * * buffer_p , size_t * len_p , git_otype * type_p ,
80- git_odb_backend * backend , const git_oid * short_oid , size_t len )
81- {
82- fake_backend * fake ;
83-
84- GIT_UNUSED (buffer_p );
85- GIT_UNUSED (len_p );
86- GIT_UNUSED (type_p );
87- GIT_UNUSED (short_oid );
88- GIT_UNUSED (len );
89-
90- fake = (fake_backend * )backend ;
91-
92- fake -> read_prefix_calls ++ ;
93-
94- git_oid_cpy (out_oid , & _oid );
95- * len_p = 0 ;
96- * buffer_p = NULL ;
97- * type_p = GIT_OBJ_BLOB ;
98-
99- return fake -> error_code ;
100- }
101-
102- static void fake_backend__free (git_odb_backend * _backend )
103- {
104- fake_backend * backend ;
105-
106- backend = (fake_backend * )_backend ;
107-
108- git__free (backend );
109- }
110-
111- static int build_fake_backend (
112- git_odb_backend * * out ,
113- git_error_code error_code )
114- {
115- fake_backend * backend ;
116-
117- backend = git__calloc (1 , sizeof (fake_backend ));
118- GITERR_CHECK_ALLOC (backend );
119-
120- backend -> parent .version = GIT_ODB_BACKEND_VERSION ;
121-
122- backend -> parent .refresh = NULL ;
123- backend -> error_code = error_code ;
124-
125- backend -> parent .read = fake_backend__read ;
126- backend -> parent .read_prefix = fake_backend__read_prefix ;
127- backend -> parent .read_header = fake_backend__read_header ;
128- backend -> parent .exists = fake_backend__exists ;
129- backend -> parent .free = & fake_backend__free ;
130-
131- * out = (git_odb_backend * )backend ;
132-
133- return 0 ;
134- }
135-
13611static void setup_repository_and_backend (git_error_code error_code , const char * hash )
13712{
13813 git_odb * odb = NULL ;
13914 git_odb_backend * backend = NULL ;
15+ git_oid oid ;
14016
14117 _repo = cl_git_sandbox_init ("testrepo.git" );
14218
143- cl_git_pass (build_fake_backend (& backend , error_code ));
19+ cl_git_pass (git_oid_fromstr (& oid , hash ));
20+ cl_git_pass (build_fake_backend (& backend , error_code , & oid ));
14421
14522 cl_git_pass (git_repository_odb__weakptr (& odb , _repo ));
14623 cl_git_pass (git_odb_add_backend (odb , backend , 10 ));
14724
14825 _fake = (fake_backend * )backend ;
149-
150- cl_git_pass (git_oid_fromstr (& _oid , hash ));
15126}
15227
15328void test_odb_backend_nonrefreshing__cleanup (void )
@@ -162,7 +37,7 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_failure(void)
16237 setup_repository_and_backend (GIT_ENOTFOUND , HASH );
16338
16439 cl_git_pass (git_repository_odb__weakptr (& odb , _repo ));
165- cl_assert_equal_b (false, git_odb_exists (odb , & _oid ));
40+ cl_assert_equal_b (false, git_odb_exists (odb , & _fake -> oid ));
16641
16742 cl_assert_equal_i (1 , _fake -> exists_calls );
16843}
@@ -174,7 +49,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_failure(void)
17449 setup_repository_and_backend (GIT_ENOTFOUND , HASH );
17550
17651 cl_git_fail_with (
177- git_object_lookup (& obj , _repo , & _oid , GIT_OBJ_ANY ),
52+ git_object_lookup (& obj , _repo , & _fake -> oid , GIT_OBJ_ANY ),
17853 GIT_ENOTFOUND );
17954
18055 cl_assert_equal_i (1 , _fake -> read_calls );
@@ -187,7 +62,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_failure(void)
18762 setup_repository_and_backend (GIT_ENOTFOUND , HASH );
18863
18964 cl_git_fail_with (
190- git_object_lookup_prefix (& obj , _repo , & _oid , 7 , GIT_OBJ_ANY ),
65+ git_object_lookup_prefix (& obj , _repo , & _fake -> oid , 7 , GIT_OBJ_ANY ),
19166 GIT_ENOTFOUND );
19267
19368 cl_assert_equal_i (1 , _fake -> read_prefix_calls );
@@ -204,7 +79,7 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_failure(void)
20479 cl_git_pass (git_repository_odb__weakptr (& odb , _repo ));
20580
20681 cl_git_fail_with (
207- git_odb_read_header (& len , & type , odb , & _oid ),
82+ git_odb_read_header (& len , & type , odb , & _fake -> oid ),
20883 GIT_ENOTFOUND );
20984
21085 cl_assert_equal_i (1 , _fake -> read_header_calls );
@@ -217,7 +92,7 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_success(void)
21792 setup_repository_and_backend (GIT_OK , HASH );
21893
21994 cl_git_pass (git_repository_odb__weakptr (& odb , _repo ));
220- cl_assert_equal_b (true, git_odb_exists (odb , & _oid ));
95+ cl_assert_equal_b (true, git_odb_exists (odb , & _fake -> oid ));
22196
22297 cl_assert_equal_i (1 , _fake -> exists_calls );
22398}
@@ -228,7 +103,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_success(void)
228103
229104 setup_repository_and_backend (GIT_OK , EMPTY_HASH );
230105
231- cl_git_pass (git_object_lookup (& obj , _repo , & _oid , GIT_OBJ_ANY ));
106+ cl_git_pass (git_object_lookup (& obj , _repo , & _fake -> oid , GIT_OBJ_ANY ));
232107
233108 cl_assert_equal_i (1 , _fake -> read_calls );
234109
@@ -241,7 +116,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_success(void)
241116
242117 setup_repository_and_backend (GIT_OK , EMPTY_HASH );
243118
244- cl_git_pass (git_object_lookup_prefix (& obj , _repo , & _oid , 7 , GIT_OBJ_ANY ));
119+ cl_git_pass (git_object_lookup_prefix (& obj , _repo , & _fake -> oid , 7 , GIT_OBJ_ANY ));
245120
246121 cl_assert_equal_i (1 , _fake -> read_prefix_calls );
247122
@@ -258,7 +133,7 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_success(void)
258133
259134 cl_git_pass (git_repository_odb__weakptr (& odb , _repo ));
260135
261- cl_git_pass (git_odb_read_header (& len , & type , odb , & _oid ));
136+ cl_git_pass (git_odb_read_header (& len , & type , odb , & _fake -> oid ));
262137
263138 cl_assert_equal_i (1 , _fake -> read_header_calls );
264139}
0 commit comments