Commit 491442f9 by Julio Espinoza-Sokal Committed by Shawn O. Pearce

Factor out test helper methods for creating/deleting loose objects

Signed-off-by: Julio Espinoza-Sokal <julioes@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
parent 840fb8b7
......@@ -114,8 +114,8 @@ $(GIT_LIB): $(OBJS)
$(AR) cr $(GIT_LIB) $(OBJS)
$(RANLIB) $(GIT_LIB)
T_HDR = tests/test_lib.h
T_LIB = tests/test_lib.o
T_HDR = tests/test_lib.h tests/test_helpers.h
T_LIB = tests/test_lib.o tests/test_helpers.o
T_MAIN_C = tests/test_main.c
$(T_LIB): $(T_HDR) $(HDRS)
......
#include "test_lib.h"
#include "test_helpers.h"
#include <git/odb.h>
#include "fileops.h"
......@@ -12,17 +13,6 @@
static char *odb_dir = "test-objects";
typedef struct object_data {
unsigned char *bytes; /* (compressed) bytes stored in object store */
size_t blen; /* length of data in object store */
char *id; /* object id (sha1) */
char *type; /* object type */
char *dir; /* object store (fan-out) directory name */
char *file; /* object store filename */
unsigned char *data; /* (uncompressed) object data */
size_t dlen; /* length of (uncompressed) object data */
} object_data;
/* commit == 3d7f8a6af076c8c3f20071a8935cdbe8228594d1 */
static unsigned char commit_bytes[] = {
0x78, 0x01, 0x85, 0x50, 0xc1, 0x6a, 0xc3, 0x30,
......@@ -534,78 +524,12 @@ static object_data some = {
sizeof(some_data),
};
static int write_object_data(char *file, void *data, size_t len)
{
git_file fd;
int ret;
if ((fd = gitfo_creat(file, 0664)) < 0)
return -1;
ret = gitfo_write(fd, data, len);
gitfo_close(fd);
return ret;
}
static int write_object_files(object_data *d)
{
if (gitfo_mkdir(odb_dir, 0755) < 0) {
if (errno == EEXIST) {
fprintf(stderr, "odb directory \"%s\" already exists!\n", odb_dir);
return -1;
}
fprintf(stderr, "can't make odb directory \"%s\"\n", odb_dir);
return -1;
}
if ((gitfo_mkdir(d->dir, 0755) < 0) && (errno != EEXIST)) {
fprintf(stderr, "can't make object directory \"%s\"\n", d->dir);
return -1;
}
if (write_object_data(d->file, d->bytes, d->blen) < 0) {
fprintf(stderr, "can't write object file \"%s\"\n", d->file);
return -1;
}
return 0;
}
static int remove_object_files(object_data *d)
{
if (gitfo_unlink(d->file) < 0) {
fprintf(stderr, "can't delete object file \"%s\"\n", d->file);
return -1;
}
if ((gitfo_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) {
fprintf(stderr, "can't remove object directory \"%s\"\n", d->dir);
return -1;
}
if (gitfo_rmdir(odb_dir) < 0) {
fprintf(stderr, "can't remove odb directory \"%s\"\n", odb_dir);
return -1;
}
return 0;
}
static int cmp_objects(git_obj *o, object_data *d)
{
if (o->type != git_obj_string_to_type(d->type))
return -1;
if (o->len != d->dlen)
return -1;
if ((o->len > 0) && (memcmp(o->data, d->data, o->len) != 0))
return -1;
return 0;
}
BEGIN_TEST(read_loose_commit)
git_odb *db;
git_oid id;
git_obj obj;
must_pass(write_object_files(&commit));
must_pass(write_object_files(odb_dir, &commit));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, commit.id));
......@@ -614,7 +538,7 @@ BEGIN_TEST(read_loose_commit)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&commit));
must_pass(remove_object_files(odb_dir, &commit));
END_TEST
BEGIN_TEST(read_loose_tree)
......@@ -622,7 +546,7 @@ BEGIN_TEST(read_loose_tree)
git_oid id;
git_obj obj;
must_pass(write_object_files(&tree));
must_pass(write_object_files(odb_dir, &tree));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, tree.id));
......@@ -631,7 +555,7 @@ BEGIN_TEST(read_loose_tree)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&tree));
must_pass(remove_object_files(odb_dir, &tree));
END_TEST
BEGIN_TEST(read_loose_tag)
......@@ -639,7 +563,7 @@ BEGIN_TEST(read_loose_tag)
git_oid id;
git_obj obj;
must_pass(write_object_files(&tag));
must_pass(write_object_files(odb_dir, &tag));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, tag.id));
......@@ -648,7 +572,7 @@ BEGIN_TEST(read_loose_tag)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&tag));
must_pass(remove_object_files(odb_dir, &tag));
END_TEST
BEGIN_TEST(read_loose_zero)
......@@ -656,7 +580,7 @@ BEGIN_TEST(read_loose_zero)
git_oid id;
git_obj obj;
must_pass(write_object_files(&zero));
must_pass(write_object_files(odb_dir, &zero));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, zero.id));
......@@ -665,7 +589,7 @@ BEGIN_TEST(read_loose_zero)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&zero));
must_pass(remove_object_files(odb_dir, &zero));
END_TEST
BEGIN_TEST(read_loose_one)
......@@ -673,7 +597,7 @@ BEGIN_TEST(read_loose_one)
git_oid id;
git_obj obj;
must_pass(write_object_files(&one));
must_pass(write_object_files(odb_dir, &one));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, one.id));
......@@ -682,7 +606,7 @@ BEGIN_TEST(read_loose_one)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&one));
must_pass(remove_object_files(odb_dir, &one));
END_TEST
BEGIN_TEST(read_loose_two)
......@@ -690,7 +614,7 @@ BEGIN_TEST(read_loose_two)
git_oid id;
git_obj obj;
must_pass(write_object_files(&two));
must_pass(write_object_files(odb_dir, &two));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, two.id));
......@@ -699,7 +623,7 @@ BEGIN_TEST(read_loose_two)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&two));
must_pass(remove_object_files(odb_dir, &two));
END_TEST
BEGIN_TEST(read_loose_some)
......@@ -707,7 +631,7 @@ BEGIN_TEST(read_loose_some)
git_oid id;
git_obj obj;
must_pass(write_object_files(&some));
must_pass(write_object_files(odb_dir, &some));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, some.id));
......@@ -716,6 +640,6 @@ BEGIN_TEST(read_loose_some)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&some));
must_pass(remove_object_files(odb_dir, &some));
END_TEST
#include "test_lib.h"
#include "test_helpers.h"
#include <git/odb.h>
#include "fileops.h"
......@@ -18,17 +19,6 @@
static char *odb_dir = "test-objects";
typedef struct object_data {
unsigned char *bytes; /* (compressed) bytes stored in object store */
size_t blen; /* length of data in object store */
char *id; /* object id (sha1) */
char *type; /* object type */
char *dir; /* object store (fan-out) directory name */
char *file; /* object store filename */
unsigned char *data; /* (uncompressed) object data */
size_t dlen; /* length of (uncompressed) object data */
} object_data;
/* commit == 3d7f8a6af076c8c3f20071a8935cdbe8228594d1 */
static unsigned char commit_bytes[] = {
0x92, 0x16, 0x78, 0x9c, 0x85, 0x90, 0x3d, 0x6e,
......@@ -535,78 +525,12 @@ static object_data some = {
sizeof(some_data),
};
static int write_object_data(char *file, void *data, size_t len)
{
git_file fd;
int ret;
if ((fd = gitfo_creat(file, 0664)) < 0)
return -1;
ret = gitfo_write(fd, data, len);
gitfo_close(fd);
return ret;
}
static int write_object_files(object_data *d)
{
if (gitfo_mkdir(odb_dir, 0755) < 0) {
if (errno == EEXIST) {
fprintf(stderr, "odb directory \"%s\" already exists!\n", odb_dir);
return -1;
}
fprintf(stderr, "can't make odb directory \"%s\"\n", odb_dir);
return -1;
}
if ((gitfo_mkdir(d->dir, 0755) < 0) && (errno != EEXIST)) {
fprintf(stderr, "can't make object directory \"%s\"\n", d->dir);
return -1;
}
if (write_object_data(d->file, d->bytes, d->blen) < 0) {
fprintf(stderr, "can't write object file \"%s\"\n", d->file);
return -1;
}
return 0;
}
static int remove_object_files(object_data *d)
{
if (gitfo_unlink(d->file) < 0) {
fprintf(stderr, "can't delete object file \"%s\"\n", d->file);
return -1;
}
if ((gitfo_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) {
fprintf(stderr, "can't remove object directory \"%s\"\n", d->dir);
return -1;
}
if (gitfo_rmdir(odb_dir) < 0) {
fprintf(stderr, "can't remove odb directory \"%s\"\n", odb_dir);
return -1;
}
return 0;
}
static int cmp_objects(git_obj *o, object_data *d)
{
if (o->type != git_obj_string_to_type(d->type))
return -1;
if (o->len != d->dlen)
return -1;
if ((o->len > 0) && (memcmp(o->data, d->data, o->len) != 0))
return -1;
return 0;
}
BEGIN_TEST(read_loose_commit)
git_odb *db;
git_oid id;
git_obj obj;
must_pass(write_object_files(&commit));
must_pass(write_object_files(odb_dir, &commit));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, commit.id));
......@@ -615,7 +539,7 @@ BEGIN_TEST(read_loose_commit)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&commit));
must_pass(remove_object_files(odb_dir, &commit));
END_TEST
BEGIN_TEST(read_loose_tree)
......@@ -623,7 +547,7 @@ BEGIN_TEST(read_loose_tree)
git_oid id;
git_obj obj;
must_pass(write_object_files(&tree));
must_pass(write_object_files(odb_dir, &tree));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, tree.id));
......@@ -632,7 +556,7 @@ BEGIN_TEST(read_loose_tree)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&tree));
must_pass(remove_object_files(odb_dir, &tree));
END_TEST
BEGIN_TEST(read_loose_tag)
......@@ -640,7 +564,7 @@ BEGIN_TEST(read_loose_tag)
git_oid id;
git_obj obj;
must_pass(write_object_files(&tag));
must_pass(write_object_files(odb_dir, &tag));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, tag.id));
......@@ -649,7 +573,7 @@ BEGIN_TEST(read_loose_tag)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&tag));
must_pass(remove_object_files(odb_dir, &tag));
END_TEST
BEGIN_TEST(read_loose_zero)
......@@ -657,7 +581,7 @@ BEGIN_TEST(read_loose_zero)
git_oid id;
git_obj obj;
must_pass(write_object_files(&zero));
must_pass(write_object_files(odb_dir, &zero));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, zero.id));
......@@ -666,7 +590,7 @@ BEGIN_TEST(read_loose_zero)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&zero));
must_pass(remove_object_files(odb_dir, &zero));
END_TEST
BEGIN_TEST(read_loose_one)
......@@ -674,7 +598,7 @@ BEGIN_TEST(read_loose_one)
git_oid id;
git_obj obj;
must_pass(write_object_files(&one));
must_pass(write_object_files(odb_dir, &one));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, one.id));
......@@ -683,7 +607,7 @@ BEGIN_TEST(read_loose_one)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&one));
must_pass(remove_object_files(odb_dir, &one));
END_TEST
BEGIN_TEST(read_loose_two)
......@@ -691,7 +615,7 @@ BEGIN_TEST(read_loose_two)
git_oid id;
git_obj obj;
must_pass(write_object_files(&two));
must_pass(write_object_files(odb_dir, &two));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, two.id));
......@@ -700,7 +624,7 @@ BEGIN_TEST(read_loose_two)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&two));
must_pass(remove_object_files(odb_dir, &two));
END_TEST
BEGIN_TEST(read_loose_some)
......@@ -708,7 +632,7 @@ BEGIN_TEST(read_loose_some)
git_oid id;
git_obj obj;
must_pass(write_object_files(&some));
must_pass(write_object_files(odb_dir, &some));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, some.id));
......@@ -717,6 +641,6 @@ BEGIN_TEST(read_loose_some)
git_obj_close(&obj);
git_odb_close(db);
must_pass(remove_object_files(&some));
must_pass(remove_object_files(odb_dir, &some));
END_TEST
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "test_helpers.h"
#include "fileops.h"
int write_object_data(char *file, void *data, size_t len)
{
git_file fd;
int ret;
if ((fd = gitfo_creat(file, S_IREAD | S_IWRITE)) < 0)
return -1;
ret = gitfo_write(fd, data, len);
gitfo_close(fd);
return ret;
}
int write_object_files(const char *odb_dir, object_data *d)
{
if (gitfo_mkdir(odb_dir, 0755) < 0) {
if (errno == EEXIST) {
fprintf(stderr, "odb directory \"%s\" already exists!\n", odb_dir);
return -1;
}
fprintf(stderr, "can't make odb directory \"%s\"\n", odb_dir);
return -1;
}
if ((gitfo_mkdir(d->dir, 0755) < 0) && (errno != EEXIST)) {
fprintf(stderr, "can't make object directory \"%s\"\n", d->dir);
return -1;
}
if (write_object_data(d->file, d->bytes, d->blen) < 0) {
fprintf(stderr, "can't write object file \"%s\"\n", d->file);
return -1;
}
return 0;
}
int remove_object_files(const char *odb_dir, object_data *d)
{
if (gitfo_unlink(d->file) < 0) {
fprintf(stderr, "can't delete object file \"%s\"\n", d->file);
return -1;
}
if ((gitfo_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) {
fprintf(stderr, "can't remove object directory \"%s\"\n", d->dir);
return -1;
}
if (gitfo_rmdir(odb_dir) < 0) {
fprintf(stderr, "can't remove odb directory \"%s\"\n", odb_dir);
return -1;
}
return 0;
}
int cmp_objects(git_obj *o, object_data *d)
{
if (o->type != git_obj_string_to_type(d->type))
return -1;
if (o->len != d->dlen)
return -1;
if ((o->len > 0) && (memcmp(o->data, d->data, o->len) != 0))
return -1;
return 0;
}
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef INCLUDE_test_helpers_h__
#define INCLUDE_test_helpers_h__
#include "test_lib.h"
#include <git/odb.h>
typedef struct object_data {
unsigned char *bytes; /* (compressed) bytes stored in object store */
size_t blen; /* length of data in object store */
char *id; /* object id (sha1) */
char *type; /* object type */
char *dir; /* object store (fan-out) directory name */
char *file; /* object store filename */
unsigned char *data; /* (uncompressed) object data */
size_t dlen; /* length of (uncompressed) object data */
} object_data;
extern int write_object_data(char *file, void *data, size_t len);
extern int write_object_files(const char *odb_dir, object_data *d);
extern int remove_object_files(const char *odb_dir, object_data *d);
extern int cmp_objects(git_obj *o, object_data *d);
#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