Commit 4378e8d470abae5e9e8f32f98869516c8b86b191

Vicent Marti 2011-02-22T15:15:35

Add unit test for writing a big index file Signed-off-by: Vicent Marti <tanoku@gmail.com>

diff --git a/tests/resources/big.index b/tests/resources/big.index
new file mode 100644
index 0000000..66932f1
Binary files /dev/null and b/tests/resources/big.index differ
diff --git a/tests/t06-index.c b/tests/t06-index.c
index 2d5de74..ded2370 100644
--- a/tests/t06-index.c
+++ b/tests/t06-index.c
@@ -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
 
 
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index 5fd9a56..c93d31b 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -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;
+}
diff --git a/tests/test_helpers.h b/tests/test_helpers.h
index 7551fff..0a9a9e1 100644
--- a/tests/test_helpers.h
+++ b/tests/test_helpers.h
@@ -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__ */