gout

A static git page generator
git clone https://git.bracken.jp/gout.git
Log | Files | Refs | README | LICENSE

commit_tests.c (3294B)


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