git_index_add: enforce test coverage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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