read.c 4.83 KB
Newer Older
Ben Straub committed
1 2 3 4 5 6 7 8 9
#include "clar_libgit2.h"

#include "tag.h"

static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
static const char *bad_tag_id = "eda9f45a2a98d4c17a09d681d88569fa4ea91755";
static const char *badly_tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
10 11
static const char *short_tag_id = "5da7760512a953e3c7c4e47e4392c7a4338fb729";
static const char *short_tagged_commit = "4a5ed60bafcf4638b7c8356bd4ce1916bfede93c";
12
static const char *taggerless = "4a23e2e65ad4e31c4c9db7dc746650bfad082679";
Ben Straub committed
13 14 15

static git_repository *g_repo;

16
/* Fixture setup and teardown */
17
void test_object_tag_read__initialize(void)
Ben Straub committed
18
{
nulltoken committed
19
	g_repo = cl_git_sandbox_init("testrepo");
Ben Straub committed
20 21
}

22
void test_object_tag_read__cleanup(void)
Ben Straub committed
23
{
nulltoken committed
24
	cl_git_sandbox_cleanup();
Ben Straub committed
25 26 27
}


28
void test_object_tag_read__parse(void)
Ben Straub committed
29
{
30
	/* read and parse a tag from the repository */
nulltoken committed
31 32 33
	git_tag *tag1, *tag2;
	git_commit *commit;
	git_oid id1, id2, id_commit;
Ben Straub committed
34

nulltoken committed
35 36 37
	git_oid_fromstr(&id1, tag1_id);
	git_oid_fromstr(&id2, tag2_id);
	git_oid_fromstr(&id_commit, tagged_commit);
Ben Straub committed
38

nulltoken committed
39
	cl_git_pass(git_tag_lookup(&tag1, g_repo, &id1));
Ben Straub committed
40

nulltoken committed
41
	cl_assert_equal_s(git_tag_name(tag1), "test");
42
	cl_assert(git_tag_target_type(tag1) == GIT_OBJECT_TAG);
Ben Straub committed
43

nulltoken committed
44 45
	cl_git_pass(git_tag_target((git_object **)&tag2, tag1));
	cl_assert(tag2 != NULL);
Ben Straub committed
46

nulltoken committed
47
	cl_assert(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
Ben Straub committed
48

nulltoken committed
49 50
	cl_git_pass(git_tag_target((git_object **)&commit, tag2));
	cl_assert(commit != NULL);
Ben Straub committed
51

nulltoken committed
52
	cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
Ben Straub committed
53

nulltoken committed
54 55 56
	git_tag_free(tag1);
	git_tag_free(tag2);
	git_commit_free(commit);
Ben Straub committed
57 58
}

59
void test_object_tag_read__parse_without_tagger(void)
Ben Straub committed
60
{
61
	/* read and parse a tag without a tagger field */
nulltoken committed
62 63 64 65
	git_repository *bad_tag_repo;
	git_tag *bad_tag;
	git_commit *commit;
	git_oid id, id_commit;
Ben Straub committed
66

67
	/* TODO: This is a little messy */
nulltoken committed
68
	cl_git_pass(git_repository_open(&bad_tag_repo, cl_fixture("bad_tag.git")));
Ben Straub committed
69

nulltoken committed
70 71
	git_oid_fromstr(&id, bad_tag_id);
	git_oid_fromstr(&id_commit, badly_tagged_commit);
Ben Straub committed
72

nulltoken committed
73 74
	cl_git_pass(git_tag_lookup(&bad_tag, bad_tag_repo, &id));
	cl_assert(bad_tag != NULL);
Ben Straub committed
75

nulltoken committed
76 77 78
	cl_assert_equal_s(git_tag_name(bad_tag), "e90810b");
	cl_assert(git_oid_cmp(&id, git_tag_id(bad_tag)) == 0);
	cl_assert(bad_tag->tagger == NULL);
Ben Straub committed
79

nulltoken committed
80 81
	cl_git_pass(git_tag_target((git_object **)&commit, bad_tag));
	cl_assert(commit != NULL);
Ben Straub committed
82

nulltoken committed
83
	cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
Ben Straub committed
84

85

nulltoken committed
86 87 88
	git_tag_free(bad_tag);
	git_commit_free(commit);
	git_repository_free(bad_tag_repo);
Ben Straub committed
89
}
90 91 92

void test_object_tag_read__parse_without_message(void)
{
93
	/* read and parse a tag without a message field */
nulltoken committed
94 95 96 97
	git_repository *short_tag_repo;
	git_tag *short_tag;
	git_commit *commit;
	git_oid id, id_commit;
98

99
	/* TODO: This is a little messy */
nulltoken committed
100
	cl_git_pass(git_repository_open(&short_tag_repo, cl_fixture("short_tag.git")));
101

nulltoken committed
102 103
	git_oid_fromstr(&id, short_tag_id);
	git_oid_fromstr(&id_commit, short_tagged_commit);
104

nulltoken committed
105 106
	cl_git_pass(git_tag_lookup(&short_tag, short_tag_repo, &id));
	cl_assert(short_tag != NULL);
107

nulltoken committed
108 109 110
	cl_assert_equal_s(git_tag_name(short_tag), "no_description");
	cl_assert(git_oid_cmp(&id, git_tag_id(short_tag)) == 0);
	cl_assert(short_tag->message == NULL);
111

nulltoken committed
112 113
	cl_git_pass(git_tag_target((git_object **)&commit, short_tag));
	cl_assert(commit != NULL);
114

nulltoken committed
115
	cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
116

nulltoken committed
117 118 119
	git_tag_free(short_tag);
	git_commit_free(commit);
	git_repository_free(short_tag_repo);
120
}
121 122 123 124 125 126 127 128 129 130 131 132 133 134

void test_object_tag_read__without_tagger_nor_message(void)
{
	git_tag *tag;
	git_oid id;
	git_repository *repo;

	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));

	cl_git_pass(git_oid_fromstr(&id, taggerless));

	cl_git_pass(git_tag_lookup(&tag, repo, &id));

	cl_assert_equal_s(git_tag_name(tag), "taggerless");
