Commit 8e11e707f3f6c32b8cd2363bb9bbdd88ac8bc4d4

nulltoken 2011-06-15T16:06:20

git_index_add: enforce test coverage

diff --git a/tests/t06-index.c b/tests/t06-index.c
index 25adbf7..621deab 100644
--- a/tests/t06-index.c
+++ b/tests/t06-index.c
@@ -161,7 +161,6 @@ BEGIN_TEST(sort0, "sort the entires in an index")
 	 */
 END_TEST
 
-
 BEGIN_TEST(sort1, "sort the entires in an empty index")
 	git_index *index;
 
@@ -173,6 +172,46 @@ BEGIN_TEST(sort1, "sort the entires in an empty index")
 	git_index_free(index);
 END_TEST
 
+BEGIN_TEST(add0, "add a new file to the index")
+	git_index *index;
+	git_filebuf file;
+	git_repository *repo;
+	git_index_entry *entry;
+	git_oid id1;
+
+	/* Intialize a new repository */
+	must_pass(git_repository_init(&repo, TEMP_REPO_FOLDER "myrepo", 0));
+
+	/* Ensure we're the only guy in the room */
+	must_pass(git_repository_index(&index, repo));
+	must_pass(git_index_entrycount(index) == 0);
+
+	/* Create a new file in the working directory */
+	must_pass(gitfo_mkdir_2file(TEMP_REPO_FOLDER "myrepo/test.txt"));
+	must_pass(git_filebuf_open(&file, TEMP_REPO_FOLDER "myrepo/test.txt", 0));
+	must_pass(git_filebuf_write(&file, "hey there\n", 10));
+	must_pass(git_filebuf_commit(&file));
+
+	/* Store the expected hash of the file/blob
+	 * This has been generated by executing the following
+	 * $ echo "hey there" | git hash-object --stdin
+	 */
+	must_pass(git_oid_mkstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
+
+	/* Add the new file to the index */
+	must_pass(git_index_add(index, "test.txt", 0));
+
+	/* Wow... it worked! */
+	must_pass(git_index_entrycount(index) == 1);
+	entry = git_index_get(index, 0);
+
+	/* And the built-in hashing mechanism worked as expected */
+    must_be_true(git_oid_cmp(&id1, &entry->oid) == 0);
+
+	git_repository_free(repo);
+	rmdir_recurs(TEMP_REPO_FOLDER);
+END_TEST
+
 BEGIN_SUITE(index)
 	ADD_TEST(read0);
 	ADD_TEST(read1);
@@ -185,4 +224,6 @@ BEGIN_SUITE(index)
 
 	ADD_TEST(sort0);
 	ADD_TEST(sort1);
+
+	ADD_TEST(add0);
 END_SUITE