|
| 1 | +#include "clar_libgit2.h" |
| 2 | +#include "worktree_helpers.h" |
| 3 | + |
| 4 | +#include "git2/worktree.h" |
| 5 | +#include "repository.h" |
| 6 | + |
| 7 | +#define COMMON_REPO "testrepo" |
| 8 | +#define WORKTREE_REPO "testrepo-worktree" |
| 9 | + |
| 10 | +static worktree_fixture fixture = |
| 11 | + WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); |
| 12 | + |
| 13 | +void test_worktree_worktree__initialize(void) |
| 14 | +{ |
| 15 | + setup_fixture_worktree(&fixture); |
| 16 | +} |
| 17 | + |
| 18 | +void test_worktree_worktree__cleanup(void) |
| 19 | +{ |
| 20 | + cleanup_fixture_worktree(&fixture); |
| 21 | +} |
| 22 | + |
| 23 | +void test_worktree_worktree__list(void) |
| 24 | +{ |
| 25 | + git_strarray wts; |
| 26 | + |
| 27 | + cl_git_pass(git_worktree_list(&wts, fixture.repo)); |
| 28 | + cl_assert_equal_i(wts.count, 1); |
| 29 | + cl_assert_equal_s(wts.strings[0], "testrepo-worktree"); |
| 30 | + |
| 31 | + git_strarray_free(&wts); |
| 32 | +} |
| 33 | + |
| 34 | +void test_worktree_worktree__list_with_invalid_worktree_dirs(void) |
| 35 | +{ |
| 36 | + const char *filesets[3][2] = { |
| 37 | + { "gitdir", "commondir" }, |
| 38 | + { "gitdir", "HEAD" }, |
| 39 | + { "HEAD", "commondir" }, |
| 40 | + }; |
| 41 | + git_buf path = GIT_BUF_INIT; |
| 42 | + git_strarray wts; |
| 43 | + unsigned i, j, len; |
| 44 | + |
| 45 | + cl_git_pass(git_buf_printf(&path, "%s/worktrees/invalid", |
| 46 | + fixture.repo->commondir)); |
| 47 | + cl_git_pass(p_mkdir(path.ptr, 0755)); |
| 48 | + |
| 49 | + len = path.size; |
| 50 | + |
| 51 | + for (i = 0; i < ARRAY_SIZE(filesets); i++) { |
| 52 | + |
| 53 | + for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) { |
| 54 | + git_buf_truncate(&path, len); |
| 55 | + cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j])); |
| 56 | + cl_git_pass(p_close(p_creat(path.ptr, 0644))); |
| 57 | + } |
| 58 | + |
| 59 | + cl_git_pass(git_worktree_list(&wts, fixture.worktree)); |
| 60 | + cl_assert_equal_i(wts.count, 1); |
| 61 | + cl_assert_equal_s(wts.strings[0], "testrepo-worktree"); |
| 62 | + git_strarray_free(&wts); |
| 63 | + |
| 64 | + for (j = 0; j < ARRAY_SIZE(filesets[i]); j++) { |
| 65 | + git_buf_truncate(&path, len); |
| 66 | + cl_git_pass(git_buf_joinpath(&path, path.ptr, filesets[i][j])); |
| 67 | + p_unlink(path.ptr); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + git_buf_free(&path); |
| 72 | +} |
| 73 | + |
| 74 | +void test_worktree_worktree__list_in_worktree_repo(void) |
| 75 | +{ |
| 76 | + git_strarray wts; |
| 77 | + |
| 78 | + cl_git_pass(git_worktree_list(&wts, fixture.worktree)); |
| 79 | + cl_assert_equal_i(wts.count, 1); |
| 80 | + cl_assert_equal_s(wts.strings[0], "testrepo-worktree"); |
| 81 | + |
| 82 | + git_strarray_free(&wts); |
| 83 | +} |
| 84 | + |
| 85 | +void test_worktree_worktree__list_bare(void) |
| 86 | +{ |
| 87 | + git_repository *repo; |
| 88 | + git_strarray wts; |
| 89 | + |
| 90 | + repo = cl_git_sandbox_init("testrepo.git"); |
| 91 | + cl_git_pass(git_worktree_list(&wts, repo)); |
| 92 | + cl_assert_equal_i(wts.count, 0); |
| 93 | + |
| 94 | + git_repository_free(repo); |
| 95 | +} |
| 96 | + |
| 97 | +void test_worktree_worktree__list_without_worktrees(void) |
| 98 | +{ |
| 99 | + git_repository *repo; |
| 100 | + git_strarray wts; |
| 101 | + |
| 102 | + repo = cl_git_sandbox_init("testrepo2"); |
| 103 | + cl_git_pass(git_worktree_list(&wts, repo)); |
| 104 | + cl_assert_equal_i(wts.count, 0); |
| 105 | + |
| 106 | + git_repository_free(repo); |
| 107 | +} |
0 commit comments