Skip to content

Commit 6e010bb

Browse files
committed
tests: odb: allow passing fake objects to the fake backend
Right now, the fake backend is quite restrained in the way how it works: we pass it an OID which it is to return later as well as an error code we want it to return. While this is sufficient for existing tests, we can make the fake backend a little bit more generic in order to allow us testing for additional scenarios. To do so, we change the backend to not accept an error code and OID which it is to return for queries, but instead a simple array of OIDs with their respective blob contents. On each query, the fake backend simply iterates through this array and returns the first matching object.
1 parent 369cb45 commit 6e010bb

File tree

3 files changed

+78
-76
lines changed

3 files changed

+78
-76
lines changed

tests/odb/backend/backend_helpers.c

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,99 @@
22
#include "git2/sys/odb_backend.h"
33
#include "backend_helpers.h"
44

5+
static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
6+
{
7+
const fake_object *obj = fake->objects;
8+
9+
while (obj && obj->oid) {
10+
git_oid current_oid;
11+
12+
git_oid_fromstr(&current_oid, obj->oid);
13+
14+
if (git_oid_ncmp(&current_oid, oid, len) == 0) {
15+
if (out)
16+
*out = obj;
17+
return 0;
18+
}
19+
20+
obj++;
21+
}
22+
23+
return GIT_ENOTFOUND;
24+
}
25+
526
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
627
{
728
fake_backend *fake;
829

9-
GIT_UNUSED(oid);
10-
1130
fake = (fake_backend *)backend;
1231

1332
fake->exists_calls++;
1433

15-
return (fake->error_code == GIT_OK);
34+
return search_object(NULL, fake, oid, GIT_OID_RAWSZ) == GIT_OK;
1635
}
1736

1837
static int fake_backend__read(
1938
void **buffer_p, size_t *len_p, git_otype *type_p,
2039
git_odb_backend *backend, const git_oid *oid)
2140
{
41+
const fake_object *obj;
2242
fake_backend *fake;
2343

24-
GIT_UNUSED(buffer_p);
25-
GIT_UNUSED(len_p);
26-
GIT_UNUSED(type_p);
27-
GIT_UNUSED(oid);
28-
2944
fake = (fake_backend *)backend;
3045

3146
fake->read_calls++;
3247

33-
*len_p = 0;
34-
*buffer_p = NULL;
35-
*type_p = GIT_OBJ_BLOB;
48+
if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
49+
*len_p = strlen(obj->content);
50+
*buffer_p = git__strdup(obj->content);
51+
*type_p = GIT_OBJ_BLOB;
52+
return 0;
53+
}
3654

37-
return fake->error_code;
55+
return GIT_ENOTFOUND;
3856
}
3957

4058
static int fake_backend__read_header(
4159
size_t *len_p, git_otype *type_p,
4260
git_odb_backend *backend, const git_oid *oid)
4361
{
62+
const fake_object *obj;
4463
fake_backend *fake;
4564

46-
GIT_UNUSED(len_p);
47-
GIT_UNUSED(type_p);
48-
GIT_UNUSED(oid);
49-
5065
fake = (fake_backend *)backend;
5166

5267
fake->read_header_calls++;
5368

54-
*len_p = 0;
55-
*type_p = GIT_OBJ_BLOB;
69+
if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
70+
*len_p = strlen(obj->content);
71+
*type_p = GIT_OBJ_BLOB;
72+
return 0;
73+
}
5674

57-
return fake->error_code;
75+
return GIT_ENOTFOUND;
5876
}
5977

6078
static int fake_backend__read_prefix(
6179
git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p,
6280
git_odb_backend *backend, const git_oid *short_oid, size_t len)
6381
{
82+
const fake_object *obj;
6483
fake_backend *fake;
6584

66-
GIT_UNUSED(buffer_p);
67-
GIT_UNUSED(len_p);
68-
GIT_UNUSED(type_p);
69-
GIT_UNUSED(short_oid);
70-
GIT_UNUSED(len);
71-
7285
fake = (fake_backend *)backend;
7386

7487
fake->read_prefix_calls++;
7588

76-
git_oid_cpy(out_oid, &fake->oid);
77-
*len_p = 0;
78-
*buffer_p = NULL;
79-
*type_p = GIT_OBJ_BLOB;
89+
if (search_object(&obj, fake, short_oid, len) == 0) {
90+
git_oid_fromstr(out_oid, obj->oid);
91+
*len_p = strlen(obj->content);
92+
*buffer_p = git__strdup(obj->content);
93+
*type_p = GIT_OBJ_BLOB;
94+
return 0;
95+
}
8096

81-
return fake->error_code;
97+
return GIT_ENOTFOUND;
8298
}
8399

