commit_tests.c (3228B)
1 #include "git/commit.h" 2 3 #include <ftw.h> 4 #include <git2.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <unistd.h> 8 9 #include "git/internal.h" 10 #include "utest.h" 11 12 struct git_commit_test { 13 char repo_path[1024]; 14 git_repository* repo; 15 }; 16 17 static int remove_obj(const char* path, 18 const struct stat* sb, 19 int typeflag, 20 struct FTW* ftwbuf) { 21 return remove(path); 22 } 23 24 UTEST_F_SETUP(git_commit_test) { 25 git_libgit2_init(); 26 strcpy(utest_fixture->repo_path, "/tmp/gout_test_XXXXXX"); 27 if (!mkdtemp(utest_fixture->repo_path)) { 28 exit(1); 29 } 30 if (git_repository_init(&utest_fixture->repo, utest_fixture->repo_path, 0)) { 31 exit(1); 32 } 33 } 34 35 UTEST_F_TEARDOWN(git_commit_test) { 36 git_repository_free(utest_fixture->repo); 37 nftw(utest_fixture->repo_path, remove_obj, 64, FTW_DEPTH | FTW_PHYS); 38 git_libgit2_shutdown(); 39 } 40 41 UTEST_F(git_commit_test, CommitParentHandling) { 42 git_oid tree_id, commit_id, child_id; 43 git_treebuilder* builder = NULL; 44 git_tree* tree = NULL; 45 git_signature* sig = NULL; 46 47 /* Create an empty tree. */ 48 ASSERT_EQ(0, git_treebuilder_new(&builder, utest_fixture->repo, NULL)); 49 ASSERT_EQ(0, git_treebuilder_write(&tree_id, builder)); 50 git_treebuilder_free(builder); 51 ASSERT_EQ(0, git_tree_lookup(&tree, utest_fixture->repo, &tree_id)); 52 53 /* Create a signature. */ 54 ASSERT_EQ(0, git_signature_now(&sig, "Test User", "test@example.com")); 55 56 /* Create the root commit (no parents). */ 57 ASSERT_EQ(0, git_commit_create(&commit_id, utest_fixture->repo, "HEAD", sig, 58 sig, NULL, "Initial commit", tree, 0, NULL)); 59 60 GitCommit* root = gitcommit_create(&commit_id, utest_fixture->repo); 61 ASSERT_NE(NULL, root); 62 /* parentoid should be an empty string if there's no parent. */ 63 EXPECT_STREQ("", root->parentoid); 64 EXPECT_STREQ("Initial commit", root->summary); 65 66 /* Create a child commit. */ 67 git_commit* root_obj = NULL; 68 ASSERT_EQ(0, git_commit_lookup(&root_obj, utest_fixture->repo, &commit_id)); 69 ASSERT_EQ(0, git_commit_create(&child_id, utest_fixture->repo, "HEAD", sig, 70 sig, NULL, "Child commit", tree, 1, 71 (const git_commit**)&root_obj)); 72 73 GitCommit* child = gitcommit_create(&child_id, utest_fixture->repo); 74 ASSERT_NE(NULL, child); 75 ASSERT_NE('\0', child->parentoid[0]); 76 77 char root_oid_str[GIT_OID_MAX_HEXSIZE + 1]; 78 git_oid_tostr(root_oid_str, sizeof(root_oid_str), &commit_id); 79 EXPECT_STREQ(root_oid_str, child->parentoid); 80 EXPECT_STREQ("Child commit", child->summary); 81 82 gitcommit_free(root); 83 gitcommit_free(child); 84 git_commit_free(root_obj); 85 git_tree_free(tree); 86 git_signature_free(sig); 87 } 88 89 UTEST_F(git_commit_test, DanglingReference) { 90 git_reference* dangling = NULL; 91 92 /* Create a symbolic reference pointing to a non-existent target. */ 93 ASSERT_EQ(0, git_reference_symbolic_create( 94 &dangling, utest_fixture->repo, "refs/heads/dangling", 95 "refs/heads/nonexistent", 0, NULL)); 96 97 /* gitreference_create should return NULL for dangling references. */ 98 GitReference* ref = gitreference_create(utest_fixture->repo, dangling); 99 EXPECT_EQ(NULL, ref); 100 }