Commit 0ba7a031 by Vicent Marti

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>
parent ec25391d
#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
#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
#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
......@@ -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))
......
......@@ -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__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment