repo_tests.c (2723B)
1 #include "git/repo.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include "fs_inmemory.h" 8 #include "git/internal.h" 9 #include "utest.h" 10 11 struct git_repo { 12 int dummy; 13 }; 14 15 UTEST_F_SETUP(git_repo) { 16 inmemory_fs_clear(); 17 } 18 19 UTEST_F_TEARDOWN(git_repo) {} 20 21 UTEST_F(git_repo, load_filesystem_metadata) { 22 /* 1. Setup mock repo files. */ 23 const char* repo_path = "/path/to/my-repo.git"; 24 25 /* Mock description */ 26 FILE* f_desc = g_fs_inmemory->fopen("/path/to/my-repo.git/description", "w"); 27 fprintf(f_desc, "My project description\n"); 28 g_fs_inmemory->fclose(f_desc); 29 30 /* Mock owner */ 31 FILE* f_owner = g_fs_inmemory->fopen("/path/to/my-repo.git/owner", "w"); 32 fprintf(f_owner, "John Doe\n"); 33 g_fs_inmemory->fclose(f_owner); 34 35 /* Mock url */ 36 FILE* f_url = g_fs_inmemory->fopen("/path/to/my-repo.git/url", "w"); 37 fprintf(f_url, "https://example.com/repo.git\n"); 38 g_fs_inmemory->fclose(f_url); 39 40 /* 2. Load metadata. */ 41 GitRepo* repo = ecalloc(1, sizeof(GitRepo)); 42 gitrepo_load_filesystem_metadata(repo, g_fs_inmemory, repo_path); 43 44 /* 3. Verify. */ 45 EXPECT_STREQ("my-repo.git", repo->name); 46 EXPECT_STREQ("my-repo", repo->short_name); 47 EXPECT_STREQ("My project description", repo->description); 48 EXPECT_STREQ("John Doe", repo->owner); 49 EXPECT_STREQ("https://example.com/repo.git", repo->clone_url); 50 51 gitrepo_free(repo); 52 } 53 54 UTEST_F(git_repo, load_filesystem_metadata_bare) { 55 /* Setup a non-dot-git directory (simulating a bare repo or just a dir). */ 56 const char* repo_path = "/path/to/my-bare-repo"; 57 58 FILE* f_desc = g_fs_inmemory->fopen("/path/to/my-bare-repo/description", "w"); 59 fprintf(f_desc, "Bare repo desc\n"); 60 g_fs_inmemory->fclose(f_desc); 61 62 GitRepo* repo = ecalloc(1, sizeof(GitRepo)); 63 gitrepo_load_filesystem_metadata(repo, g_fs_inmemory, repo_path); 64 65 EXPECT_STREQ("my-bare-repo", repo->name); 66 EXPECT_STREQ("Bare repo desc", repo->description); 67 68 gitrepo_free(repo); 69 } 70 71 UTEST_F(git_repo, load_filesystem_metadata_crlf) { 72 const char* repo_path = "/path/to/crlf-repo.git"; 73 74 /* Mock description with CRLF */ 75 FILE* f_desc = 76 g_fs_inmemory->fopen("/path/to/crlf-repo.git/description", "w"); 77 fprintf(f_desc, "Description with CRLF\r\n"); 78 g_fs_inmemory->fclose(f_desc); 79 80 /* Mock owner with just CR (unlikely but good for testing robustness) */ 81 FILE* f_owner = g_fs_inmemory->fopen("/path/to/crlf-repo.git/owner", "w"); 82 fprintf(f_owner, "Owner with CR\r"); 83 g_fs_inmemory->fclose(f_owner); 84 85 GitRepo* repo = ecalloc(1, sizeof(GitRepo)); 86 gitrepo_load_filesystem_metadata(repo, g_fs_inmemory, repo_path); 87 88 EXPECT_STREQ("Description with CRLF", repo->description); 89 EXPECT_STREQ("Owner with CR", repo->owner); 90 91 gitrepo_free(repo); 92 }