clay: add tests for tree diff'ing Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk> Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
diff --git a/tests-clay/clay.h b/tests-clay/clay.h
index b3aae46..7210e79 100644
--- a/tests-clay/clay.h
+++ b/tests-clay/clay.h
@@ -158,6 +158,12 @@ extern void test_object_raw_size__validate_oid_size(void);
extern void test_object_raw_type2string__check_type_is_loose(void);
extern void test_object_raw_type2string__convert_string_to_type(void);
extern void test_object_raw_type2string__convert_type_to_string(void);
+extern void test_object_tree_diff__addition(void);
+extern void test_object_tree_diff__cleanup(void);
+extern void test_object_tree_diff__deletion(void);
+extern void test_object_tree_diff__initialize(void);
+extern void test_object_tree_diff__modification(void);
+extern void test_object_tree_diff__more(void);
extern void test_object_tree_frompath__cleanup(void);
extern void test_object_tree_frompath__fail_when_processing_an_invalid_path(void);
extern void test_object_tree_frompath__fail_when_processing_an_unknown_tree_segment(void);
diff --git a/tests-clay/clay_main.c b/tests-clay/clay_main.c
index 409ce76..ee08842 100644
--- a/tests-clay/clay_main.c
+++ b/tests-clay/clay_main.c
@@ -245,6 +245,12 @@ static const struct clay_func _clay_cb_object_raw_type2string[] = {
{"convert_string_to_type", &test_object_raw_type2string__convert_string_to_type},
{"convert_type_to_string", &test_object_raw_type2string__convert_type_to_string}
};
+static const struct clay_func _clay_cb_object_tree_diff[] = {
+ {"addition", &test_object_tree_diff__addition},
+ {"deletion", &test_object_tree_diff__deletion},
+ {"modification", &test_object_tree_diff__modification},
+ {"more", &test_object_tree_diff__more}
+};
static const struct clay_func _clay_cb_object_tree_frompath[] = {
{"fail_when_processing_an_invalid_path", &test_object_tree_frompath__fail_when_processing_an_invalid_path},
{"fail_when_processing_an_unknown_tree_segment", &test_object_tree_frompath__fail_when_processing_an_unknown_tree_segment},
@@ -438,6 +444,12 @@ static const struct clay_suite _clay_suites[] = {
_clay_cb_object_raw_type2string, 3
},
{
+ "object::tree::diff",
+ {"initialize", &test_object_tree_diff__initialize},
+ {"cleanup", &test_object_tree_diff__cleanup},
+ _clay_cb_object_tree_diff, 4
+ },
+ {
"object::tree::frompath",
{"initialize", &test_object_tree_frompath__initialize},
{"cleanup", &test_object_tree_frompath__cleanup},
@@ -493,8 +505,8 @@ static const struct clay_suite _clay_suites[] = {
}
};
-static size_t _clay_suite_count = 34;
-static size_t _clay_callback_count = 113;
+static size_t _clay_suite_count = 35;
+static size_t _clay_callback_count = 117;
/* Core test functions */
static void
diff --git a/tests-clay/object/tree/diff.c b/tests-clay/object/tree/diff.c
new file mode 100644
index 0000000..471e465
--- /dev/null
+++ b/tests-clay/object/tree/diff.c
@@ -0,0 +1,150 @@
+#include "clay_libgit2.h"
+#include "tree.h"
+#include "repository.h"
+
+static unsigned int expect_idx;
+static git_repository *repo;
+static git_tree *atree, *btree;
+static git_oid aoid, boid;
+
+static void diff_cmp(const git_tree_diff_data *a, const git_tree_diff_data *b)
+{
+ cl_assert(a->old_attr - b->old_attr == 0);
+
+ cl_assert(a->new_attr - b->new_attr == 0);
+
+ cl_assert(git_oid_cmp(&a->old_oid, &b->old_oid) == 0);
+ cl_assert(git_oid_cmp(&a->new_oid, &b->new_oid) == 0);
+
+ cl_assert(a->status - b->status == 0);
+
+ cl_assert(strcmp(a->path, b->path) == 0);
+}
+
+static int diff_cb(const git_tree_diff_data *diff, void *data)
+{
+ diff_cmp(diff, data);
+ return GIT_SUCCESS;
+}
+
+void test_object_tree_diff__initialize(void)
+{
+ cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
+}
+
+void test_object_tree_diff__cleanup(void)
+{
+ git_tree_free(atree);
+ git_tree_free(btree);
+ git_repository_free(repo);
+}
+
+void test_object_tree_diff__addition(void)
+{
+ char *astr = "181037049a54a1eb5fab404658a3a250b44335d7";
+ char *bstr = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
+ git_tree_diff_data expect;
+
+ memset(&expect, 0x0, sizeof(git_tree_diff_data));
+ expect.old_attr = 0;
+ expect.new_attr = 0100644;
+ git_oid_fromstr(&expect.new_oid, "fa49b077972391ad58037050f2a75f74e3671e92");
+ expect.status = GIT_STATUS_ADDED;
+ expect.path = "new.txt";
+
+ cl_must_pass(git_oid_fromstr(&aoid, astr));
+ cl_must_pass(git_oid_fromstr(&boid, bstr));
+
+ cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
+ cl_must_pass(git_tree_lookup(&btree, repo, &boid));
+
+ cl_must_pass(git_tree_diff(atree, btree, diff_cb, &expect));
+}
+
+void test_object_tree_diff__deletion(void)
+{
+ char *astr = "f60079018b664e4e79329a7ef9559c8d9e0378d1";
+ char *bstr = "181037049a54a1eb5fab404658a3a250b44335d7";
+ git_tree_diff_data expect;
+
+ memset(&expect, 0x0, sizeof(git_tree_diff_data));
+ expect.old_attr = 0100644;
+ expect.new_attr = 0;
+ git_oid_fromstr(&expect.old_oid, "fa49b077972391ad58037050f2a75f74e3671e92");
+ expect.status = GIT_STATUS_DELETED;
+ expect.path = "new.txt";
+ cl_must_pass(git_oid_fromstr(&aoid, astr));
+ cl_must_pass(git_oid_fromstr(&boid, bstr));
+
+ cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
+ cl_must_pass(git_tree_lookup(&btree, repo, &boid));
+
+ cl_must_pass(git_tree_diff(atree, btree, diff_cb, &expect));
+}
+
+void test_object_tree_diff__modification(void)
+{
+ char *astr = "1810dff58d8a660512d4832e740f692884338ccd";
+ char *bstr = "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162";
+ git_tree_diff_data expect;
+
+ expect.old_attr = 0100644;
+ expect.new_attr = 0100644;
+ git_oid_fromstr(&expect.old_oid, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
+ git_oid_fromstr(&expect.new_oid, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
+ expect.status = GIT_STATUS_MODIFIED;
+ expect.path = "branch_file.txt";
+
+ cl_must_pass(git_oid_fromstr(&aoid, astr));
+ cl_must_pass(git_oid_fromstr(&boid, bstr));
+
+ cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
+ cl_must_pass(git_tree_lookup(&btree, repo, &boid));
+
+ cl_must_pass(git_tree_diff(atree, btree, diff_cb, &expect));
+}
+
+static int diff_more_cb(const git_tree_diff_data *diff, void *data)
+{
+ git_tree_diff_data *expect = (git_tree_diff_data *) data;
+ diff_cmp(diff, &expect[expect_idx++]);
+
+ return GIT_SUCCESS;
+}
+
+void test_object_tree_diff__more(void)
+{
+ char *astr = "814889a078c031f61ed08ab5fa863aea9314344d";
+ char *bstr = "75057dd4114e74cca1d750d0aee1647c903cb60a";
+ git_tree_diff_data expect[3];
+
+ memset(expect, 0x0, 3 * sizeof(git_tree_diff_data));
+ /* M README */
+ expect[0].old_attr = 0100644;
+ expect[0].new_attr = 0100644;
+ git_oid_fromstr(&expect[0].old_oid, "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
+ git_oid_fromstr(&expect[0].new_oid, "1385f264afb75a56a5bec74243be9b367ba4ca08");
+ expect[0].status = GIT_STATUS_MODIFIED;
+ expect[0].path = "README";
+ /* A branch_file.txt */
+ expect[1].old_attr = 0;
+ expect[1].new_attr = 0100644;
+ git_oid_fromstr(&expect[1].new_oid, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
+ expect[1].status = GIT_STATUS_ADDED;
+ expect[1].path = "branch_file.txt";
+ /* M new.txt */
+ expect[2].old_attr = 0100644;
+ expect[2].new_attr = 0100644;
+ git_oid_fromstr(&expect[2].old_oid, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
+ git_oid_fromstr(&expect[2].new_oid, "fa49b077972391ad58037050f2a75f74e3671e92");
+ expect[2].status = GIT_STATUS_MODIFIED;
+ expect[2].path = "new.txt";
+
+ cl_must_pass(git_oid_fromstr(&aoid, astr));
+ cl_must_pass(git_oid_fromstr(&boid, bstr));
+
+ cl_must_pass(git_tree_lookup(&atree, repo, &aoid));
+ cl_must_pass(git_tree_lookup(&btree, repo, &boid));
+
+ cl_must_pass(git_tree_diff(atree, btree, diff_more_cb, expect));
+}