summaryrefslogtreecommitdiff
path: root/tests/libgit2/odb/backend/refreshing.c
blob: 9e49298a888d817aec1faa35dee0d0207484331e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "clar_libgit2.h"
#include "repository.h"
#include "backend_helpers.h"

static git_repository *_repo;
static fake_backend *_fake;

#define NONEXISTING_HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"

static const fake_object _objects[] = {
	{ EXISTING_HASH, "" },
	{ NULL, NULL }
};

static git_oid _nonexisting_oid;
static git_oid _existing_oid;

static void setup_repository_and_backend(void)
{
	git_odb *odb = NULL;
	git_odb_backend *backend = NULL;

	_repo = cl_git_sandbox_init("testrepo.git");

	cl_git_pass(build_fake_backend(&backend, _objects, true));

	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
	cl_git_pass(git_odb_add_backend(odb, backend, 10));

	_fake = (fake_backend *)backend;
}

void test_odb_backend_refreshing__initialize(void)
{
	git_oid_fromstr(&_nonexisting_oid, NONEXISTING_HASH);
	git_oid_fromstr(&_existing_oid, EXISTING_HASH);
	setup_repository_and_backend();
}

void test_odb_backend_refreshing__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

void test_odb_backend_refreshing__exists_is_invoked_twice_on_failure(void)
{
	git_odb *odb;

	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
	cl_assert_equal_b(false, git_odb_exists(odb, &_nonexisting_oid));

	cl_assert_equal_i(2, _fake->exists_calls);
	cl_assert_equal_i(1, _fake->refresh_calls);
}

void test_odb_backend_refreshing__read_is_invoked_twice_on_failure(void)
{
	git_object *obj;

	cl_git_fail_with(
		git_object_lookup(&obj, _repo, &_nonexisting_oid, GIT_OBJECT_ANY),
		GIT_ENOTFOUND);

	cl_assert_equal_i(2, _fake->read_calls);
	cl_assert_equal_i(1, _fake->refresh_calls);
}

void test_odb_backend_refreshing__readprefix_is_invoked_twice_on_failure(void)
{
	git_object *obj;

	cl_git_fail_with(
		git_object_lookup_prefix(&obj, _repo, &_nonexisting_oid, 7, GIT_OBJECT_ANY),
		GIT_ENOTFOUND);

	cl_assert_equal_i(2, _fake->read_prefix_calls);
	cl_assert_equal_i(1, _fake->refresh_calls);
}

void test_odb_backend_refreshing__readheader_is_invoked_twice_on_failure(void)
{
	git_odb *odb;
	size_t len;
	git_object_t type;

	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));

	cl_git_fail_with(
		git_odb_read_header(&len, &type, odb, &_nonexisting_oid),
		GIT_ENOTFOUND);

	cl_assert_equal_i(2, _fake->read_header_calls);
	cl_assert_equal_i(1, _fake->refresh_calls);
}

void test_odb_backend_refreshing__exists_is_invoked_once_on_success(void)
{
	git_odb *odb;

	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
	cl_assert_equal_b(true, git_odb_exists(odb, &_existing_oid));

	cl_assert_equal_i(1, _fake->exists_calls);
	cl_assert_equal_i(0, _fake->refresh_calls);
}

void test_odb_backend_refreshing__read_is_invoked_once_on_success(void)
{
	git_object *obj;

	cl_git_pass(git_object_lookup(&obj, _repo, &_existing_oid, GIT_OBJECT_ANY));

	cl_assert_equal_i(1, _fake->read_calls);
	cl_assert_equal_i(0, _fake->refresh_calls);

	git_object_free(obj);
}

void test_odb_backend_refreshing__readprefix_is_invoked_once_on_success(void)
{
	git_object *obj;

	cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_existing_oid, 7, GIT_OBJECT_ANY));

	cl_assert_equal_i(1, _fake->read_prefix_calls);
	cl_assert_equal_i(0, _fake->refresh_calls);

	git_object_free(obj);
}

void test_odb_backend_refreshing__readheader_is_invoked_once_on_success(void)
{
	git_odb *odb;
	size_t len;
	git_object_t type;

	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));

	cl_git_pass(git_odb_read_header(&len, &type, odb, &_existing_oid));

	cl_assert_equal_i(1, _fake->read_header_calls);
	cl_assert_equal_i(0, _fake->refresh_calls);
}

void test_odb_backend_refreshing__read_is_invoked_twice_when_revparsing_a_full_oid(void)
{
	git_object *obj;

	cl_git_fail_with(
		git_revparse_single(&obj, _repo, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
		GIT_ENOTFOUND);

	cl_assert_equal_i(2, _fake->read_calls);
	cl_assert_equal_i(1, _fake->refresh_calls);
}

void test_odb_backend_refreshing__refresh_is_invoked(void)
{
	git_odb *odb;

	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
	cl_assert_equal_i(0, git_odb_refresh(odb));

	cl_assert_equal_i(1, _fake->refresh_calls);
}

void test_odb_backend_refreshing__refresh_suppressed_with_no_refresh(void)
{
	git_odb *odb;

	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
	cl_assert_equal_b(false, git_odb_exists_ext(odb, &_nonexisting_oid, GIT_ODB_LOOKUP_NO_REFRESH));

	cl_assert_equal_i(0, _fake->refresh_calls);
}