gout

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

commit_tests.c (4351B)


      1 #include "writer/gopher/commit.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "fs_inmemory.h"
      8 #include "git/commit.h"
      9 #include "git/delta.h"
     10 #include "git/repo.h"
     11 #include "test_utils.h"
     12 #include "utest.h"
     13 
     14 struct gopher_commit {
     15   int dummy;
     16 };
     17 
     18 UTEST_F_SETUP(gopher_commit) {
     19   inmemory_fs_clear();
     20 }
     21 
     22 UTEST_F_TEARDOWN(gopher_commit) {}
     23 
     24 UTEST_F(gopher_commit, basic) {
     25   GitRepo repo = {.short_name = "test-repo"};
     26   GopherCommit* commit_writer =
     27       gopher_commit_create(&repo, g_fs_inmemory, "sha123", "Commit Title");
     28   ASSERT_NE(NULL, commit_writer);
     29 
     30   GitCommit commit = {
     31       .oid = "sha123",
     32       .parentoid = "parent456",
     33       .summary = "Fix a bug",
     34       .message = "Detailed description.",
     35       .author_name = "Author Name",
     36       .author_email = "author@example.com",
     37       .author_time = 1702031400,
     38       .author_timezone_offset = 0,
     39   };
     40 
     41   gopher_commit_begin(commit_writer);
     42   gopher_commit_add_commit(commit_writer, &commit);
     43   gopher_commit_end(commit_writer);
     44   gopher_commit_free(commit_writer);
     45 
     46   const char* buf = inmemory_fs_get_buffer("commit/sha123.gph");
     47   ASSERT_NE(NULL, buf);
     48 
     49   /* Verify Header/Page Title */
     50   EXPECT_STR_SEQUENCE(buf, "Commit Title");
     51 
     52   /* Verify Author Info */
     53   EXPECT_STR_SEQUENCE(buf, "Author: Author Name", "author@example.com");
     54 
     55   /* Verify Parent */
     56   EXPECT_STR_SEQUENCE(buf, "parent", "parent456");
     57 
     58   /* Verify Message */
     59   EXPECT_STR_SEQUENCE(buf, "Detailed description.");
     60 }
     61 
     62 UTEST_F(gopher_commit, diff) {
     63   GitRepo repo = {.short_name = "test-repo"};
     64   GopherCommit* commit_writer =
     65       gopher_commit_create(&repo, g_fs_inmemory, "sha123", "Commit Title");
     66 
     67   GitHunkLine l1 = {.id = 1,
     68                     .old_lineno = 1,
     69                     .new_lineno = 1,
     70                     .content = " unchanged",
     71                     .content_len = 10};
     72   GitHunkLine l2 = {.id = 2,
     73                     .old_lineno = -1,
     74                     .new_lineno = 2,
     75                     .content = "added line",
     76                     .content_len = 10};
     77   GitHunkLine l3 = {.id = 3,
     78                     .old_lineno = 2,
     79                     .new_lineno = -1,
     80                     .content = "removed line",
     81                     .content_len = 12};
     82 
     83   GitHunkLine* lines[] = {&l1, &l2, &l3};
     84   GitHunk hunk = {
     85       .id = 1, .header = "@@ -1,2 +1,2 @@", .lines = lines, .lines_len = 3};
     86   GitHunk* hunks[] = {&hunk};
     87 
     88   GitDelta delta = {
     89       .status = 'M',
     90       .old_file_path = "file.txt",
     91       .new_file_path = "file.txt",
     92       .addcount = 1,
     93       .delcount = 1,
     94       .hunks = hunks,
     95       .hunks_len = 1,
     96   };
     97   GitDelta* deltas[] = {&delta};
     98 
     99   GitCommit commit = {
    100       .oid = "sha123",
    101       .parentoid = "",
    102       .author_name = "User",
    103       .author_email = "user@mail.com",
    104       .deltas = deltas,
    105       .deltas_len = 1,
    106       .filecount = 1,
    107       .addcount = 1,
    108       .delcount = 1,
    109   };
    110 
    111   gopher_commit_begin(commit_writer);
    112   gopher_commit_add_commit(commit_writer, &commit);
    113   gopher_commit_end(commit_writer);
    114   gopher_commit_free(commit_writer);
    115 
    116   const char* buf = inmemory_fs_get_buffer("commit/sha123.gph");
    117   ASSERT_NE(NULL, buf);
    118 
    119   /* Verify Diffstat */
    120   EXPECT_STR_SEQUENCE(buf, "Diffstat:", "file.txt", "1");
    121 
    122   /* Verify Diff Content */
    123   EXPECT_STR_SEQUENCE(buf, "[1|diff --git", "file.txt", "@@ -1,2 +1,2 @@",
    124                       " unchanged", "+added line", "-removed line");
    125 }
    126 
    127 UTEST_F(gopher_commit, too_large_diff) {
    128   GitRepo repo = {.short_name = "test-repo"};
    129   GopherCommit* commit_writer =
    130       gopher_commit_create(&repo, g_fs_inmemory, "sha123", "Commit Title");
    131 
    132   /* Set limits small to trigger suppression. */
    133   DiffLimits limits = {
    134       .max_files = 0, .max_deltas = 1000, .max_delta_lines = 1000};
    135   gopher_commit_set_diff_limits(commit_writer, &limits);
    136 
    137   GitCommit commit = {
    138       .oid = "sha123",
    139       .author_name = "User",
    140       .author_email = "user@example.com",
    141       .summary = "Summary",
    142       .message = "Message",
    143       .deltas_len = 1,
    144       .filecount = 1,
    145   };
    146 
    147   gopher_commit_begin(commit_writer);
    148   gopher_commit_add_commit(commit_writer, &commit);
    149   gopher_commit_end(commit_writer);
    150   gopher_commit_free(commit_writer);
    151 
    152   const char* buf = inmemory_fs_get_buffer("commit/sha123.gph");
    153   ASSERT_NE(NULL, buf);
    154   EXPECT_NE(NULL, strstr(buf, "Diff is too large, output suppressed."));
    155 }