Blame tests/object/tag/list.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
Packit Service 20376f
#include "tag.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *g_repo;
Packit Service 20376f
Packit Service 20376f
#define MAX_USED_TAGS 6
Packit Service 20376f
Packit Service 20376f
struct pattern_match_t
Packit Service 20376f
{
Packit Service 20376f
	const char* pattern;
Packit Service 20376f
	const size_t expected_matches;
Packit Service 20376f
	const char* expected_results[MAX_USED_TAGS];
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
// Helpers
Packit Service 20376f
static void ensure_tag_pattern_match(git_repository *repo,
Packit Service 20376f
									 const struct pattern_match_t* data)
Packit Service 20376f
{
Packit Service 20376f
	int already_found[MAX_USED_TAGS] = { 0 };
Packit Service 20376f
	git_strarray tag_list;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	size_t sucessfully_found = 0;
Packit Service 20376f
	size_t i, j;
Packit Service 20376f
Packit Service 20376f
	cl_assert(data->expected_matches <= MAX_USED_TAGS);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_tag_list_match(&tag_list, data->pattern, repo)) < 0)
Packit Service 20376f
		goto exit;
Packit Service 20376f
Packit Service 20376f
	if (tag_list.count != data->expected_matches)
Packit Service 20376f
	{
Packit Service 20376f
		error = GIT_ERROR;
Packit Service 20376f
		goto exit;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	// we have to be prepared that tags come in any order.
Packit Service 20376f
	for (i = 0; i < tag_list.count; i++)
Packit Service 20376f
	{
Packit Service 20376f
		for (j = 0; j < data->expected_matches; j++)
Packit Service 20376f
		{
Packit Service 20376f
			if (!already_found[j] && !strcmp(data->expected_results[j], tag_list.strings[i]))
Packit Service 20376f
			{
Packit Service 20376f
				already_found[j] = 1;
Packit Service 20376f
				sucessfully_found++;
Packit Service 20376f
				break;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
	cl_assert_equal_i((int)sucessfully_found, (int)data->expected_matches);
Packit Service 20376f
Packit Service 20376f
exit:
Packit Service 20376f
	git_strarray_free(&tag_list);
Packit Service 20376f
	cl_git_pass(error);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
// Fixture setup and teardown
Packit Service 20376f
void test_object_tag_list__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	g_repo = cl_git_sandbox_init("testrepo");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_tag_list__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_tag_list__list_all(void)
Packit Service 20376f
{
Packit Service 20376f
	// list all tag names from the repository
Packit Service 20376f
	git_strarray tag_list;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_tag_list(&tag_list, g_repo));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i((int)tag_list.count, 6);
Packit Service 20376f
Packit Service 20376f
	git_strarray_free(&tag_list);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static const struct pattern_match_t matches[] = {
Packit Service 20376f
	// All tags, including a packed one and two namespaced ones.
Packit Service 20376f
	{ "", 6, { "e90810b", "point_to_blob", "test", "packed-tag", "foo/bar", "foo/foo/bar" } },
Packit Service 20376f
Packit Service 20376f
	// beginning with
Packit Service 20376f
	{ "t*", 1, { "test" } },
Packit Service 20376f
Packit Service 20376f
	// ending with
Packit Service 20376f
	{ "*b", 2, { "e90810b", "point_to_blob" } },
Packit Service 20376f
Packit Service 20376f
	// exact match
Packit Service 20376f
	{ "e", 0 },
Packit Service 20376f
	{ "e90810b", 1, { "e90810b" } },
Packit Service 20376f
Packit Service 20376f
	// either or
Packit Service 20376f
	{ "e90810[ab]", 1, { "e90810b" } },
Packit Service 20376f
Packit Service 20376f
	// glob in the middle
Packit Service 20376f
	{ "foo/*/bar", 1, { "foo/foo/bar" } },
Packit Service 20376f
Packit Service 20376f
	// The matching of '*' is based on plain string matching analog to the regular expression ".*"
Packit Service 20376f
	// => a '/' in the tag name has no special meaning.
Packit Service 20376f
	// Compare to `git tag -l "*bar"`
Packit Service 20376f
	{ "*bar", 2, { "foo/bar", "foo/foo/bar" } },
Packit Service 20376f
Packit Service 20376f
	// End of list
Packit Service 20376f
	{ NULL }
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
void test_object_tag_list__list_by_pattern(void)
Packit Service 20376f
{
Packit Service 20376f
	// list all tag names from the repository matching a specified pattern
Packit Service 20376f
	size_t i = 0;
Packit Service 20376f
	while (matches[i].pattern)
Packit Service 20376f
		ensure_tag_pattern_match(g_repo, &matches[i++]);
Packit Service 20376f
}