84100
static void fake_backend__free(git_odb_backend *_backend)
@@ -92,8 +108,7 @@ static void fake_backend__free(git_odb_backend *_backend)
92108

93109
int build_fake_backend(
94110
git_odb_backend **out,
95-
git_error_code error_code,
96-
const git_oid *oid)
111+
const fake_object *objects)
97112
{
98113
fake_backend *backend;
99114

@@ -103,16 +118,14 @@ int build_fake_backend(
103118
backend->parent.version = GIT_ODB_BACKEND_VERSION;
104119

105120
backend->parent.refresh = NULL;
106-
backend->error_code = error_code;
121+
backend->objects = objects;
107122

108123
backend->parent.read = fake_backend__read;
109124
backend->parent.read_prefix = fake_backend__read_prefix;
110125
backend->parent.read_header = fake_backend__read_header;
111126
backend->parent.exists = fake_backend__exists;
112127
backend->parent.free = &fake_backend__free;
113128

114-
git_oid_cpy(&backend->oid, oid);
115-
116129
*out = (git_odb_backend *)backend;
117130

118131
return 0;
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
#include "git2/sys/odb_backend.h"
22

33
typedef struct {
4-
git_odb_backend parent;
4+
const char *oid;
5+
const char *content;
6+
} fake_object;
57

6-
git_error_code error_code;
7-
git_oid oid;
8+
typedef struct {
9+
git_odb_backend parent;
810

911
int exists_calls;
1012
int read_calls;
1113
int read_header_calls;
1214
int read_prefix_calls;
15+
16+
const fake_object *objects;
1317
} fake_backend;
1418

1519
int build_fake_backend(
1620
git_odb_backend **out,
17-
git_error_code error_code,
18-
const git_oid *oid);
21+
const fake_object *objects);

tests/odb/backend/nonrefreshing.c

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
static git_repository *_repo;
66
static fake_backend *_fake;
77

8-
#define HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
9-
#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
8+
#define NONEXISTING_HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
9+
#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
1010

11-
static git_oid _oid;
12-
static git_oid _empty_oid;
11+
static const fake_object _objects[] = {
12+
{ EXISTING_HASH, "" },
13+
{ NULL, NULL }
14+
};
1315

14-
static void setup_repository_and_backend(git_error_code error_code, const char *hash)
16+
static git_oid _nonexisting_oid;
17+
static git_oid _existing_oid;
18+
19+
static void setup_repository_and_backend(void)
1520
{
1621
git_odb *odb = NULL;
1722
git_odb_backend *backend = NULL;
18-
git_oid oid;
1923

2024
_repo = cl_git_sandbox_init("testrepo.git");
2125

22-
cl_git_pass(git_oid_fromstr(&oid, hash));
23-
cl_git_pass(build_fake_backend(&backend, error_code, &oid));
26+
cl_git_pass(build_fake_backend(&backend, _objects));
2427

2528
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
2629
cl_git_pass(git_odb_add_backend(odb, backend, 10));
@@ -30,8 +33,9 @@ static void setup_repository_and_backend(git_error_code error_code, const char *
3033

3134
void test_odb_backend_nonrefreshing__initialize(void)
3235
{
33-
git_oid_fromstr(&_oid, HASH);
34-
git_oid_fromstr(&_empty_oid, EMPTY_HASH);
36+
git_oid_fromstr(&_nonexisting_oid, NONEXISTING_HASH);
37+
git_oid_fromstr(&_existing_oid, EXISTING_HASH);
38+
setup_repository_and_backend();
3539
}
3640

3741
void test_odb_backend_nonrefreshing__cleanup(void)
@@ -43,10 +47,8 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_failure(void)
4347
{
4448
git_odb *odb;
4549

46-
setup_repository_and_backend(GIT_ENOTFOUND, HASH);
47-
4850
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
49-
cl_assert_equal_b(false, git_odb_exists(odb, &_oid));
51+
cl_assert_equal_b(false, git_odb_exists(odb, &_nonexisting_oid));
5052

5153
cl_assert_equal_i(1, _fake->exists_calls);
5254
}
@@ -55,10 +57,8 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_failure(void)
5557
{
5658
git_object *obj;
5759

58-
setup_repository_and_backend(GIT_ENOTFOUND, HASH);
59-
6060
cl_git_fail_with(
61-
git_object_lookup(&obj, _repo, &_oid, GIT_OBJ_ANY),
61+
git_object_lookup(&obj, _repo, &_nonexisting_oid, GIT_OBJ_ANY),
6262
GIT_ENOTFOUND);
6363

6464
cl_assert_equal_i(1, _fake->read_calls);
@@ -68,10 +68,8 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_failure(void)
6868
{
6969
git_object *obj;
7070

71-
setup_repository_and_backend(GIT_ENOTFOUND, HASH);
72-
7371
cl_git_fail_with(
74-
git_object_lookup_prefix(&obj, _repo, &_oid, 7, GIT_OBJ_ANY),
72+
git_object_lookup_prefix(&obj, _repo, &_nonexisting_oid, 7, GIT_OBJ_ANY),
7573
GIT_ENOTFOUND);
7674

7775
cl_assert_equal_i(1, _fake->read_prefix_calls);
@@ -83,12 +81,10 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_failure(void)
8381
size_t len;
8482
git_otype type;
8583

86-
setup_repository_and_backend(GIT_ENOTFOUND, HASH);
87-
8884
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
8985

9086
cl_git_fail_with(
91-
git_odb_read_header(&len, &type, odb, &_oid),
87+
git_odb_read_header(&len, &type, odb, &_nonexisting_oid),
9288
GIT_ENOTFOUND);
9389

9490
cl_assert_equal_i(1, _fake->read_header_calls);
@@ -98,10 +94,8 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_success(void)
9894
{
9995
git_odb *odb;
10096

101-
setup_repository_and_backend(GIT_OK, HASH);
102-
10397
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
104-
cl_assert_equal_b(true, git_odb_exists(odb, &_oid));
98+
cl_assert_equal_b(true, git_odb_exists(odb, &_existing_oid));
10599

106100
cl_assert_equal_i(1, _fake->exists_calls);
107101
}
@@ -110,9 +104,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_success(void)
110104
{
111105
git_object *obj;
112106

113-
setup_repository_and_backend(GIT_OK, EMPTY_HASH);
114-
115-
cl_git_pass(git_object_lookup(&obj, _repo, &_empty_oid, GIT_OBJ_ANY));
107+
cl_git_pass(git_object_lookup(&obj, _repo, &_existing_oid, GIT_OBJ_ANY));
116108

117109
cl_assert_equal_i(1, _fake->read_calls);
118110

@@ -123,9 +115,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_success(void)
123115
{
124116
git_object *obj;
125117

126-
setup_repository_and_backend(GIT_OK, EMPTY_HASH);
127-
128-
cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_empty_oid, 7, GIT_OBJ_ANY));
118+
cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_existing_oid, 7, GIT_OBJ_ANY));
129119

130120
cl_assert_equal_i(1, _fake->read_prefix_calls);
131121

@@ -138,11 +128,9 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_success(void)
138128
size_t len;
139129
git_otype type;
140130

141-
setup_repository_and_backend(GIT_OK, HASH);
142-
143131
cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
144132

145-
cl_git_pass(git_odb_read_header(&len, &type, odb, &_oid));
133+
cl_git_pass(git_odb_read_header(&len, &type, odb, &_existing_oid));
146134

147135
cl_assert_equal_i(1, _fake->read_header_calls);
148136
}
@@ -151,8 +139,6 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_when_revparsing_a_full
151139
{
152140
git_object *obj;
153141

154-
setup_repository_and_backend(GIT_ENOTFOUND, HASH);
155-
156142
cl_git_fail_with(
157143
git_revparse_single(&obj, _repo, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
158144
GIT_ENOTFOUND);

0 commit comments

Comments
 (0)