Add unit tests for object write-back Basic write-back & in-memory editing for objects is now tested in t0403 (commits), t0802 (tags) and t0902 (trees). Add new helper functions in test_helpers.c to remove the loose objects created when doing write-back tests. 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
diff --git a/tests/t0403-write.c b/tests/t0403-write.c
new file mode 100644
index 0000000..b1d088d
--- /dev/null
+++ b/tests/t0403-write.c
@@ -0,0 +1,127 @@
+#include "test_lib.h"
+#include "test_helpers.h"
+#include "commit.h"
+
+#include <git/odb.h>
+#include <git/commit.h>
+#include <git/revwalk.h>
+
+static const char *odb_dir = "../resources/sample-odb";
+static const char *commit_ids[] = {
+ "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
+ "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
+ "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
+ "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
+ "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
+ "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
+};
+static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
+
+BEGIN_TEST(writenew_test)
+ git_odb *db;
+ git_repository *repo;
+ git_commit *commit, *parent;
+ git_tree *tree;
+ git_oid id;
+ /* char hex_oid[41]; */
+
+ git_person author = {"Vicent Marti", "vicent@github.com", 123456789};
+ git_person committer = {"Vicent Marti", "vicent@github.com", 987654321};
+
+ must_pass(git_odb_open(&db, odb_dir));
+
+ repo = git_repository_alloc(db);
+ must_be_true(repo != NULL);
+
+ /* Create commit in memory */
+ commit = git_commit_new(repo);
+ must_be_true(commit != NULL);
+
+ /* Add new parent */
+ git_oid_mkstr(&id, commit_ids[4]);
+ parent = git_commit_lookup(repo, &id);
+ must_be_true(parent != NULL);
+
+ git_commit_add_parent(commit, parent);
+
+ /* Set other attributes */
+ git_commit_set_committer(commit, &committer);
+ git_commit_set_author(commit, &author);
+ git_commit_set_message(commit,
+ "This commit has been created in memory\n\
+This is a commit created in memory and it will be written back to disk\n");
+
+ /* add new tree */
+ git_oid_mkstr(&id, tree_oid);
+ tree = git_tree_lookup(repo, &id);
+ must_be_true(tree != NULL);
+
+ git_commit_set_tree(commit, tree);
+
+ /* Test it has no OID */
+ must_be_true(git_commit_id(commit) == NULL);
+
+ /* Write to disk */
+ must_pass(git_object_write((git_object *)commit));
+
+ /* Show new SHA1 */
+/*
+ git_oid_fmt(hex_oid, git_commit_id(commit));
+ hex_oid[40] = 0;
+ printf("Written new commit, SHA1: %s\n", hex_oid);
+*/
+
+ must_pass(remove_loose_object(odb_dir, (git_object *)commit));
+
+ git_repository_free(repo);
+ git_odb_close(db);
+
+END_TEST
+
+BEGIN_TEST(writeback_test)
+ git_odb *db;
+ git_repository *repo;
+ git_oid id;
+ git_commit *commit, *parent;
+ const char *message;
+ /* char hex_oid[41]; */
+
+ must_pass(git_odb_open(&db, odb_dir));
+
+ repo = git_repository_alloc(db);
+ must_be_true(repo != NULL);
+
+ git_oid_mkstr(&id, commit_ids[0]);
+
+ commit = git_commit_lookup(repo, &id);
+ must_be_true(commit != NULL);
+
+ message = git_commit_message(commit);
+
+/*
+ git_oid_fmt(hex_oid, git_commit_id(commit));
+ hex_oid[40] = 0;
+ printf("Old SHA1: %s\n", hex_oid);
+*/
+
+ git_commit_set_message(commit, "This is a new test message. Cool!\n");
+
+ git_oid_mkstr(&id, commit_ids[4]);
+ parent = git_commit_lookup(repo, &id);
+ must_be_true(parent != NULL);
+
+ git_commit_add_parent(commit, parent);
+
+ must_pass(git_object_write((git_object *)commit));
+
+/*
+ git_oid_fmt(hex_oid, git_commit_id(commit));
+ hex_oid[40] = 0;
+ printf("New SHA1: %s\n", hex_oid);
+*/
+
+ must_pass(remove_loose_object(odb_dir, (git_object *)commit));
+
+ git_repository_free(repo);
+ git_odb_close(db);
+END_TEST
diff --git a/tests/t0802-write.c b/tests/t0802-write.c
new file mode 100644
index 0000000..4fc2621
--- /dev/null
+++ b/tests/t0802-write.c
@@ -0,0 +1,43 @@
+#include "test_lib.h"
+#include "test_helpers.h"
+#include "commit.h"
+
+#include <git/odb.h>
+#include <git/tag.h>
+#include <git/revwalk.h>
+
+static const char *odb_dir = "../resources/pack-odb";
+static const char *tag_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
+
+BEGIN_TEST(tag_writeback_test)
+ git_odb *db;
+ git_oid id;
+ git_repository *repo;
+ git_tag *tag;
+ /* char hex_oid[41]; */
+
+ must_pass(git_odb_open(&db, odb_dir));
+
+ repo = git_repository_alloc(db);
+ must_be_true(repo != NULL);
+
+ git_oid_mkstr(&id, tag_id);
+
+ tag = git_tag_lookup(repo, &id);
+ must_be_true(tag != NULL);
+
+ git_tag_set_name(tag, "This is a different tag LOL");
+
+ must_pass(git_object_write((git_object *)tag));
+
+/*
+ git_oid_fmt(hex_oid, git_tag_id(tag));
+ hex_oid[40] = 0;
+ printf("TAG New SHA1: %s\n", hex_oid);
+*/
+
+ must_pass(remove_loose_object(odb_dir, (git_object *)tag));
+
+ git_repository_free(repo);
+ git_odb_close(db);
+END_TEST
diff --git a/tests/t0902-modify.c b/tests/t0902-modify.c
new file mode 100644
index 0000000..a321165
--- /dev/null
+++ b/tests/t0902-modify.c
@@ -0,0 +1,64 @@
+#include "test_lib.h"
+#include "test_helpers.h"
+#include "commit.h"
+
+#include <git/odb.h>
+#include <git/commit.h>
+#include <git/revwalk.h>
+
+static const char *odb_dir = "../resources/sample-odb";
+static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
+
+BEGIN_TEST(tree_add_entry_test)
+ git_odb *db;
+ git_oid id;
+ git_repository *repo;
+ git_tree *tree;
+ git_tree_entry *entry;
+ unsigned int i;
+ /* char hex_oid[41]; */
+
+ must_pass(git_odb_open(&db, odb_dir));
+
+ repo = git_repository_alloc(db);
+ must_be_true(repo != NULL);
+
+ git_oid_mkstr(&id, tree_oid);
+
+ tree = git_tree_lookup(repo, &id);
+ must_be_true(tree != NULL);
+
+ must_be_true(git_tree_entrycount(tree) == 3);
+
+ git_tree_add_entry(tree, &id, "zzz_test_entry.dat", 0);
+ git_tree_add_entry(tree, &id, "01_test_entry.txt", 0);
+
+ must_be_true(git_tree_entrycount(tree) == 5);
+
+ entry = git_tree_entry_byindex(tree, 0);
+ must_be_true(strcmp(git_tree_entry_name(entry), "01_test_entry.txt") == 0);
+
+ entry = git_tree_entry_byindex(tree, 4);
+ must_be_true(strcmp(git_tree_entry_name(entry), "zzz_test_entry.dat") == 0);
+
+ must_pass(git_tree_remove_entry_byname(tree, "README"));
+ must_be_true(git_tree_entrycount(tree) == 4);
+
+ for (i = 0; i < git_tree_entrycount(tree); ++i) {
+ entry = git_tree_entry_byindex(tree, i);
+ must_be_true(strcmp(git_tree_entry_name(entry), "README") != 0);
+ }
+
+ must_pass(git_object_write((git_object *)tree));
+
+/*
+ git_oid_fmt(hex_oid, git_tree_id(tree));
+ hex_oid[40] = 0;
+ printf("TREE New SHA1: %s\n", hex_oid);
+*/
+
+ must_pass(remove_loose_object(odb_dir, (git_object *)tree));
+
+ git_repository_free(repo);
+ git_odb_close(db);
+END_TEST
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index e7c2692..08434b8 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -26,6 +26,8 @@
#include "common.h"
#include "test_helpers.h"
#include "fileops.h"
+#include "git/oid.h"
+#include "git/repository.h"
int write_object_data(char *file, void *data, size_t len)
{
@@ -82,6 +84,40 @@ int remove_object_files(const char *odb_dir, object_data *d)
return 0;
}
+int remove_loose_object(const char *odb_dir, git_object *object)
+{
+ char *ptr, *full_path, *top_folder;
+ int path_length;
+
+ assert(odb_dir && object);
+
+ path_length = strlen(odb_dir);
+ ptr = full_path = git__malloc(path_length + GIT_OID_HEXSZ + 3);
+
+ strcpy(ptr, odb_dir);
+ ptr = top_folder = ptr + path_length;
+ *ptr++ = '/';
+ git_oid_pathfmt(ptr, git_object_id(object));
+ ptr += GIT_OID_HEXSZ + 1;
+ *ptr = 0;
+
+ if (gitfo_unlink(full_path) < 0) {
+ fprintf(stderr, "can't delete object file \"%s\"\n", full_path);
+ return -1;
+ }
+
+ *top_folder = 0;
+
+ if ((gitfo_rmdir(full_path) < 0) && (errno != ENOTEMPTY)) {
+ fprintf(stderr, "can't remove object directory \"%s\"\n", full_path);
+ return -1;
+ }
+
+ free(full_path);
+
+ return GIT_SUCCESS;
+}
+
int cmp_objects(git_rawobj *o, object_data *d)
{
if (o->type != git_obj_string_to_type(d->type))
diff --git a/tests/test_helpers.h b/tests/test_helpers.h
index 342a852..0d79d43 100644
--- a/tests/test_helpers.h
+++ b/tests/test_helpers.h
@@ -49,5 +49,7 @@ extern int remove_object_files(const char *odb_dir, object_data *d);
extern int cmp_objects(git_rawobj *o, object_data *d);
+extern int remove_loose_object(const char *odb_dir, git_object *object);
+
#endif
/* INCLUDE_test_helpers_h__ */