135
	cl_assert(git_tag_target_type(tag) == GIT_OBJECT_COMMIT);
136 137 138 139 140 141 142

	cl_assert(tag->message == NULL);
	cl_assert(tag->tagger == NULL);

	git_tag_free(tag);
	git_repository_free(repo);
}
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

static const char *silly_tag = "object c054ccaefbf2da31c3b19178f9e3ef20a3867924\n\
type commit\n\
tag v1_0_1\n\
tagger Jamis Buck <jamis@37signals.com> 1107717917\n\
diff --git a/lib/sqlite3/version.rb b/lib/sqlite3/version.rb\n\
index 0b3bf69..4ee8fc2 100644\n\
--- a/lib/sqlite3/version.rb\n\
+++ b/lib/sqlite3/version.rb\n\
@@ -36,7 +36,7 @@ module SQLite3\n\
 \n\
     MAJOR = 1\n\
     MINOR = 0\n\
-    TINY  = 0\n\
+    TINY  = 1\n\
 \n\
     STRING = [ MAJOR, MINOR, TINY ].join( \".\" )\n\
 \n\
 -0600\n\
\n\
v1_0_1 release\n";

void test_object_tag_read__extra_header_fields(void)
{
	git_tag *tag;
	git_odb *odb;
	git_oid id;

	cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));

173
	cl_git_pass(git_odb_write(&id, odb, silly_tag, strlen(silly_tag), GIT_OBJECT_TAG));
174 175 176 177 178 179
	cl_git_pass(git_tag_lookup(&tag, g_repo, &id));

	cl_assert_equal_s("v1_0_1 release\n", git_tag_message(tag));

	git_tag_free(tag);
}