log_tests.c (3129B)
1 #include "writer/gopher/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 gopher_log { 14 int dummy; 15 }; 16 17 UTEST_F_SETUP(gopher_log) { 18 inmemory_fs_clear(); 19 } 20 21 UTEST_F_TEARDOWN(gopher_log) {} 22 23 UTEST_F(gopher_log, begin) { 24 GitRepo repo = {.short_name = "test-repo"}; 25 GopherLog* log = gopher_log_create(&repo, g_fs_inmemory); 26 ASSERT_NE(NULL, log); 27 gopher_log_begin(log); 28 gopher_log_free(log); 29 30 const char* buf = inmemory_fs_get_buffer("log.gph"); 31 ASSERT_NE(NULL, buf); 32 EXPECT_STR_SEQUENCE(buf, "test-repo", "Date", "Commit message", "Author"); 33 } 34 35 UTEST_F(gopher_log, add_commit) { 36 GitRepo repo = {.short_name = "test-repo"}; 37 GopherLog* log = gopher_log_create(&repo, g_fs_inmemory); 38 ASSERT_NE(NULL, log); 39 40 GitCommit commit = { 41 .oid = "abc1234", 42 .summary = "Fix bug", 43 .author_name = "User", 44 .author_time = 1702031400, 45 }; 46 47 gopher_log_begin(log); 48 gopher_log_add_commit(log, &commit); 49 gopher_log_end(log); 50 gopher_log_free(log); 51 52 const char* buf = inmemory_fs_get_buffer("log.gph"); 53 ASSERT_NE(NULL, buf); 54 /* Gopher format: [1|2023-12-08 10:30 Fix bug 55 * User|commit/abc1234.gph|server|port] */ 56 EXPECT_STR_SEQUENCE(buf, "[1|", "2023-12-08 10:30", "Fix bug", "User", 57 "|commit/abc1234.gph"); 58 } 59 60 UTEST_F(gopher_log, commit_limit) { 61 GitRepo repo = {.short_name = "test-repo"}; 62 GopherLog* log = gopher_log_create(&repo, g_fs_inmemory); 63 ASSERT_NE(NULL, log); 64 gopher_log_set_commit_limit(log, 2); 65 66 GitCommit commit = {.oid = "sha", .author_name = "user", .summary = "msg"}; 67 68 gopher_log_begin(log); 69 gopher_log_add_commit(log, &commit); 70 gopher_log_add_commit(log, &commit); 71 gopher_log_add_commit(log, &commit); 72 gopher_log_add_commit(log, &commit); 73 74 gopher_log_end(log); 75 gopher_log_free(log); 76 77 const char* buf = inmemory_fs_get_buffer("log.gph"); 78 ASSERT_NE(NULL, buf); 79 EXPECT_STR_COUNT(buf, "msg", 2); 80 EXPECT_NE(NULL, strstr(buf, "2 more commits remaining")); 81 } 82 83 UTEST_F(gopher_log, cache_integration) { 84 const char* old_cache_content = "sha_old\n[1|Old Commit|path|server|port]\n"; 85 FILE* pre = g_fs_inmemory->fopen("cache.db", "w"); 86 fprintf(pre, "%s", old_cache_content); 87 g_fs_inmemory->fclose(pre); 88 89 GitRepo repo = {.short_name = "test-repo"}; 90 GopherLog* log = gopher_log_create(&repo, g_fs_inmemory); 91 gopher_log_set_cachefile(log, "cache.db"); 92 93 GitCommit c_new = { 94 .oid = "sha_new", .summary = "New Commit", .author_name = "User"}; 95 GitCommit c_old = { 96 .oid = "sha_old", .summary = "Old Commit", .author_name = "User"}; 97 98 gopher_log_begin(log); 99 gopher_log_add_commit(log, &c_new); 100 gopher_log_add_commit(log, &c_old); 101 gopher_log_end(log); 102 gopher_log_free(log); 103 104 const char* buf = inmemory_fs_get_buffer("log.gph"); 105 ASSERT_NE(NULL, buf); 106 107 EXPECT_STR_SEQUENCE(buf, "New Commit", "Old Commit"); 108 109 const char* cache_buf = inmemory_fs_get_buffer("cache.db"); 110 ASSERT_NE(NULL, cache_buf); 111 EXPECT_TRUE(strncmp(cache_buf, "sha_new", 7) == 0); 112 }