]> git.proxmox.com Git - libgit2.git/blob - tests-clar/object/tag/write.c
Update test suite
[libgit2.git] / tests-clar / object / tag / write.c
1 #include "clar_libgit2.h"
2
3 static const char* tagger_name = "Vicent Marti";
4 static const char* tagger_email = "vicent@github.com";
5 static const char* tagger_message = "This is my tag.\n\nThere are many tags, but this one is mine\n";
6
7 static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
8 static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
9
10 static git_repository *g_repo;
11
12 // Fixture setup and teardown
13 void test_object_tag_write__initialize(void)
14 {
15 g_repo = cl_git_sandbox_init("testrepo");
16 }
17
18 void test_object_tag_write__cleanup(void)
19 {
20 cl_git_sandbox_cleanup();
21 }
22
23 void test_object_tag_write__basic(void)
24 {
25 // write a tag to the repository and read it again
26 git_tag *tag;
27 git_oid target_id, tag_id;
28 git_signature *tagger;
29 const git_signature *tagger1;
30 git_reference *ref_tag;
31 git_object *target;
32
33 git_oid_fromstr(&target_id, tagged_commit);
34 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
35
36 /* create signature */
37 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
38
39 cl_git_pass(
40 git_tag_create(&tag_id, g_repo,
41 "the-tag", target, tagger, tagger_message, 0)
42 );
43
44 git_object_free(target);
45 git_signature_free(tagger);
46
47 cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
48 cl_assert(git_oid_cmp(git_tag_target_oid(tag), &target_id) == 0);
49
50 /* Check attributes were set correctly */
51 tagger1 = git_tag_tagger(tag);
52 cl_assert(tagger1 != NULL);
53 cl_assert_equal_s(tagger1->name, tagger_name);
54 cl_assert_equal_s(tagger1->email, tagger_email);
55 cl_assert(tagger1->when.time == 123456789);
56 cl_assert(tagger1->when.offset == 60);
57
58 cl_assert_equal_s(git_tag_message(tag), tagger_message);
59
60 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag"));
61 cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0);
62 cl_git_pass(git_reference_delete(ref_tag));
63
64 git_tag_free(tag);
65 }
66
67 void test_object_tag_write__overwrite(void)
68 {
69 // Attempt to write a tag bearing the same name than an already existing tag
70 git_oid target_id, tag_id;
71 git_signature *tagger;
72 git_object *target;
73
74 git_oid_fromstr(&target_id, tagged_commit);
75 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
76
77 /* create signature */
78 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
79
80 cl_git_fail(git_tag_create(
81 &tag_id, /* out id */
82 g_repo,
83 "e90810b",
84 target,
85 tagger,
86 tagger_message,
87 0));
88
89 git_object_free(target);
90 git_signature_free(tagger);
91
92 }
93
94 void test_object_tag_write__replace(void)
95 {
96 // Replace an already existing tag
97 git_oid target_id, tag_id, old_tag_id;
98 git_signature *tagger;
99 git_reference *ref_tag;
100 git_object *target;
101
102 git_oid_fromstr(&target_id, tagged_commit);
103 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
104
105 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
106 git_oid_cpy(&old_tag_id, git_reference_oid(ref_tag));
107 git_reference_free(ref_tag);
108
109 /* create signature */
110 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
111
112 cl_git_pass(git_tag_create(
113 &tag_id, /* out id */
114 g_repo,
115 "e90810b",
116 target,
117 tagger,
118 tagger_message,
119 1));
120
121 git_object_free(target);
122 git_signature_free(tagger);
123
124 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
125 cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0);
126 cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &old_tag_id) != 0);
127
128 git_reference_free(ref_tag);
129 }
130
131 void test_object_tag_write__lightweight(void)
132 {
133 // write a lightweight tag to the repository and read it again
134 git_oid target_id, object_id;
135 git_reference *ref_tag;
136 git_object *target;
137
138 git_oid_fromstr(&target_id, tagged_commit);
139 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
140
141 cl_git_pass(git_tag_create_lightweight(
142 &object_id,
143 g_repo,
144 "light-tag",
145 target,
146 0));
147
148 git_object_free(target);
149
150 cl_assert(git_oid_cmp(&object_id, &target_id) == 0);
151
152 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/light-tag"));
153 cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &target_id) == 0);
154
155 cl_git_pass(git_tag_delete(g_repo, "light-tag"));
156
157 git_reference_free(ref_tag);
158 }
159
160 void test_object_tag_write__lightweight_over_existing(void)
161 {
162 // Attempt to write a lightweight tag bearing the same name than an already existing tag
163 git_oid target_id, object_id, existing_object_id;
164 git_object *target;
165
166 git_oid_fromstr(&target_id, tagged_commit);
167 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT));
168
169 cl_git_fail(git_tag_create_lightweight(
170 &object_id,
171 g_repo,
172 "e90810b",
173 target,
174 0));
175
176 git_oid_fromstr(&existing_object_id, tag2_id);
177 cl_assert(git_oid_cmp(&object_id, &existing_object_id) == 0);
178
179 git_object_free(target);
180 }
181
182 void test_object_tag_write__delete(void)
183 {
184 // Delete an already existing tag
185 git_reference *ref_tag;
186
187 cl_git_pass(git_tag_delete(g_repo, "e90810b"));
188
189 cl_git_fail(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
190
191 git_reference_free(ref_tag);
192 }