diff options
Diffstat (limited to 'tests-clar/index/tests.c')
-rw-r--r-- | tests-clar/index/tests.c | 262 |
1 files changed, 217 insertions, 45 deletions
diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c index 3436f8d1e..88e374e6e 100644 --- a/tests-clar/index/tests.c +++ b/tests-clar/index/tests.c @@ -1,8 +1,8 @@ #include "clar_libgit2.h" #include "index.h" -static const int index_entry_count = 109; -static const int index_entry_count_2 = 1437; +static const size_t index_entry_count = 109; +static const size_t index_entry_count_2 = 1437; #define TEST_INDEX_PATH cl_fixture("testrepo.git/index") #define TEST_INDEX2_PATH cl_fixture("gitgit.index") #define TEST_INDEXBIG_PATH cl_fixture("big.index") @@ -10,7 +10,7 @@ static const int index_entry_count_2 = 1437; // Suite data struct test_entry { - int index; + size_t index; char path[128]; git_off_t file_size; git_time_t mtime; @@ -24,7 +24,6 @@ static struct test_entry test_entries[] = { {48, "src/revobject.h", 1448, 0x4C3F7FE2} }; - // Helpers static void copy_file(const char *src, const char *dst) { @@ -33,7 +32,7 @@ static void copy_file(const char *src, const char *dst) cl_git_pass(git_futils_readbuffer(&source_buf, src)); - dst_fd = git_futils_creat_withpath(dst, 0777, 0666); + dst_fd = git_futils_creat_withpath(dst, 0777, 0666); //-V536 if (dst_fd < 0) goto cleanup; @@ -72,11 +71,6 @@ void test_index_tests__initialize(void) { } -void test_index_tests__cleanup(void) -{ -} - - void test_index_tests__empty_index(void) { git_index *index; @@ -99,7 +93,7 @@ void test_index_tests__default_test_index(void) cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); cl_assert(index->on_disk); - cl_assert(git_index_entrycount(index) == (unsigned int)index_entry_count); + cl_assert(git_index_entrycount(index) == index_entry_count); cl_assert(index->entries.sorted); entries = (git_index_entry **)index->entries.contents; @@ -122,7 +116,7 @@ void test_index_tests__gitgit_index(void) cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH)); cl_assert(index->on_disk); - cl_assert(git_index_entrycount(index) == (unsigned int)index_entry_count_2); + cl_assert(git_index_entrycount(index) == index_entry_count_2); cl_assert(index->entries.sorted); cl_assert(index->tree != NULL); @@ -137,8 +131,10 @@ void test_index_tests__find_in_existing(void) cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { - int idx = git_index_find(index, test_entries[i].path); - cl_assert(idx == test_entries[i].index); + size_t idx; + + cl_assert(!git_index_find(&idx, index, test_entries[i].path)); + cl_assert(idx == test_entries[i].index); } git_index_free(index); @@ -152,8 +148,7 @@ void test_index_tests__find_in_empty(void) cl_git_pass(git_index_open(&index, "fake-index")); for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { - int idx = git_index_find(index, test_entries[i].path); - cl_assert(idx == GIT_ENOTFOUND); + cl_assert(GIT_ENOTFOUND == git_index_find(NULL, index, test_entries[i].path)); } git_index_free(index); @@ -203,44 +198,221 @@ void test_index_tests__sort1(void) git_index_free(index); } +static void cleanup_myrepo(void *opaque) +{ + GIT_UNUSED(opaque); + cl_fixture_cleanup("myrepo"); +} + void test_index_tests__add(void) { - git_index *index; - git_filebuf file = GIT_FILEBUF_INIT; - git_repository *repo; - git_index_entry *entry; - git_oid id1; + git_index *index; + git_filebuf file = GIT_FILEBUF_INIT; + git_repository *repo; + const git_index_entry *entry; + git_oid id1; - /* Intialize a new repository */ - cl_git_pass(git_repository_init(&repo, "./myrepo", 0)); + cl_set_cleanup(&cleanup_myrepo, NULL); - /* Ensure we're the only guy in the room */ - cl_git_pass(git_repository_index(&index, repo)); - cl_assert(git_index_entrycount(index) == 0); + /* Intialize a new repository */ + cl_git_pass(git_repository_init(&repo, "./myrepo", 0)); - /* Create a new file in the working directory */ - cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777)); - cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0)); - cl_git_pass(git_filebuf_write(&file, "hey there\n", 10)); - cl_git_pass(git_filebuf_commit(&file, 0666)); + /* Ensure we're the only guy in the room */ + cl_git_pass(git_repository_index(&index, repo)); + cl_assert(git_index_entrycount(index) == 0); - /* Store the expected hash of the file/blob - * This has been generated by executing the following - * $ echo "hey there" | git hash-object --stdin - */ - cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); + /* Create a new file in the working directory */ + cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777)); + cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0)); + cl_git_pass(git_filebuf_write(&file, "hey there\n", 10)); + cl_git_pass(git_filebuf_commit(&file, 0666)); - /* Add the new file to the index */ - cl_git_pass(git_index_add(index, "test.txt", 0)); + /* Store the expected hash of the file/blob + * This has been generated by executing the following + * $ echo "hey there" | git hash-object --stdin + */ + cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); - /* Wow... it worked! */ - cl_assert(git_index_entrycount(index) == 1); - entry = git_index_get(index, 0); + /* Add the new file to the index */ + cl_git_pass(git_index_add_bypath(index, "test.txt")); - /* And the built-in hashing mechanism worked as expected */ - cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); + /* Wow... it worked! */ + cl_assert(git_index_entrycount(index) == 1); + entry = git_index_get_byindex(index, 0); - git_index_free(index); - git_repository_free(repo); + /* And the built-in hashing mechanism worked as expected */ + cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); + + /* Test access by path instead of index */ + cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL); + cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); + + git_index_free(index); + git_repository_free(repo); +} + +static void cleanup_1397(void *opaque) +{ + GIT_UNUSED(opaque); + cl_git_sandbox_cleanup(); +} + +void test_index_tests__add_issue_1397(void) +{ + git_index *index; + git_repository *repo; + const git_index_entry *entry; + git_oid id1; + + cl_set_cleanup(&cleanup_1397, NULL); + + repo = cl_git_sandbox_init("issue_1397"); + + cl_repo_set_bool(repo, "core.autocrlf", true); + + /* Ensure we're the only guy in the room */ + cl_git_pass(git_repository_index(&index, repo)); + + /* Store the expected hash of the file/blob + * This has been generated by executing the following + * $ git hash-object crlf_file.txt + */ + cl_git_pass(git_oid_fromstr(&id1, "8312e0889a9cbab77c732b6bc39b51a683e3a318")); + + /* Make sure the initial SHA-1 is correct */ + cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL); + cl_assert_(git_oid_cmp(&id1, &entry->oid) == 0, "first oid check"); + + /* Update the index */ + cl_git_pass(git_index_add_bypath(index, "crlf_file.txt")); + + /* Check the new SHA-1 */ + cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL); + cl_assert_(git_oid_cmp(&id1, &entry->oid) == 0, "second oid check"); + + git_index_free(index); } +void test_index_tests__add_bypath_to_a_bare_repository_returns_EBAREPO(void) +{ + git_repository *bare_repo; + git_index *index; + + cl_git_pass(git_repository_open(&bare_repo, cl_fixture("testrepo.git"))); + cl_git_pass(git_repository_index(&index, bare_repo)); + + cl_assert_equal_i(GIT_EBAREREPO, git_index_add_bypath(index, "test.txt")); + + git_index_free(index); + git_repository_free(bare_repo); +} + +/* Test that writing an invalid filename fails */ +void test_index_tests__write_invalid_filename(void) +{ + git_repository *repo; + git_index *index; + git_oid expected; + + p_mkdir("read_tree", 0700); + + cl_git_pass(git_repository_init(&repo, "./read_tree", 0)); + cl_git_pass(git_repository_index(&index, repo)); + + cl_assert(git_index_entrycount(index) == 0); + + cl_git_mkfile("./read_tree/.git/hello", NULL); + + cl_git_pass(git_index_add_bypath(index, ".git/hello")); + + /* write-tree */ + cl_git_fail(git_index_write_tree(&expected, index)); + + git_index_free(index); + git_repository_free(repo); + + cl_fixture_cleanup("read_tree"); +} + +void test_index_tests__remove_entry(void) +{ + git_repository *repo; + git_index *index; + + p_mkdir("index_test", 0770); + + cl_git_pass(git_repository_init(&repo, "index_test", 0)); + cl_git_pass(git_repository_index(&index, repo)); + cl_assert(git_index_entrycount(index) == 0); + + cl_git_mkfile("index_test/hello", NULL); + cl_git_pass(git_index_add_bypath(index, "hello")); + cl_git_pass(git_index_write(index)); + + cl_git_pass(git_index_read(index)); /* reload */ + cl_assert(git_index_entrycount(index) == 1); + cl_assert(git_index_get_bypath(index, "hello", 0) != NULL); + + cl_git_pass(git_index_remove(index, "hello", 0)); + cl_git_pass(git_index_write(index)); + + cl_git_pass(git_index_read(index)); /* reload */ + cl_assert(git_index_entrycount(index) == 0); + cl_assert(git_index_get_bypath(index, "hello", 0) == NULL); + + git_index_free(index); + git_repository_free(repo); + cl_fixture_cleanup("index_test"); +} + +void test_index_tests__remove_directory(void) +{ + git_repository *repo; + git_index *index; + + p_mkdir("index_test", 0770); + + cl_git_pass(git_repository_init(&repo, "index_test", 0)); + cl_git_pass(git_repository_index(&index, repo)); + cl_assert_equal_i(0, (int)git_index_entrycount(index)); + + p_mkdir("index_test/a", 0770); + cl_git_mkfile("index_test/a/1.txt", NULL); + cl_git_mkfile("index_test/a/2.txt", NULL); + cl_git_mkfile("index_test/a/3.txt", NULL); + cl_git_mkfile("index_test/b.txt", NULL); + + cl_git_pass(git_index_add_bypath(index, "a/1.txt")); + cl_git_pass(git_index_add_bypath(index, "a/2.txt")); + cl_git_pass(git_index_add_bypath(index, "a/3.txt")); + cl_git_pass(git_index_add_bypath(index, "b.txt")); + cl_git_pass(git_index_write(index)); + + cl_git_pass(git_index_read(index)); /* reload */ + cl_assert_equal_i(4, (int)git_index_entrycount(index)); + cl_assert(git_index_get_bypath(index, "a/1.txt", 0) != NULL); + cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL); + cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL); + + cl_git_pass(git_index_remove(index, "a/1.txt", 0)); + cl_git_pass(git_index_write(index)); + + cl_git_pass(git_index_read(index)); /* reload */ + cl_assert_equal_i(3, (int)git_index_entrycount(index)); + cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL); + cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL); + cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL); + + cl_git_pass(git_index_remove_directory(index, "a", 0)); + cl_git_pass(git_index_write(index)); + + cl_git_pass(git_index_read(index)); /* reload */ + cl_assert_equal_i(1, (int)git_index_entrycount(index)); + cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL); + cl_assert(git_index_get_bypath(index, "a/2.txt", 0) == NULL); + cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL); + + git_index_free(index); + git_repository_free(repo); + cl_fixture_cleanup("index_test"); +} |