]> git.proxmox.com Git - libgit2.git/blob - tests/object/tag/list.c
8a1a2d26d2df36e66d0c63c55c3e0199eb77b12f
[libgit2.git] / tests / object / tag / list.c
1 #include "clar_libgit2.h"
2
3 #include "tag.h"
4
5 static git_repository *g_repo;
6
7 #define MAX_USED_TAGS 6
8
9 struct pattern_match_t
10 {
11 const char* pattern;
12 const size_t expected_matches;
13 const char* expected_results[MAX_USED_TAGS];
14 };
15
16 /* Helpers */
17 static void ensure_tag_pattern_match(git_repository *repo,
18 const struct pattern_match_t* data)
19 {
20 int already_found[MAX_USED_TAGS] = { 0 };
21 git_strarray tag_list;
22 int error = 0;
23 size_t sucessfully_found = 0;
24 size_t i, j;
25
26 cl_assert(data->expected_matches <= MAX_USED_TAGS);
27
28 if ((error = git_tag_list_match(&tag_list, data->pattern, repo)) < 0)
29 goto exit;
30
31 if (tag_list.count != data->expected_matches)
32 {
33 error = GIT_ERROR;
34 goto exit;
35 }
36
37 /* we have to be prepared that tags come in any order. */
38 for (i = 0; i < tag_list.count; i++)
39 {
40 for (j = 0; j < data->expected_matches; j++)
41 {
42 if (!already_found[j] && !strcmp(data->expected_results[j], tag_list.strings[i]))
43 {
44 already_found[j] = 1;
45 sucessfully_found++;
46 break;
47 }
48 }
49 }
50 cl_assert_equal_i((int)sucessfully_found, (int)data->expected_matches);
51
52 exit:
53 git_strarray_dispose(&tag_list);
54 cl_git_pass(error);
55 }
56
57 /* Fixture setup and teardown */
58 void test_object_tag_list__initialize(void)
59 {
60 g_repo = cl_git_sandbox_init("testrepo");
61 }
62
63 void test_object_tag_list__cleanup(void)
64 {
65 cl_git_sandbox_cleanup();
66 }
67
68 void test_object_tag_list__list_all(void)
69 {
70 /* list all tag names from the repository */
71 git_strarray tag_list;
72
73 cl_git_pass(git_tag_list(&tag_list, g_repo));
74
75 cl_assert_equal_i((int)tag_list.count, 6);
76
77 git_strarray_dispose(&tag_list);
78 }
79
80 static const struct pattern_match_t matches[] = {
81 /* All tags, including a packed one and two namespaced ones. */
82 { "", 6, { "e90810b", "point_to_blob", "test", "packed-tag", "foo/bar", "foo/foo/bar" } },
83
84 /* beginning with */
85 { "t*", 1, { "test" } },
86
87 /* ending with */
88 { "*b", 2, { "e90810b", "point_to_blob" } },
89
90 /* exact match */
91 { "e", 0 },
92 { "e90810b", 1, { "e90810b" } },
93
94 /* either or */
95 { "e90810[ab]", 1, { "e90810b" } },
96
97 /* glob in the middle */
98 { "foo/*/bar", 1, { "foo/foo/bar" } },
99
100 /*
101 * The matching of '*' is based on plain string matching analog to the regular expression ".*"
102 * => a '/' in the tag name has no special meaning.
103 * Compare to `git tag -l "*bar"`
104 */
105 { "*bar", 2, { "foo/bar", "foo/foo/bar" } },
106
107 /* End of list */
108 { NULL }
109 };
110
111 void test_object_tag_list__list_by_pattern(void)
112 {
113 /* list all tag names from the repository matching a specified pattern */
114 size_t i = 0;
115 while (matches[i].pattern)
116 ensure_tag_pattern_match(g_repo, &matches[i++]);
117 }