|
| 1 | +#include "clar_libgit2.h" |
| 2 | +#include "repository.h" |
| 3 | +#include "backend_helpers.h" |
| 4 | + |
| 5 | +static git_repository *_repo; |
| 6 | +static fake_backend *_fake; |
| 7 | + |
| 8 | +#define NONEXISTING_HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" |
| 9 | +#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" |
| 10 | + |
| 11 | +static const fake_object _objects[] = { |
| 12 | + { EXISTING_HASH, "" }, |
| 13 | + { NULL, NULL } |
| 14 | +}; |
| 15 | + |
| 16 | +static git_oid _nonexisting_oid; |
| 17 | +static git_oid _existing_oid; |
| 18 | + |
| 19 | +static void setup_repository_and_backend(void) |
| 20 | +{ |
| 21 | + git_odb *odb = NULL; |
| 22 | + git_odb_backend *backend = NULL; |
| 23 | + |
| 24 | + _repo = cl_git_sandbox_init("testrepo.git"); |
| 25 | + |
| 26 | + cl_git_pass(build_fake_backend(&backend, _objects, true)); |
| 27 | + |
| 28 | + cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); |
| 29 | + cl_git_pass(git_odb_add_backend(odb, backend, 10)); |
| 30 | + |
| 31 | + _fake = (fake_backend *)backend; |
| 32 | +} |
| 33 | + |
| 34 | +void test_odb_backend_refreshing__initialize(void) |
| 35 | +{ |
| 36 | + git_oid_fromstr(&_nonexisting_oid, NONEXISTING_HASH); |
| 37 | + git_oid_fromstr(&_existing_oid, EXISTING_HASH); |
| 38 | + setup_repository_and_backend(); |
| 39 | +} |
| 40 | + |
| 41 | +void test_odb_backend_refreshing__cleanup(void) |
| 42 | +{ |
| 43 | + cl_git_sandbox_cleanup(); |
| 44 | +} |
| 45 | + |
| 46 | +void test_odb_backend_refreshing__exists_is_invoked_twice_on_failure(void) |
| 47 | +{ |
| 48 | + git_odb *odb; |
| 49 | + |
| 50 | + cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); |
| 51 | + cl_assert_equal_b(false, git_odb_exists(odb, &_nonexisting_oid)); |
| 52 | + |
| 53 | + cl_assert_equal_i(2, _fake->exists_calls); |
| 54 | + cl_assert_equal_i(1, _fake->refresh_calls); |
| 55 | +} |
| 56 | + |
| 57 | +void test_odb_backend_refreshing__read_is_invoked_twice_on_failure(void) |
| 58 | +{ |
| 59 | + git_object *obj; |
| 60 | + |
| 61 | + cl_git_fail_with( |
| 62 | + git_object_lookup(&obj, _repo, &_nonexisting_oid, GIT_OBJECT_ANY), |
| 63 | + GIT_ENOTFOUND); |
| 64 | + |
| 65 | + cl_assert_equal_i(2, _fake->read_calls); |
| 66 | + cl_assert_equal_i(1, _fake->refresh_calls); |
| 67 | +} |
| 68 | + |
| 69 | +void test_odb_backend_refreshing__readprefix_is_invoked_twice_on_failure(void) |
| 70 | +{ |
| 71 | + git_object *obj; |
| 72 | + |
| 73 | + cl_git_fail_with( |
| 74 | + git_object_lookup_prefix(&obj, _repo, &_nonexisting_oid, 7, GIT_OBJECT_ANY), |
| 75 | + GIT_ENOTFOUND); |
| 76 | + |
| 77 | + cl_assert_equal_i(2, _fake->read_prefix_calls); |
| 78 | + cl_assert_equal_i(1, _fake->refresh_calls); |
| 79 | +} |
| 80 | + |
| 81 | +void test_odb_backend_refreshing__readheader_is_invoked_twice_on_failure(void) |
| 82 | +{ |
| 83 | + git_odb *odb; |
| 84 | + size_t len; |
| 85 | + git_object_t type; |
| 86 | + |
| 87 | + cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); |
| 88 | + |
| 89 | + cl_git_fail_with( |
| 90 | + git_odb_read_header(&len, &type, odb, &_nonexisting_oid), |
| 91 | + GIT_ENOTFOUND); |
| 92 | + |
| 93 | + cl_assert_equal_i(2, _fake->read_header_calls); |
| 94 | + cl_assert_equal_i(1, _fake->refresh_calls); |
| 95 | +} |
| 96 | + |
| 97 | +void test_odb_backend_refreshing__exists_is_invoked_once_on_success(void) |
| 98 | +{ |
| 99 | + git_odb *odb; |
| 100 | + |
| 101 | + cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); |
| 102 | + cl_assert_equal_b(true, git_odb_exists(odb, &_existing_oid)); |
| 103 | + |
| 104 | + cl_assert_equal_i(1, _fake->exists_calls); |
| 105 | + cl_assert_equal_i(0, _fake->refresh_calls); |
| 106 | +} |
| 107 | + |
| 108 | +void test_odb_backend_refreshing__read_is_invoked_once_on_success(void) |
| 109 | +{ |
| 110 | + git_object *obj; |
| 111 | + |
| 112 | + cl_git_pass(git_object_lookup(&obj, _repo, &_existing_oid, GIT_OBJECT_ANY)); |
| 113 | + |
| 114 | + cl_assert_equal_i(1, _fake->read_calls); |
| 115 | + cl_assert_equal_i(0, _fake->refresh_calls); |
| 116 | + |
| 117 | + git_object_free(obj); |
| 118 | +} |
| 119 | + |
| 120 | +void test_odb_backend_refreshing__readprefix_is_invoked_once_on_success(void) |
| 121 | +{ |
| 122 | + git_object *obj; |
| 123 | + |
| 124 | + cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_existing_oid, 7, GIT_OBJECT_ANY)); |
| 125 | + |
| 126 | + cl_assert_equal_i(1, _fake->read_prefix_calls); |
| 127 | + cl_assert_equal_i(0, _fake->refresh_calls); |
| 128 | + |
| 129 | + git_object_free(obj); |
| 130 | +} |
| 131 | + |
| 132 | +void test_odb_backend_refreshing__readheader_is_invoked_once_on_success(void) |
| 133 | +{ |
| 134 | + git_odb *odb; |
| 135 | + size_t len; |
| 136 | + git_object_t type; |
| 137 | + |
| 138 | + cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); |
| 139 | + |
| 140 | + cl_git_pass(git_odb_read_header(&len, &type, odb, &_existing_oid)); |
| 141 | + |
| 142 | + cl_assert_equal_i(1, _fake->read_header_calls); |
| 143 | + cl_assert_equal_i(0, _fake->refresh_calls); |
| 144 | +} |
| 145 | + |
| 146 | +void test_odb_backend_refreshing__read_is_invoked_twice_when_revparsing_a_full_oid(void) |
| 147 | +{ |
| 148 | + git_object *obj; |
| 149 | + |
| 150 | + cl_git_fail_with( |
| 151 | + git_revparse_single(&obj, _repo, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"), |
| 152 | + GIT_ENOTFOUND); |
| 153 | + |
| 154 | + cl_assert_equal_i(2, _fake->read_calls); |
| 155 | + cl_assert_equal_i(1, _fake->refresh_calls); |
| 156 | +} |
| 157 | + |
| 158 | +void test_odb_backend_refreshing__refresh_is_invoked(void) |
| 159 | +{ |
| 160 | + git_odb *odb; |
| 161 | + |
| 162 | + cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); |
| 163 | + cl_assert_equal_i(0, git_odb_refresh(odb)); |
| 164 | + |
| 165 | + cl_assert_equal_i(1, _fake->refresh_calls); |
| 166 | +} |
| 167 | + |
| 168 | +void test_odb_backend_refreshing__refresh_suppressed_with_no_refresh(void) |
| 169 | +{ |
| 170 | + git_odb *odb; |
| 171 | + |
| 172 | + cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); |
| 173 | + cl_assert_equal_b(false, git_odb_exists_ext(odb, &_nonexisting_oid, GIT_ODB_LOOKUP_NO_REFRESH)); |
| 174 | + |
| 175 | + cl_assert_equal_i(0, _fake->refresh_calls); |
| 176 | +} |
0 commit comments