gout

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

log_tests.c (3549B)


      1 #include "writer/html/log.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/repo.h"
     10 #include "test_utils.h"
     11 #include "utest.h"
     12 
     13 struct html_log {
     14   int dummy;
     15 };
     16 
     17 UTEST_F_SETUP(html_log) {
     18   inmemory_fs_clear();
     19 }
     20 
     21 UTEST_F_TEARDOWN(html_log) {}
     22 
     23 UTEST_F(html_log, begin) {
     24   GitRepo repo = {.short_name = "test-repo"};
     25   HtmlLog* log = html_log_create(&repo, g_fs_inmemory);
     26   ASSERT_NE(NULL, log);
     27   html_log_begin(log);
     28   html_log_free(log);
     29 
     30   const char* buf = inmemory_fs_get_buffer("log.html");
     31   ASSERT_NE(NULL, buf);
     32   EXPECT_STR_SEQUENCE(buf, "test-repo", "<thead>", "Date", "Commit message",
     33                       "Author");
     34 }
     35 
     36 UTEST_F(html_log, add_commit) {
     37   GitRepo repo = {.short_name = "test-repo"};
     38   HtmlLog* log = html_log_create(&repo, g_fs_inmemory);
     39   ASSERT_NE(NULL, log);
     40 
     41   GitCommit commit = {
     42       .oid = "abc1234",
     43       .summary = "Fix bug",
     44       .author_name = "User",
     45       .author_time = 1702031400,
     46       .filecount = 2,
     47       .addcount = 10,
     48       .delcount = 5,
     49   };
     50 
     51   html_log_begin(log);
     52   html_log_add_commit(log, &commit);
     53   html_log_end(log);
     54   html_log_free(log);
     55 
     56   const char* buf = inmemory_fs_get_buffer("log.html");
     57   ASSERT_NE(NULL, buf);
     58   EXPECT_STR_SEQUENCE(buf, "2023-12-08 10:30", "Fix bug", "User", "2", "+10",
     59                       "-5");
     60 }
     61 
     62 UTEST_F(html_log, commit_limit) {
     63   GitRepo repo = {.short_name = "test-repo"};
     64   HtmlLog* log = html_log_create(&repo, g_fs_inmemory);
     65   ASSERT_NE(NULL, log);
     66   html_log_set_commit_limit(log, 2);
     67 
     68   GitCommit commit = {.oid = "sha", .author_name = "user", .summary = "msg"};
     69 
     70   html_log_begin(log);
     71   html_log_add_commit(log, &commit);
     72   html_log_add_commit(log, &commit);
     73   html_log_add_commit(log, &commit); /* Should be unlogged */
     74 
     75   html_log_end(log);
     76   html_log_free(log);
     77 
     78   const char* buf = inmemory_fs_get_buffer("log.html");
     79   ASSERT_NE(NULL, buf);
     80   EXPECT_STR_COUNT(buf, "msg", 2);
     81   EXPECT_NE(NULL, strstr(buf, "1 more commits remaining"));
     82 }
     83 
     84 UTEST_F(html_log, cache_integration) {
     85   /* 1. Setup a "previous" cache.
     86    * The first line is the OID, the rest is the log rows. */
     87   const char* old_cache_content = "sha_old\n<tr><td>Old Commit</td></tr>\n";
     88 
     89   /* We need to pre-register this file in our mock FS so it can be read.
     90    * Since our mock fopen('r') looks at g_files, we can 'write' it first. */
     91   FILE* pre = g_fs_inmemory->fopen("cache.db", "w");
     92   fprintf(pre, "%s", old_cache_content);
     93   g_fs_inmemory->fclose(pre);
     94 
     95   GitRepo repo = {.short_name = "test-repo"};
     96   HtmlLog* log = html_log_create(&repo, g_fs_inmemory);
     97   html_log_set_cachefile(log, "cache.db");
     98 
     99   GitCommit c_new = {
    100       .oid = "sha_new", .summary = "New Commit", .author_name = "User"};
    101   GitCommit c_old = {
    102       .oid = "sha_old", .summary = "Old Commit", .author_name = "User"};
    103 
    104   html_log_begin(log);
    105   html_log_add_commit(log, &c_new);
    106   html_log_add_commit(log, &c_old); /* This should trigger the cache stop */
    107   html_log_end(log);
    108   html_log_free(log);
    109 
    110   /* Verify log.html contains BOTH the new commit and the OLD cache content. */
    111   const char* buf = inmemory_fs_get_buffer("log.html");
    112   ASSERT_NE(NULL, buf);
    113 
    114   EXPECT_STR_SEQUENCE(
    115       buf, "New Commit",
    116       "Old Commit" /* This comes from the cache_copy_log call in html_log_end */
    117   );
    118 
    119   /* Verify the cache file itself was updated with the new OID. */
    120   const char* cache_buf = inmemory_fs_get_buffer("cache.db");
    121   ASSERT_NE(NULL, cache_buf);
    122   EXPECT_TRUE(strncmp(cache_buf, "sha_new", 7) == 0);
    123 }