iterator.c 3.66 KB
Newer Older
1 2 3 4 5 6
#include "clar_libgit2.h"
#include "refs.h"

static git_repository *repo;
static git_reference *fake_remote;

7
void test_refs_branches_iterator__initialize(void)
8 9 10 11 12 13 14
{
	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, NULL));
16 17
}

18
void test_refs_branches_iterator__cleanup(void)
19 20
{
	git_reference_free(fake_remote);
21 22
	fake_remote = NULL;

23
	git_repository_free(repo);
24
	repo = NULL;
25 26

	cl_fixture_cleanup("testrepo.git");
27 28

	cl_git_sandbox_cleanup();
29 30 31 32
}

static void assert_retrieval(unsigned int flags, unsigned int expected_count)
{
33 34 35
	git_branch_iterator *iter;
	git_reference *ref;
	int count = 0, error;
36
	git_branch_t type;
37 38 39 40 41 42

	cl_git_pass(git_branch_iterator_new(&iter, repo, flags));
	while ((error = git_branch_next(&ref, &type, iter)) == 0) {
		count++;
		git_reference_free(ref);
	}
43

44 45
	git_branch_iterator_free(iter);
	cl_assert_equal_i(error, GIT_ITEROVER);
46 47 48
	cl_assert_equal_i(expected_count, count);
}

49
void test_refs_branches_iterator__retrieve_all_branches(void)
50
{
51
	assert_retrieval(GIT_BRANCH_ALL, 14);
52 53
}

54
void test_refs_branches_iterator__retrieve_remote_branches(void)
55 56 57 58
{
	assert_retrieval(GIT_BRANCH_REMOTE, 2);
}

59
void test_refs_branches_iterator__retrieve_local_branches(void)
60
{
61
	assert_retrieval(GIT_BRANCH_LOCAL, 12);
62 63 64 65 66 67 68 69 70 71 72
}

struct expectations {
	const char *branch_name;
	int encounters;
};

static void assert_branch_has_been_found(struct expectations *findings, const char* expected_branch_name)
{
	int pos = 0;

73
	for (pos = 0; findings[pos].branch_name; ++pos) {
74 75 76 77 78 79 80 81 82
		if (strcmp(expected_branch_name, findings[pos].branch_name) == 0) {
			cl_assert_equal_i(1, findings[pos].encounters);
			return;
		}
	}

	cl_fail("expected branch not found in list.");
}

83
static void contains_branches(struct expectations exp[], git_branch_iterator *iter)
84
{
85
	git_reference *ref;
86
	git_branch_t type;
87 88 89 90 91 92 93
	int error, pos = 0;

	while ((error = git_branch_next(&ref, &type, iter)) == 0) {
		for (pos = 0; exp[pos].branch_name; ++pos) {
			if (strcmp(git_reference_shorthand(ref), exp[pos].branch_name) == 0)
				exp[pos].encounters++;
		}
94

95
		git_reference_free(ref);
96 97
	}

98
	cl_assert_equal_i(error, GIT_ITEROVER);
99 100 101 102 103 104 105
}

/*
 * $ git branch -r
 *  nulltoken/HEAD -> nulltoken/master
 *  nulltoken/master
 */
106
void test_refs_branches_iterator__retrieve_remote_symbolic_HEAD_when_present(void)
107
{
108
	git_branch_iterator *iter;
109 110 111 112 113 114 115
	struct expectations exp[] = {
		{ "nulltoken/HEAD", 0 },
		{ "nulltoken/master", 0 },
		{ NULL, 0 }
	};

	git_reference_free(fake_remote);
116
	cl_git_pass(git_reference_symbolic_create(&fake_remote, repo, "refs/remotes/nulltoken/HEAD", "refs/remotes/nulltoken/master", 0, NULL, NULL));
117 118 119

	assert_retrieval(GIT_BRANCH_REMOTE, 3);

120 121 122
	cl_git_pass(git_branch_iterator_new(&iter, repo, GIT_BRANCH_REMOTE));
	contains_branches(exp, iter);
	git_branch_iterator_free(iter);
123 124

	assert_branch_has_been_found(exp, "nulltoken/HEAD");
125
	assert_branch_has_been_found(exp, "nulltoken/master");
126
}
127

128
void test_refs_branches_iterator__mix_of_packed_and_loose(void)
129
{
130
	git_branch_iterator *iter;
131 132 133 134 135 136 137 138 139 140 141
	struct expectations exp[] = {
		{ "master", 0 },
		{ "origin/HEAD", 0 },
		{ "origin/master", 0 },
		{ "origin/packed", 0 },
		{ NULL, 0 }
	};
	git_repository *r2;

	r2 = cl_git_sandbox_init("testrepo2");

142
	cl_git_pass(git_branch_iterator_new(&iter, r2, GIT_BRANCH_ALL));
143 144 145
	contains_branches(exp, iter);

	git_branch_iterator_free(iter);
146 147 148 149 150 151

	assert_branch_has_been_found(exp, "master");
	assert_branch_has_been_found(exp, "origin/HEAD");
	assert_branch_has_been_found(exp, "origin/master");
	assert_branch_has_been_found(exp, "origin/packed");
}