]>
git.proxmox.com Git - libgit2.git/blob - tests/libgit2/worktree/open.c
1 #include "clar_libgit2.h"
2 #include "repository.h"
4 #include "worktree_helpers.h"
6 #define COMMON_REPO "testrepo"
7 #define WORKTREE_REPO "testrepo-worktree"
9 static worktree_fixture fixture
=
10 WORKTREE_FIXTURE_INIT(COMMON_REPO
, WORKTREE_REPO
);
12 static void assert_worktree_valid(git_repository
*wt
, const char *parentdir
, const char *wtdir
)
14 cl_assert(wt
->is_worktree
);
16 cl_assert_equal_s(wt
->workdir
, cl_git_sandbox_path(1, wtdir
, NULL
));
17 cl_assert_equal_s(wt
->gitlink
, cl_git_sandbox_path(0, wtdir
, ".git", NULL
));
18 cl_assert_equal_s(wt
->gitdir
, cl_git_sandbox_path(1, parentdir
, ".git", "worktrees", wtdir
, NULL
));
21 void test_worktree_open__initialize(void)
23 setup_fixture_worktree(&fixture
);
26 void test_worktree_open__cleanup(void)
28 cleanup_fixture_worktree(&fixture
);
31 void test_worktree_open__repository(void)
33 assert_worktree_valid(fixture
.worktree
, COMMON_REPO
, WORKTREE_REPO
);
36 void test_worktree_open__repository_through_workdir(void)
40 cl_git_pass(git_repository_open(&wt
, WORKTREE_REPO
));
41 assert_worktree_valid(wt
, COMMON_REPO
, WORKTREE_REPO
);
43 git_repository_free(wt
);
46 void test_worktree_open__repository_through_gitlink(void)
50 cl_git_pass(git_repository_open(&wt
, WORKTREE_REPO
"/.git"));
51 assert_worktree_valid(wt
, COMMON_REPO
, WORKTREE_REPO
);
53 git_repository_free(wt
);
56 void test_worktree_open__repository_through_gitdir(void)
58 git_str gitdir_path
= GIT_STR_INIT
;
61 cl_git_pass(git_str_joinpath(&gitdir_path
, COMMON_REPO
, ".git"));
62 cl_git_pass(git_str_joinpath(&gitdir_path
, gitdir_path
.ptr
, "worktrees"));
63 cl_git_pass(git_str_joinpath(&gitdir_path
, gitdir_path
.ptr
, "testrepo-worktree"));
65 cl_git_pass(git_repository_open(&wt
, gitdir_path
.ptr
));
66 assert_worktree_valid(wt
, COMMON_REPO
, WORKTREE_REPO
);
68 git_str_dispose(&gitdir_path
);
69 git_repository_free(wt
);
72 void test_worktree_open__open_discovered_worktree(void)
74 git_buf path
= GIT_BUF_INIT
;
77 cl_git_pass(git_repository_discover(&path
,
78 git_repository_workdir(fixture
.worktree
), false, NULL
));
79 cl_git_pass(git_repository_open(&repo
, path
.ptr
));
80 cl_assert_equal_s(git_repository_workdir(fixture
.worktree
),
81 git_repository_workdir(repo
));
83 git_buf_dispose(&path
);
84 git_repository_free(repo
);
87 void test_worktree_open__repository_with_nonexistent_parent(void)
91 cleanup_fixture_worktree(&fixture
);
93 cl_fixture_sandbox(WORKTREE_REPO
);
94 cl_git_pass(p_chdir(WORKTREE_REPO
));
95 cl_git_pass(cl_rename(".gitted", ".git"));
96 cl_git_pass(p_chdir(".."));
98 cl_git_fail(git_repository_open(&repo
, WORKTREE_REPO
));
100 cl_fixture_cleanup(WORKTREE_REPO
);
103 void test_worktree_open__open_from_repository(void)
105 git_worktree
*opened
, *lookedup
;
107 cl_git_pass(git_worktree_open_from_repository(&opened
, fixture
.worktree
));
108 cl_git_pass(git_worktree_lookup(&lookedup
, fixture
.repo
, WORKTREE_REPO
));
110 cl_assert_equal_s(opened
->name
, lookedup
->name
);
111 cl_assert_equal_s(opened
->gitdir_path
, lookedup
->gitdir_path
);
112 cl_assert_equal_s(opened
->gitlink_path
, lookedup
->gitlink_path
);
113 cl_assert_equal_s(opened
->parent_path
, lookedup
->parent_path
);
114 cl_assert_equal_s(opened
->commondir_path
, lookedup
->commondir_path
);
115 cl_assert_equal_i(opened
->locked
, lookedup
->locked
);
117 git_worktree_free(opened
);
118 git_worktree_free(lookedup
);
121 void test_worktree_open__open_from_nonworktree_fails(void)
125 cl_git_fail(git_worktree_open_from_repository(&wt
, fixture
.repo
));