Commit 4378e8d4 by Vicent Marti

Add unit test for writing a big index file

Signed-off-by: Vicent Marti <tanoku@gmail.com>
parent 5591ea15
......@@ -134,77 +134,31 @@ END_TEST
BEGIN_TEST("write", index_write_test)
git_index *index;
git_filelock out_file;
must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
must_pass(copy_file(TEST_INDEXBIG_PATH, "index_rewrite"));
must_pass(git_index_open_bare(&index, "index_rewrite"));
must_pass(git_index_read(index));
must_be_true(index->on_disk);
/*
* TODO:
* Don't write the index like this; make sure the filelocks
* are manually set
*/
/*
must_pass(git_filelock_init(&out_file, "index_rewrite"));
must_pass(git_filelock_lock(&out_file, 0));
must_pass(git_index__write(index, &out_file));
must_pass(git_filelock_commit(&out_file));
*/
must_pass(git_index_write(index));
must_pass(cmp_files(TEST_INDEXBIG_PATH, "index_rewrite"));
git_index_free(index);
gitfo_unlink("index_rewrite");
END_TEST
static void randomize_entries(git_index *index)
{
unsigned int i, j;
git_index_entry *tmp;
git_index_entry **entries;
entries = (git_index_entry **)index->entries.contents;
srand((unsigned int)time(NULL));
for (i = 0; i < index->entries.length; ++i) {
j = rand() % index->entries.length;
tmp = entries[j];
entries[j] = entries[i];
entries[i] = tmp;
}
index->sorted = 0;
}
BEGIN_TEST("sort", index_sort_test)
git_index *index;
unsigned int i;
git_index_entry **entries;
must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
must_pass(git_index_read(index));
randomize_entries(index);
/*
* TODO: This no longer applies:
* index sorting in Git uses some specific changes to the way
* directories are sorted.
*
* We need to specificially check for this by creating a new
* index, adding entries in random order and then
* checking for consistency
*/
/*
git_index__sort(index);
must_be_true(index->sorted);
entries = (git_index_entry **)index->entries.contents;
for (i = 1; i < index->entries.length; ++i)
must_be_true(strcmp(entries[i - 1]->path, entries[i]->path) < 0);
*/
git_index_free(index);
END_TEST
......
......@@ -131,3 +131,47 @@ int cmp_objects(git_rawobj *o, object_data *d)
return -1;
return 0;
}
int copy_file(const char *src, const char *dst)
{
gitfo_buf source_buf;
git_file dst_fd;
int error = GIT_ERROR;
if (gitfo_read_file(&source_buf, src) < GIT_SUCCESS)
return GIT_ENOTFOUND;
dst_fd = gitfo_creat(dst, 0644);
if (dst_fd < 0)
goto cleanup;
error = gitfo_write(dst_fd, source_buf.data, source_buf.len);
cleanup:
gitfo_free_buf(&source_buf);
gitfo_close(dst_fd);
return error;
}
int cmp_files(const char *a, const char *b)
{
gitfo_buf buf_a, buf_b;
int error = GIT_ERROR;
if (gitfo_read_file(&buf_a, a) < GIT_SUCCESS)
return GIT_ERROR;
if (gitfo_read_file(&buf_b, b) < GIT_SUCCESS) {
gitfo_free_buf(&buf_a);
return GIT_ERROR;
}
if (buf_a.len == buf_b.len && !memcmp(buf_a.data, buf_b.data, buf_a.len))
error = GIT_SUCCESS;
gitfo_free_buf(&buf_a);
gitfo_free_buf(&buf_b);
return error;
}
......@@ -33,6 +33,7 @@
#define REPOSITORY_FOLDER (TEST_RESOURCES "/testrepo.git/")
#define TEST_INDEX_PATH (TEST_RESOURCES "/testrepo.git/index")
#define TEST_INDEX2_PATH (TEST_RESOURCES "/gitgit.index")
#define TEST_INDEXBIG_PATH (TEST_RESOURCES "/big.index")
typedef struct object_data {
unsigned char *bytes; /* (compressed) bytes stored in object store */
......@@ -55,5 +56,8 @@ extern int cmp_objects(git_rawobj *o, object_data *d);
extern int remove_loose_object(const char *odb_dir, git_object *object);
extern int cmp_files(const char *a, const char *b);
extern int copy_file(const char *source, const char *dest);
#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