commit_tests.c (4291B)
1 #include "writer/html/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 html_commit { 15 int dummy; 16 }; 17 18 UTEST_F_SETUP(html_commit) { 19 inmemory_fs_clear(); 20 } 21 22 UTEST_F_TEARDOWN(html_commit) {} 23 24 UTEST_F(html_commit, basic) { 25 GitRepo repo = {.short_name = "test-repo"}; 26 HtmlCommit* commit_writer = 27 html_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 html_commit_begin(commit_writer); 42 html_commit_add_commit(commit_writer, &commit); 43 html_commit_end(commit_writer); 44 html_commit_free(commit_writer); 45 46 const char* buf = inmemory_fs_get_buffer("commit/sha123.html"); 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 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(html_commit, diff) { 63 GitRepo repo = {.short_name = "test-repo"}; 64 HtmlCommit* commit_writer = 65 html_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 html_commit_begin(commit_writer); 112 html_commit_add_commit(commit_writer, &commit); 113 html_commit_end(commit_writer); 114 html_commit_free(commit_writer); 115 116 const char* buf = inmemory_fs_get_buffer("commit/sha123.html"); 117 ASSERT_NE(NULL, buf); 118 119 /* Verify Diffstat */ 120 EXPECT_STR_SEQUENCE(buf, "Diffstat:", "file.txt", "2"); 121 122 /* Verify Diff Content */ 123 EXPECT_STR_SEQUENCE(buf, "diff --git", "file.txt", "@@ -1,2 +1,2 @@", 124 " unchanged", "+added line", "-removed line"); 125 } 126 127 UTEST_F(html_commit, too_large_diff) { 128 GitRepo repo = {.short_name = "test-repo"}; 129 HtmlCommit* commit_writer = 130 html_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 html_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 html_commit_begin(commit_writer); 148 html_commit_add_commit(commit_writer, &commit); 149 html_commit_end(commit_writer); 150 html_commit_free(commit_writer); 151 152 const char* buf = inmemory_fs_get_buffer("commit/sha123.html"); 153 ASSERT_NE(NULL, buf); 154 EXPECT_NE(NULL, strstr(buf, "Diff is too large, output suppressed.")); 155 }