files_tests.c (2438B)
1 #include "writer/html/files.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include "fs_inmemory.h" 8 #include "git/file.h" 9 #include "git/repo.h" 10 #include "test_utils.h" 11 #include "utest.h" 12 13 struct html_files { 14 int dummy; 15 }; 16 17 UTEST_F_SETUP(html_files) { 18 inmemory_fs_clear(); 19 } 20 21 UTEST_F_TEARDOWN(html_files) {} 22 23 UTEST_F(html_files, basic) { 24 GitRepo repo = {.short_name = "test-repo"}; 25 HtmlFiles* files = html_files_create(&repo, g_fs_inmemory); 26 ASSERT_NE(NULL, files); 27 28 GitFile f1 = { 29 .type = kFileTypeFile, 30 .mode = "-rw-r--r--", 31 .display_path = "README.md", 32 .repo_path = "README.md", 33 .size_bytes = 100, 34 .size_lines = 5, 35 .commit_oid = "", 36 }; 37 38 html_files_begin(files); 39 html_files_add_file(files, &f1); 40 html_files_end(files); 41 html_files_free(files); 42 43 const char* buf = inmemory_fs_get_buffer("files.html"); 44 ASSERT_NE(NULL, buf); 45 EXPECT_STR_SEQUENCE(buf, "<thead>", "Mode", "Name", "Size", "</thead>", 46 "<tbody>", "-rw-r--r--", "README.md", "5L", "</tbody>"); 47 } 48 49 UTEST_F(html_files, submodule) { 50 GitRepo repo = {.short_name = "test-repo"}; 51 HtmlFiles* files = html_files_create(&repo, g_fs_inmemory); 52 ASSERT_NE(NULL, files); 53 54 GitFile sub = { 55 .type = kFileTypeSubmodule, 56 .mode = "m---------", 57 .display_path = "third_party/lib", 58 .repo_path = "third_party/lib", 59 .size_bytes = -1, 60 .size_lines = -1, 61 .commit_oid = "abcdef123456", 62 }; 63 64 html_files_begin(files); 65 html_files_add_file(files, &sub); 66 html_files_end(files); 67 html_files_free(files); 68 69 const char* buf = inmemory_fs_get_buffer("files.html"); 70 ASSERT_NE(NULL, buf); 71 EXPECT_STR_SEQUENCE(buf, "m---------", "third_party/lib", "@ abcdef123456"); 72 } 73 74 UTEST_F(html_files, binary_file) { 75 GitRepo repo = {.short_name = "test-repo"}; 76 HtmlFiles* files = html_files_create(&repo, g_fs_inmemory); 77 ASSERT_NE(NULL, files); 78 79 GitFile bin = { 80 .type = kFileTypeFile, 81 .mode = "-rwxr-xr-x", 82 .display_path = "logo.png", 83 .repo_path = "logo.png", 84 .size_bytes = 1024, 85 .size_lines = -1, /* -1 indicates binary */ 86 .commit_oid = "", 87 }; 88 89 html_files_begin(files); 90 html_files_add_file(files, &bin); 91 html_files_end(files); 92 html_files_free(files); 93 94 const char* buf = inmemory_fs_get_buffer("files.html"); 95 ASSERT_NE(NULL, buf); 96 EXPECT_STR_SEQUENCE(buf, "-rwxr-xr-x", "logo.png", "1024B"); 97 }