foreachglob.c 2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "clar_libgit2.h"
#include "refs.h"

static git_repository *repo;
static git_reference *fake_remote;

void test_refs_foreachglob__initialize(void)
{
	git_oid id;

	cl_fixture_sandbox("testrepo.git");
	cl_git_pass(git_repository_open(&repo, "testrepo.git"));

	cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
15
	cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
16 17 18 19 20
}

void test_refs_foreachglob__cleanup(void)
{
	git_reference_free(fake_remote);
21 22
	fake_remote = NULL;

23
	git_repository_free(repo);
24
	repo = NULL;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

	cl_fixture_cleanup("testrepo.git");
}

static int count_cb(const char *reference_name, void *payload)
{
	int *count = (int *)payload;

	GIT_UNUSED(reference_name);

	(*count)++;

	return 0;
}

40
static void assert_retrieval(const char *glob, int expected_count)
41 42 43
{
	int count = 0;

44
	cl_git_pass(git_reference_foreach_glob(repo, glob, count_cb, &count));
45 46 47 48 49 50

	cl_assert_equal_i(expected_count, count);
}

void test_refs_foreachglob__retrieve_all_refs(void)
{
51 52
	/* 12 heads (including one packed head) + 1 note + 2 remotes + 7 tags + 1 blob */
	assert_retrieval("*", 23);
53 54 55 56
}

void test_refs_foreachglob__retrieve_remote_branches(void)
{
57
	assert_retrieval("refs/remotes/*", 2);
58 59 60 61
}

void test_refs_foreachglob__retrieve_local_branches(void)
{
62
	assert_retrieval("refs/heads/*", 12);
63 64
}

65 66 67 68 69
void test_refs_foreachglob__retrieve_nonexistant(void)
{
	assert_retrieval("refs/nonexistent/*", 0);
}

70 71 72 73 74 75 76
void test_refs_foreachglob__retrieve_partially_named_references(void)
{
	/*
	 * refs/heads/packed-test, refs/heads/test
	 * refs/remotes/test/master, refs/tags/test
	 */

77
	assert_retrieval("*test*", 4);
78
}
79 80 81 82 83 84 85 86 87 88


static int interrupt_cb(const char *reference_name, void *payload)
{
	int *count = (int *)payload;

	GIT_UNUSED(reference_name);

	(*count)++;

89
	return (*count == 11) ? -1000 : 0;
90 91 92 93 94 95
}

void test_refs_foreachglob__can_cancel(void)
{
	int count = 0;

96
	cl_assert_equal_i(-1000, git_reference_foreach_glob(
97
		repo, "*", interrupt_cb, &count) );
98 99 100

	cl_assert_equal_i(11, count);
}