]>
git.proxmox.com Git - libgit2.git/blob - tests-clar/index/tests.c
1 #include "clar_libgit2.h"
4 static const size_t index_entry_count
= 109;
5 static const size_t index_entry_count_2
= 1437;
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")
19 static struct test_entry test_entries
[] = {
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}
28 static void copy_file(const char *src
, const char *dst
)
30 git_buf source_buf
= GIT_BUF_INIT
;
33 cl_git_pass(git_futils_readbuffer(&source_buf
, src
));
35 dst_fd
= git_futils_creat_withpath(dst
, 0777, 0666); //-V536
39 cl_git_pass(p_write(dst_fd
, source_buf
.ptr
, source_buf
.size
));
42 git_buf_free(&source_buf
);
46 static void files_are_equal(const char *a
, const char *b
)
48 git_buf buf_a
= GIT_BUF_INIT
;
49 git_buf buf_b
= GIT_BUF_INIT
;
52 if (git_futils_readbuffer(&buf_a
, a
) < 0)
55 if (git_futils_readbuffer(&buf_b
, b
) < 0) {
60 pass
= (buf_a
.size
== buf_b
.size
&& !memcmp(buf_a
.ptr
, buf_b
.ptr
, buf_a
.size
));
69 // Fixture setup and teardown
70 void test_index_tests__initialize(void)
74 void test_index_tests__empty_index(void)
78 cl_git_pass(git_index_open(&index
, "in-memory-index"));
79 cl_assert(index
->on_disk
== 0);
81 cl_assert(git_index_entrycount(index
) == 0);
82 cl_assert(index
->entries
.sorted
);
84 git_index_free(index
);
87 void test_index_tests__default_test_index(void)
91 git_index_entry
**entries
;
93 cl_git_pass(git_index_open(&index
, TEST_INDEX_PATH
));
94 cl_assert(index
->on_disk
);
96 cl_assert(git_index_entrycount(index
) == index_entry_count
);
97 cl_assert(index
->entries
.sorted
);
99 entries
= (git_index_entry
**)index
->entries
.contents
;
101 for (i
= 0; i
< ARRAY_SIZE(test_entries
); ++i
) {
102 git_index_entry
*e
= entries
[test_entries
[i
].index
];
104 cl_assert_equal_s(e
->path
, test_entries
[i
].path
);
105 cl_assert(e
->mtime
.seconds
== test_entries
[i
].mtime
);
106 cl_assert(e
->file_size
== test_entries
[i
].file_size
);
109 git_index_free(index
);
112 void test_index_tests__gitgit_index(void)
116 cl_git_pass(git_index_open(&index
, TEST_INDEX2_PATH
));
117 cl_assert(index
->on_disk
);
119 cl_assert(git_index_entrycount(index
) == index_entry_count_2
);
120 cl_assert(index
->entries
.sorted
);
121 cl_assert(index
->tree
!= NULL
);
123 git_index_free(index
);
126 void test_index_tests__find_in_existing(void)
131 cl_git_pass(git_index_open(&index
, TEST_INDEX_PATH
));
133 for (i
= 0; i
< ARRAY_SIZE(test_entries
); ++i
) {
136 cl_assert(!git_index_find(&idx
, index
, test_entries
[i
].path
));
137 cl_assert(idx
== test_entries
[i
].index
);
140 git_index_free(index
);
143 void test_index_tests__find_in_empty(void)
148 cl_git_pass(git_index_open(&index
, "fake-index"));
150 for (i
= 0; i
< ARRAY_SIZE(test_entries
); ++i
) {
151 cl_assert(GIT_ENOTFOUND
== git_index_find(NULL
, index
, test_entries
[i
].path
));
154 git_index_free(index
);
157 void test_index_tests__write(void)
161 copy_file(TEST_INDEXBIG_PATH
, "index_rewrite");
163 cl_git_pass(git_index_open(&index
, "index_rewrite"));
164 cl_assert(index
->on_disk
);
166 cl_git_pass(git_index_write(index
));
167 files_are_equal(TEST_INDEXBIG_PATH
, "index_rewrite");
169 git_index_free(index
);
171 p_unlink("index_rewrite");
174 void test_index_tests__sort0(void)
176 // sort the entires in an index
178 * TODO: This no longer applies:
179 * index sorting in Git uses some specific changes to the way
180 * directories are sorted.
182 * We need to specificially check for this by creating a new
183 * index, adding entries in random order and then
184 * checking for consistency
188 void test_index_tests__sort1(void)
190 // sort the entires in an empty index
193 cl_git_pass(git_index_open(&index
, "fake-index"));
195 /* FIXME: this test is slightly dumb */
196 cl_assert(index
->entries
.sorted
);
198 git_index_free(index
);
201 static void cleanup_myrepo(void *opaque
)
204 cl_fixture_cleanup("myrepo");
207 void test_index_tests__add(void)
210 git_filebuf file
= GIT_FILEBUF_INIT
;
211 git_repository
*repo
;
212 const git_index_entry
*entry
;
215 cl_set_cleanup(&cleanup_myrepo
, NULL
);
217 /* Intialize a new repository */
218 cl_git_pass(git_repository_init(&repo
, "./myrepo", 0));
220 /* Ensure we're the only guy in the room */
221 cl_git_pass(git_repository_index(&index
, repo
));
222 cl_assert(git_index_entrycount(index
) == 0);
224 /* Create a new file in the working directory */
225 cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
226 cl_git_pass(git_filebuf_open(&file
, "myrepo/test.txt", 0));
227 cl_git_pass(git_filebuf_write(&file
, "hey there\n", 10));
228 cl_git_pass(git_filebuf_commit(&file
, 0666));
230 /* Store the expected hash of the file/blob
231 * This has been generated by executing the following
232 * $ echo "hey there" | git hash-object --stdin
234 cl_git_pass(git_oid_fromstr(&id1
, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
236 /* Add the new file to the index */
237 cl_git_pass(git_index_add_bypath(index
, "test.txt"));
239 /* Wow... it worked! */
240 cl_assert(git_index_entrycount(index
) == 1);
241 entry
= git_index_get_byindex(index
, 0);
243 /* And the built-in hashing mechanism worked as expected */
244 cl_assert(git_oid_cmp(&id1
, &entry
->oid
) == 0);
246 /* Test access by path instead of index */
247 cl_assert((entry
= git_index_get_bypath(index
, "test.txt", 0)) != NULL
);
248 cl_assert(git_oid_cmp(&id1
, &entry
->oid
) == 0);
250 git_index_free(index
);
251 git_repository_free(repo
);
254 void test_index_tests__add_bypath_to_a_bare_repository_returns_EBAREPO(void)
256 git_repository
*bare_repo
;
259 cl_git_pass(git_repository_open(&bare_repo
, cl_fixture("testrepo.git")));
260 cl_git_pass(git_repository_index(&index
, bare_repo
));
262 cl_assert_equal_i(GIT_EBAREREPO
, git_index_add_bypath(index
, "test.txt"));
264 git_index_free(index
);
265 git_repository_free(bare_repo
);
268 /* Test that writing an invalid filename fails */
269 void test_index_tests__write_invalid_filename(void)
271 git_repository
*repo
;
275 p_mkdir("read_tree", 0700);
277 cl_git_pass(git_repository_init(&repo
, "./read_tree", 0));
278 cl_git_pass(git_repository_index(&index
, repo
));
280 cl_assert(git_index_entrycount(index
) == 0);
282 cl_git_mkfile("./read_tree/.git/hello", NULL
);
284 cl_git_pass(git_index_add_bypath(index
, ".git/hello"));
287 cl_git_fail(git_index_write_tree(&expected
, index
));
289 git_index_free(index
);
290 git_repository_free(repo
);
292 cl_fixture_cleanup("read_tree");
295 void test_index_tests__remove_entry(void)
297 git_repository
*repo
;
300 p_mkdir("index_test", 0770);
302 cl_git_pass(git_repository_init(&repo
, "index_test", 0));
303 cl_git_pass(git_repository_index(&index
, repo
));
304 cl_assert(git_index_entrycount(index
) == 0);
306 cl_git_mkfile("index_test/hello", NULL
);
307 cl_git_pass(git_index_add_bypath(index
, "hello"));
308 cl_git_pass(git_index_write(index
));
310 cl_git_pass(git_index_read(index
)); /* reload */
311 cl_assert(git_index_entrycount(index
) == 1);
312 cl_assert(git_index_get_bypath(index
, "hello", 0) != NULL
);
314 cl_git_pass(git_index_remove(index
, "hello", 0));
315 cl_git_pass(git_index_write(index
));
317 cl_git_pass(git_index_read(index
)); /* reload */
318 cl_assert(git_index_entrycount(index
) == 0);
319 cl_assert(git_index_get_bypath(index
, "hello", 0) == NULL
);
321 git_index_free(index
);
322 git_repository_free(repo
);
323 cl_fixture_cleanup("index_test");
326 void test_index_tests__remove_directory(void)
328 git_repository
*repo
;
331 p_mkdir("index_test", 0770);
333 cl_git_pass(git_repository_init(&repo
, "index_test", 0));
334 cl_git_pass(git_repository_index(&index
, repo
));
335 cl_assert_equal_i(0, (int)git_index_entrycount(index
));
337 p_mkdir("index_test/a", 0770);
338 cl_git_mkfile("index_test/a/1.txt", NULL
);
339 cl_git_mkfile("index_test/a/2.txt", NULL
);
340 cl_git_mkfile("index_test/a/3.txt", NULL
);
341 cl_git_mkfile("index_test/b.txt", NULL
);
343 cl_git_pass(git_index_add_bypath(index
, "a/1.txt"));
344 cl_git_pass(git_index_add_bypath(index
, "a/2.txt"));
345 cl_git_pass(git_index_add_bypath(index
, "a/3.txt"));
346 cl_git_pass(git_index_add_bypath(index
, "b.txt"));
347 cl_git_pass(git_index_write(index
));
349 cl_git_pass(git_index_read(index
)); /* reload */
350 cl_assert_equal_i(4, (int)git_index_entrycount(index
));
351 cl_assert(git_index_get_bypath(index
, "a/1.txt", 0) != NULL
);
352 cl_assert(git_index_get_bypath(index
, "a/2.txt", 0) != NULL
);
353 cl_assert(git_index_get_bypath(index
, "b.txt", 0) != NULL
);
355 cl_git_pass(git_index_remove(index
, "a/1.txt", 0));
356 cl_git_pass(git_index_write(index
));
358 cl_git_pass(git_index_read(index
)); /* reload */
359 cl_assert_equal_i(3, (int)git_index_entrycount(index
));
360 cl_assert(git_index_get_bypath(index
, "a/1.txt", 0) == NULL
);
361 cl_assert(git_index_get_bypath(index
, "a/2.txt", 0) != NULL
);
362 cl_assert(git_index_get_bypath(index
, "b.txt", 0) != NULL
);
364 cl_git_pass(git_index_remove_directory(index
, "a", 0));
365 cl_git_pass(git_index_write(index
));
367 cl_git_pass(git_index_read(index
)); /* reload */
368 cl_assert_equal_i(1, (int)git_index_entrycount(index
));
369 cl_assert(git_index_get_bypath(index
, "a/1.txt", 0) == NULL
);
370 cl_assert(git_index_get_bypath(index
, "a/2.txt", 0) == NULL
);
371 cl_assert(git_index_get_bypath(index
, "b.txt", 0) != NULL
);
373 git_index_free(index
);
374 git_repository_free(repo
);
375 cl_fixture_cleanup("index_test");