]> git.proxmox.com Git - libgit2.git/blob - tests-clar/object/tag/read.c
Simple readability fixes.
[libgit2.git] / tests-clar / object / tag / read.c
1 #include "clar_libgit2.h"
2
3 #include "tag.h"
4
5 static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
6 static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
7 static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
8 static const char *bad_tag_id = "eda9f45a2a98d4c17a09d681d88569fa4ea91755";
9 static const char *badly_tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
10
11 static git_repository *g_repo;
12
13
14 // Helpers
15 static void ensure_tag_pattern_match(git_repository *repo,
16 const char *pattern,
17 const size_t expected_matches)
18 {
19 git_strarray tag_list;
20 int error = GIT_SUCCESS;
21
22 if ((error = git_tag_list_match(&tag_list, pattern, repo)) < GIT_SUCCESS)
23 goto exit;
24
25 if (tag_list.count != expected_matches)
26 error = GIT_ERROR;
27
28 exit:
29 git_strarray_free(&tag_list);
30 cl_git_pass(error);
31 }
32
33
34 // Fixture setup and teardown
35 void test_object_tag_read__initialize(void)
36 {
37 g_repo = cl_git_sandbox_init("testrepo");
38 }
39
40 void test_object_tag_read__cleanup(void)
41 {
42 cl_git_sandbox_cleanup();
43 }
44
45
46 void test_object_tag_read__parse(void)
47 {
48 // read and parse a tag from the repository
49 git_tag *tag1, *tag2;
50 git_commit *commit;
51 git_oid id1, id2, id_commit;
52
53 git_oid_fromstr(&id1, tag1_id);
54 git_oid_fromstr(&id2, tag2_id);
55 git_oid_fromstr(&id_commit, tagged_commit);
56
57 cl_git_pass(git_tag_lookup(&tag1, g_repo, &id1));
58
59 cl_assert_strequal(git_tag_name(tag1), "test");
60 cl_assert(git_tag_type(tag1) == GIT_OBJ_TAG);
61
62 cl_git_pass(git_tag_target((git_object **)&tag2, tag1));
63 cl_assert(tag2 != NULL);
64
65 cl_assert(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
66
67 cl_git_pass(git_tag_target((git_object **)&commit, tag2));
68 cl_assert(commit != NULL);
69
70 cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
71
72 git_tag_free(tag1);
73 git_tag_free(tag2);
74 git_commit_free(commit);
75 }
76
77 void test_object_tag_read__list(void)
78 {
79 // list all tag names from the repository
80 git_strarray tag_list;
81
82 cl_git_pass(git_tag_list(&tag_list, g_repo));
83
84 cl_assert(tag_list.count == 3);
85
86 git_strarray_free(&tag_list);
87 }
88
89 void test_object_tag_read__list_pattern(void)
90 {
91 // list all tag names from the repository matching a specified pattern
92 ensure_tag_pattern_match(g_repo, "", 3);
93 ensure_tag_pattern_match(g_repo, "*", 3);
94 ensure_tag_pattern_match(g_repo, "t*", 1);
95 ensure_tag_pattern_match(g_repo, "*b", 2);
96 ensure_tag_pattern_match(g_repo, "e", 0);
97 ensure_tag_pattern_match(g_repo, "e90810b", 1);
98 ensure_tag_pattern_match(g_repo, "e90810[ab]", 1);
99 }
100
101 void test_object_tag_read__parse_without_tagger(void)
102 {
103 // read and parse a tag without a tagger field
104 git_repository *bad_tag_repo;
105 git_tag *bad_tag;
106 git_commit *commit;
107 git_oid id, id_commit;
108
109 // TODO: This is a little messy
110 cl_git_pass(git_repository_open(&bad_tag_repo, cl_fixture("bad_tag.git")));
111
112 git_oid_fromstr(&id, bad_tag_id);
113 git_oid_fromstr(&id_commit, badly_tagged_commit);
114
115 cl_git_pass(git_tag_lookup(&bad_tag, bad_tag_repo, &id));
116 cl_assert(bad_tag != NULL);
117
118 cl_assert_strequal(git_tag_name(bad_tag), "e90810b");
119 cl_assert(git_oid_cmp(&id, git_tag_id(bad_tag)) == 0);
120 cl_assert(bad_tag->tagger == NULL);
121
122 cl_git_pass(git_tag_target((git_object **)&commit, bad_tag));
123 cl_assert(commit != NULL);
124
125 cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
126
127 git_tag_free(bad_tag);
128 git_commit_free(commit);
129 git_repository_free(bad_tag_repo);
130 }