diff options
author | Edward Thomson <ethomson@microsoft.com> | 2013-05-11 13:42:49 +0400 |
---|---|---|
committer | Edward Thomson <ethomson@microsoft.com> | 2013-05-11 13:42:49 +0400 |
commit | b6cc559a78a073f2aadd179fe40c09be7318c898 (patch) | |
tree | c40a7806c16eb133794427aa2e1ca25e86ce1d74 /tests-clar | |
parent | 4a65aead2ce2003902f4b54b331af33af7340b58 (diff) | |
parent | c58cac12c24fbb127cf1928bec20decb007a75e8 (diff) |
Merge pull request #1385 from carlosmn/refs-iter
Introduce a refs iterator
Diffstat (limited to 'tests-clar')
-rw-r--r-- | tests-clar/network/fetchlocal.c | 6 | ||||
-rw-r--r-- | tests-clar/refdb/inmemory.c | 4 | ||||
-rw-r--r-- | tests-clar/refdb/testdb.c | 56 | ||||
-rw-r--r-- | tests-clar/refs/foreachglob.c | 14 | ||||
-rw-r--r-- | tests-clar/refs/iterator.c | 94 | ||||
-rw-r--r-- | tests-clar/refs/list.c | 15 | ||||
-rw-r--r-- | tests-clar/refs/listall.c | 4 |
7 files changed, 147 insertions, 46 deletions
diff --git a/tests-clar/network/fetchlocal.c b/tests-clar/network/fetchlocal.c index bcf298cde..09335b3df 100644 --- a/tests-clar/network/fetchlocal.c +++ b/tests-clar/network/fetchlocal.c @@ -34,7 +34,7 @@ void test_network_fetchlocal__complete(void) cl_git_pass(git_remote_download(origin, transfer_cb, &callcount)); cl_git_pass(git_remote_update_tips(origin)); - cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); + cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(19, (int)refnames.count); cl_assert(callcount > 0); @@ -58,7 +58,7 @@ void test_network_fetchlocal__partial(void) const char *url; cl_set_cleanup(&cleanup_sandbox, NULL); - cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); + cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(1, (int)refnames.count); url = cl_git_fixture_url("testrepo.git"); @@ -69,7 +69,7 @@ void test_network_fetchlocal__partial(void) git_strarray_free(&refnames); - cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); + cl_git_pass(git_reference_list(&refnames, repo)); cl_assert_equal_i(20, (int)refnames.count); /* 18 remote + 1 local */ cl_assert(callcount > 0); diff --git a/tests-clar/refdb/inmemory.c b/tests-clar/refdb/inmemory.c index 243b5bb37..d2594cd6d 100644 --- a/tests-clar/refdb/inmemory.c +++ b/tests-clar/refdb/inmemory.c @@ -163,7 +163,7 @@ void test_refdb_inmemory__foreach(void) cl_git_pass(git_oid_fromstr(&oid3, "763d71aadf09a7951596c9746c024e7eece7c7af")); cl_git_pass(git_reference_create(&write3, repo, GIT_REFS_HEADS_DIR "test3", &oid3, 0)); - cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, foreach_test, &i)); + cl_git_pass(git_reference_foreach(repo,foreach_test, &i)); cl_assert_equal_i(3, (int)i); git_reference_free(write1); @@ -210,7 +210,7 @@ void test_refdb_inmemory__delete(void) git_reference_delete(write3); git_reference_free(write3); - cl_git_pass(git_reference_foreach(repo, GIT_REF_LISTALL, delete_test, &i)); + cl_git_pass(git_reference_foreach(repo, delete_test, &i)); cl_assert_equal_i(1, (int)i); git_reference_free(write2); diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c index 627254e44..961e18d44 100644 --- a/tests-clar/refdb/testdb.c +++ b/tests-clar/refdb/testdb.c @@ -112,33 +112,49 @@ static int refdb_test_backend__lookup( return GIT_ENOTFOUND; } -static int refdb_test_backend__foreach( - git_refdb_backend *_backend, - unsigned int list_flags, - git_reference_foreach_cb callback, - void *payload) -{ - refdb_test_backend *backend; - refdb_test_entry *entry; +typedef struct { + git_reference_iterator parent; size_t i; +} refdb_test_iter; - assert(_backend); - backend = (refdb_test_backend *)_backend; +static int refdb_test_backend__iterator(git_reference_iterator **out, git_refdb_backend *_backend) +{ + refdb_test_iter *iter; - git_vector_foreach(&backend->refs, i, entry) { - if (entry->type == GIT_REF_OID && (list_flags & GIT_REF_OID) == 0) - continue; + GIT_UNUSED(_backend); - if (entry->type == GIT_REF_SYMBOLIC && (list_flags & GIT_REF_SYMBOLIC) == 0) - continue; + iter = git__calloc(1, sizeof(refdb_test_iter)); + GITERR_CHECK_ALLOC(iter); - if (callback(entry->name, payload) != 0) - return GIT_EUSER; - } + iter->parent.backend = _backend; + iter->i = 0; + + *out = (git_reference_iterator *) iter; return 0; } +static int refdb_test_backend__next(const char **name, git_reference_iterator *_iter) +{ + refdb_test_entry *entry; + refdb_test_backend *backend = (refdb_test_backend *) _iter->backend; + refdb_test_iter *iter = (refdb_test_iter *) _iter; + + entry = git_vector_get(&backend->refs, iter->i); + if (!entry) + return GIT_ITEROVER; + + *name = entry->name; + iter->i++; + + return 0; +} + +static void refdb_test_backend__iterator_free(git_reference_iterator *iter) +{ + git__free(iter); +} + static void refdb_test_entry_free(refdb_test_entry *entry) { if (entry->type == GIT_REF_SYMBOLIC) @@ -200,7 +216,9 @@ int refdb_backend_test( backend->parent.exists = &refdb_test_backend__exists; backend->parent.lookup = &refdb_test_backend__lookup; - backend->parent.foreach = &refdb_test_backend__foreach; + backend->parent.iterator = &refdb_test_backend__iterator; + backend->parent.next = &refdb_test_backend__next; + backend->parent.iterator_free = &refdb_test_backend__iterator_free; backend->parent.write = &refdb_test_backend__write; backend->parent.delete = &refdb_test_backend__delete; backend->parent.free = &refdb_test_backend__free; diff --git a/tests-clar/refs/foreachglob.c b/tests-clar/refs/foreachglob.c index 4da1a15dd..2c458082f 100644 --- a/tests-clar/refs/foreachglob.c +++ b/tests-clar/refs/foreachglob.c @@ -37,11 +37,11 @@ static int count_cb(const char *reference_name, void *payload) return 0; } -static void assert_retrieval(const char *glob, unsigned int flags, int expected_count) +static void assert_retrieval(const char *glob, int expected_count) { int count = 0; - cl_git_pass(git_reference_foreach_glob(repo, glob, flags, count_cb, &count)); + cl_git_pass(git_reference_foreach_glob(repo, glob, count_cb, &count)); cl_assert_equal_i(expected_count, count); } @@ -49,17 +49,17 @@ static void assert_retrieval(const char *glob, unsigned int flags, int expected_ void test_refs_foreachglob__retrieve_all_refs(void) { /* 12 heads (including one packed head) + 1 note + 2 remotes + 7 tags */ - assert_retrieval("*", GIT_REF_LISTALL, 22); + assert_retrieval("*", 22); } void test_refs_foreachglob__retrieve_remote_branches(void) { - assert_retrieval("refs/remotes/*", GIT_REF_LISTALL, 2); + assert_retrieval("refs/remotes/*", 2); } void test_refs_foreachglob__retrieve_local_branches(void) { - assert_retrieval("refs/heads/*", GIT_REF_LISTALL, 12); + assert_retrieval("refs/heads/*", 12); } void test_refs_foreachglob__retrieve_partially_named_references(void) @@ -69,7 +69,7 @@ void test_refs_foreachglob__retrieve_partially_named_references(void) * refs/remotes/test/master, refs/tags/test */ - assert_retrieval("*test*", GIT_REF_LISTALL, 4); + assert_retrieval("*test*", 4); } @@ -89,7 +89,7 @@ void test_refs_foreachglob__can_cancel(void) int count = 0; cl_assert_equal_i(GIT_EUSER, git_reference_foreach_glob( - repo, "*", GIT_REF_LISTALL, interrupt_cb, &count) ); + repo, "*", interrupt_cb, &count) ); cl_assert_equal_i(11, count); } diff --git a/tests-clar/refs/iterator.c b/tests-clar/refs/iterator.c new file mode 100644 index 000000000..d5555c657 --- /dev/null +++ b/tests-clar/refs/iterator.c @@ -0,0 +1,94 @@ +#include "clar_libgit2.h" +#include "refs.h" +#include "vector.h" + +static git_repository *repo; + +void test_refs_iterator__initialize(void) +{ + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); +} + +void test_refs_iterator__cleanup(void) +{ + git_repository_free(repo); +} + +static const char *refnames[] = { + "refs/heads/br2", + "refs/heads/cannot-fetch", + "refs/heads/chomped", + "refs/heads/haacked", + "refs/heads/master", + "refs/heads/not-good", + "refs/heads/packed", + "refs/heads/packed-test", + "refs/heads/subtrees", + "refs/heads/test", + "refs/heads/track-local", + "refs/heads/trailing", + "refs/notes/fanout", + "refs/remotes/test/master", + "refs/tags/annotated_tag_to_blob", + "refs/tags/e90810b", + "refs/tags/hard_tag", + "refs/tags/point_to_blob", + "refs/tags/taggerless", + "refs/tags/test", + "refs/tags/wrapped_tag", +}; + +void test_refs_iterator__list(void) +{ + git_reference_iterator *iter; + git_vector output; + char *refname; + int error; + size_t i; + + cl_git_pass(git_vector_init(&output, 32, git__strcmp_cb)); + cl_git_pass(git_reference_iterator_new(&iter, repo)); + + do { + const char *name; + error = git_reference_next(&name, iter); + cl_assert(error == 0 || error == GIT_ITEROVER); + if (error != GIT_ITEROVER) { + char *dup = git__strdup(name); + cl_assert(dup != NULL); + cl_git_pass(git_vector_insert(&output, dup)); + } + } while (!error); + + cl_assert_equal_i(output.length, ARRAY_SIZE(refnames)); + + git_vector_sort(&output); + git_vector_foreach(&output, i, refname) { + cl_assert_equal_s(refname, refnames[i]); + } + + git_reference_iterator_free(iter); + + git_vector_foreach(&output, i, refname) { + git__free(refname); + } + git_vector_free(&output); +} + +void test_refs_iterator__empty(void) +{ + git_reference_iterator *iter; + git_odb *odb; + const char *name; + git_repository *empty; + + cl_git_pass(git_odb_new(&odb)); + cl_git_pass(git_repository_wrap_odb(&empty, odb)); + + cl_git_pass(git_reference_iterator_new(&iter, empty)); + cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&name, iter)); + + git_reference_iterator_free(iter); + git_odb_free(odb); + git_repository_free(empty); +} diff --git a/tests-clar/refs/list.c b/tests-clar/refs/list.c index 3948b2b7a..c9c2af4a7 100644 --- a/tests-clar/refs/list.c +++ b/tests-clar/refs/list.c @@ -25,7 +25,7 @@ void test_refs_list__all(void) // try to list all the references in our test repo git_strarray ref_list; - cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_LISTALL)); + cl_git_pass(git_reference_list(&ref_list, g_repo)); /*{ unsigned short i; @@ -41,17 +41,6 @@ void test_refs_list__all(void) git_strarray_free(&ref_list); } -void test_refs_list__symbolic_only(void) -{ - // try to list only the symbolic references - git_strarray ref_list; - - cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_SYMBOLIC)); - cl_assert(ref_list.count == 0); /* no symrefs in the test repo */ - - git_strarray_free(&ref_list); -} - void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_extension(void) { git_strarray ref_list; @@ -61,7 +50,7 @@ void test_refs_list__do_not_retrieve_references_which_name_end_with_a_lock_exten "./testrepo/.git/refs/heads/hanwen.lock", "144344043ba4d4a405da03de3844aa829ae8be0e\n"); - cl_git_pass(git_reference_list(&ref_list, g_repo, GIT_REF_LISTALL)); + cl_git_pass(git_reference_list(&ref_list, g_repo)); cl_assert_equal_i((int)ref_list.count, 13); git_strarray_free(&ref_list); diff --git a/tests-clar/refs/listall.c b/tests-clar/refs/listall.c index 8f4c3746b..c696fbb2e 100644 --- a/tests-clar/refs/listall.c +++ b/tests-clar/refs/listall.c @@ -9,7 +9,7 @@ static void ensure_no_refname_starts_with_a_forward_slash(const char *path) size_t i; cl_git_pass(git_repository_open(&repo, path)); - cl_git_pass(git_reference_list(&ref_list, repo, GIT_REF_LISTALL)); + cl_git_pass(git_reference_list(&ref_list, repo)); cl_assert(ref_list.count > 0); @@ -38,7 +38,7 @@ void test_refs_listall__from_repository_opened_through_gitdir_path(void) void test_refs_listall__from_repository_with_no_trailing_newline(void) { cl_git_pass(git_repository_open(&repo, cl_fixture("bad_tag.git"))); - cl_git_pass(git_reference_list(&ref_list, repo, GIT_REF_LISTALL)); + cl_git_pass(git_reference_list(&ref_list, repo)); cl_assert(ref_list.count > 0); |