]>
Commit | Line | Data |
---|---|---|
6c106eec BS |
1 | #include "clar_libgit2.h" |
2 | #include "index.h" | |
3 | ||
a8122b5d RB |
4 | static const size_t index_entry_count = 109; |
5 | static const size_t index_entry_count_2 = 1437; | |
6c106eec BS |
6 | #define TEST_INDEX_PATH cl_fixture("testrepo.git/index") |
7 | #define TEST_INDEX2_PATH cl_fixture("gitgit.index") | |
8 | #define TEST_INDEXBIG_PATH cl_fixture("big.index") | |
9 | ||
10 | ||
11 | // Suite data | |
12 | struct test_entry { | |
13 | int index; | |
14 | char path[128]; | |
15 | git_off_t file_size; | |
16 | git_time_t mtime; | |
17 | }; | |
18 | ||
b482c420 | 19 | static struct test_entry test_entries[] = { |
6c106eec BS |
20 | {4, "Makefile", 5064, 0x4C3F7F33}, |
21 | {62, "tests/Makefile", 2631, 0x4C3F7F33}, | |
22 | {36, "src/index.c", 10014, 0x4C43368D}, | |
23 | {6, "git.git-authors", 2709, 0x4C3F7F33}, | |
24 | {48, "src/revobject.h", 1448, 0x4C3F7FE2} | |
25 | }; | |
26 | ||
6c106eec | 27 | // Helpers |
270303ca | 28 | static void copy_file(const char *src, const char *dst) |
6c106eec BS |
29 | { |
30 | git_buf source_buf = GIT_BUF_INIT; | |
31 | git_file dst_fd; | |
6c106eec | 32 | |
270303ca | 33 | cl_git_pass(git_futils_readbuffer(&source_buf, src)); |
6c106eec | 34 | |
3f035860 | 35 | dst_fd = git_futils_creat_withpath(dst, 0777, 0666); //-V536 |
6c106eec BS |
36 | if (dst_fd < 0) |
37 | goto cleanup; | |
38 | ||
270303ca | 39 | cl_git_pass(p_write(dst_fd, source_buf.ptr, source_buf.size)); |
6c106eec BS |
40 | |
41 | cleanup: | |
42 | git_buf_free(&source_buf); | |
43 | p_close(dst_fd); | |
6c106eec BS |
44 | } |
45 | ||
270303ca | 46 | static void files_are_equal(const char *a, const char *b) |
6c106eec BS |
47 | { |
48 | git_buf buf_a = GIT_BUF_INIT; | |
49 | git_buf buf_b = GIT_BUF_INIT; | |
270303ca | 50 | int pass; |
6c106eec | 51 | |
e172cf08 | 52 | if (git_futils_readbuffer(&buf_a, a) < 0) |
270303ca | 53 | cl_assert(0); |
6c106eec | 54 | |
e172cf08 | 55 | if (git_futils_readbuffer(&buf_b, b) < 0) { |
6c106eec | 56 | git_buf_free(&buf_a); |
270303ca | 57 | cl_assert(0); |
6c106eec BS |
58 | } |
59 | ||
270303ca | 60 | pass = (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size)); |
6c106eec BS |
61 | |
62 | git_buf_free(&buf_a); | |
63 | git_buf_free(&buf_b); | |
13ed2966 MS |
64 | |
65 | cl_assert(pass); | |
6c106eec BS |
66 | } |
67 | ||
68 | ||
69 | // Fixture setup and teardown | |
70 | void test_index_tests__initialize(void) | |
71 | { | |
72 | } | |
73 | ||
6c106eec BS |
74 | void test_index_tests__empty_index(void) |
75 | { | |
76 | git_index *index; | |
77 | ||
78 | cl_git_pass(git_index_open(&index, "in-memory-index")); | |
79 | cl_assert(index->on_disk == 0); | |
80 | ||
81 | cl_assert(git_index_entrycount(index) == 0); | |
82 | cl_assert(index->entries.sorted); | |
83 | ||
84 | git_index_free(index); | |
85 | } | |
86 | ||
87 | void test_index_tests__default_test_index(void) | |
88 | { | |
89 | git_index *index; | |
90 | unsigned int i; | |
91 | git_index_entry **entries; | |
92 | ||
93 | cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); | |
94 | cl_assert(index->on_disk); | |
95 | ||
a8122b5d | 96 | cl_assert(git_index_entrycount(index) == index_entry_count); |
6c106eec BS |
97 | cl_assert(index->entries.sorted); |
98 | ||
99 | entries = (git_index_entry **)index->entries.contents; | |
100 | ||
b482c420 BS |
101 | for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { |
102 | git_index_entry *e = entries[test_entries[i].index]; | |
6c106eec | 103 | |
946a6dc4 | 104 | cl_assert_equal_s(e->path, test_entries[i].path); |
b482c420 BS |
105 | cl_assert(e->mtime.seconds == test_entries[i].mtime); |
106 | cl_assert(e->file_size == test_entries[i].file_size); | |
6c106eec BS |
107 | } |
108 | ||
109 | git_index_free(index); | |
110 | } | |
111 | ||
112 | void test_index_tests__gitgit_index(void) | |
113 | { | |
114 | git_index *index; | |
115 | ||
116 | cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH)); | |
117 | cl_assert(index->on_disk); | |
118 | ||
a8122b5d | 119 | cl_assert(git_index_entrycount(index) == index_entry_count_2); |
6c106eec BS |
120 | cl_assert(index->entries.sorted); |
121 | cl_assert(index->tree != NULL); | |
122 | ||
123 | git_index_free(index); | |
124 | } | |
125 | ||
126 | void test_index_tests__find_in_existing(void) | |
127 | { | |
128 | git_index *index; | |
129 | unsigned int i; | |
130 | ||
131 | cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); | |
132 | ||
b482c420 BS |
133 | for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { |
134 | int idx = git_index_find(index, test_entries[i].path); | |
135 | cl_assert(idx == test_entries[i].index); | |
6c106eec BS |
136 | } |
137 | ||
138 | git_index_free(index); | |
139 | } | |
140 | ||
141 | void test_index_tests__find_in_empty(void) | |
142 | { | |
143 | git_index *index; | |
144 | unsigned int i; | |
145 | ||
146 | cl_git_pass(git_index_open(&index, "fake-index")); | |
147 | ||
b482c420 BS |
148 | for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { |
149 | int idx = git_index_find(index, test_entries[i].path); | |
904b67e6 | 150 | cl_assert(idx == GIT_ENOTFOUND); |
6c106eec BS |
151 | } |
152 | ||
153 | git_index_free(index); | |
154 | } | |
155 | ||
156 | void test_index_tests__write(void) | |
157 | { | |
158 | git_index *index; | |
159 | ||
270303ca | 160 | copy_file(TEST_INDEXBIG_PATH, "index_rewrite"); |
6c106eec BS |
161 | |
162 | cl_git_pass(git_index_open(&index, "index_rewrite")); | |
163 | cl_assert(index->on_disk); | |
164 | ||
165 | cl_git_pass(git_index_write(index)); | |
270303ca | 166 | files_are_equal(TEST_INDEXBIG_PATH, "index_rewrite"); |
6c106eec BS |
167 | |
168 | git_index_free(index); | |
169 | ||
170 | p_unlink("index_rewrite"); | |
171 | } | |
172 | ||
173 | void test_index_tests__sort0(void) | |
174 | { | |
175 | // sort the entires in an index | |
176 | /* | |
177 | * TODO: This no longer applies: | |
178 | * index sorting in Git uses some specific changes to the way | |
179 | * directories are sorted. | |
180 | * | |
181 | * We need to specificially check for this by creating a new | |
182 | * index, adding entries in random order and then | |
183 | * checking for consistency | |
184 | */ | |
185 | } | |
186 | ||
187 | void test_index_tests__sort1(void) | |
188 | { | |
189 | // sort the entires in an empty index | |
190 | git_index *index; | |
191 | ||
192 | cl_git_pass(git_index_open(&index, "fake-index")); | |
193 | ||
194 | /* FIXME: this test is slightly dumb */ | |
195 | cl_assert(index->entries.sorted); | |
196 | ||
197 | git_index_free(index); | |
198 | } | |
199 | ||
f6fded8f VM |
200 | static void cleanup_myrepo(void *opaque) |
201 | { | |
202 | GIT_UNUSED(opaque); | |
203 | cl_fixture_cleanup("myrepo"); | |
204 | } | |
205 | ||
6c106eec BS |
206 | void test_index_tests__add(void) |
207 | { | |
f6fded8f VM |
208 | git_index *index; |
209 | git_filebuf file = GIT_FILEBUF_INIT; | |
210 | git_repository *repo; | |
211 | const git_index_entry *entry; | |
212 | git_oid id1; | |
6c106eec | 213 | |
f6fded8f | 214 | cl_set_cleanup(&cleanup_myrepo, NULL); |
6c106eec | 215 | |
f6fded8f VM |
216 | /* Intialize a new repository */ |
217 | cl_git_pass(git_repository_init(&repo, "./myrepo", 0)); | |
6c106eec | 218 | |
f6fded8f VM |
219 | /* Ensure we're the only guy in the room */ |
220 | cl_git_pass(git_repository_index(&index, repo)); | |
221 | cl_assert(git_index_entrycount(index) == 0); | |
6c106eec | 222 | |
f6fded8f VM |
223 | /* Create a new file in the working directory */ |
224 | cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777)); | |
225 | cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0)); | |
226 | cl_git_pass(git_filebuf_write(&file, "hey there\n", 10)); | |
227 | cl_git_pass(git_filebuf_commit(&file, 0666)); | |
6c106eec | 228 | |
f6fded8f VM |
229 | /* Store the expected hash of the file/blob |
230 | * This has been generated by executing the following | |
231 | * $ echo "hey there" | git hash-object --stdin | |
232 | */ | |
233 | cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); | |
6c106eec | 234 | |
f6fded8f VM |
235 | /* Add the new file to the index */ |
236 | cl_git_pass(git_index_add_from_workdir(index, "test.txt")); | |
6c106eec | 237 | |
f6fded8f VM |
238 | /* Wow... it worked! */ |
239 | cl_assert(git_index_entrycount(index) == 1); | |
240 | entry = git_index_get_byindex(index, 0); | |
6c106eec | 241 | |
f6fded8f VM |
242 | /* And the built-in hashing mechanism worked as expected */ |
243 | cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); | |
f45ec1a0 | 244 | |
f6fded8f VM |
245 | /* Test access by path instead of index */ |
246 | cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL); | |
247 | cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); | |
248 | ||
249 | git_index_free(index); | |
250 | git_repository_free(repo); | |
6c106eec BS |
251 | } |
252 | ||
33f95a9b | 253 | void test_index_tests__add_from_workdir_to_a_bare_repository_returns_EBAREPO(void) |
254 | { | |
255 | git_repository *bare_repo; | |
256 | git_index *index; | |
257 | ||
258 | cl_git_pass(git_repository_open(&bare_repo, cl_fixture("testrepo.git"))); | |
259 | cl_git_pass(git_repository_index(&index, bare_repo)); | |
260 | ||
261 | cl_assert_equal_i(GIT_EBAREREPO, git_index_add_from_workdir(index, "test.txt")); | |
262 | ||
263 | git_index_free(index); | |
264 | git_repository_free(bare_repo); | |
265 | } | |
1876360f SG |
266 | |
267 | /* Test that writing an invalid filename fails */ | |
268 | void test_index_tests__write_invalid_filename(void) | |
269 | { | |
270 | git_repository *repo; | |
271 | git_index *index; | |
272 | git_oid expected; | |
273 | ||
274 | p_mkdir("read_tree", 0700); | |
275 | ||
276 | cl_git_pass(git_repository_init(&repo, "./read_tree", 0)); | |
277 | cl_git_pass(git_repository_index(&index, repo)); | |
278 | ||
279 | cl_assert(git_index_entrycount(index) == 0); | |
280 | ||
281 | cl_git_mkfile("./read_tree/.git/hello", NULL); | |
282 | ||
283 | cl_git_pass(git_index_add_from_workdir(index, ".git/hello")); | |
284 | ||
285 | /* write-tree */ | |
286 | cl_git_fail(git_index_write_tree(&expected, index)); | |
287 | ||
288 | git_index_free(index); | |
289 | git_repository_free(repo); | |
290 | ||
291 | cl_fixture_cleanup("read_tree"); | |
292 | } |