Skip to content

Commit 3f3a4ce

Browse files
committed
worktree: test opening worktree via gitlink, gitdir and worktree
1 parent 6f6dd17 commit 3f3a4ce

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

tests/worktree/open.c

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,91 @@
88
#define COMMON_REPO "testrepo"
99
#define WORKTREE_REPO "testrepo-worktree"
1010

11+
static void assert_worktree_valid(git_repository *wt, const char *parentdir, const char *wtdir)
12+
{
13+
git_buf path = GIT_BUF_INIT;
14+
15+
cl_assert(wt->is_worktree);
16+
17+
cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), wtdir));
18+
cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
19+
cl_git_pass(git_path_to_dir(&path));
20+
cl_assert_equal_s(wt->workdir, path.ptr);
21+
22+
cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
23+
cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
24+
cl_assert_equal_s(wt->gitlink, path.ptr);
25+
26+
cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), parentdir));
27+
cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
28+
cl_git_pass(git_buf_joinpath(&path, path.ptr, "worktrees"));
29+
cl_git_pass(git_buf_joinpath(&path, path.ptr, wtdir));
30+
cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
31+
cl_git_pass(git_path_to_dir(&path));
32+
cl_assert_equal_s(wt->gitdir, path.ptr);
33+
34+
git_buf_free(&path);
35+
}
36+
1137
void test_worktree_open__repository(void)
1238
{
1339
worktree_fixture fixture =
1440
WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
1541
setup_fixture_worktree(&fixture);
1642

17-
cl_assert(git_repository_path(fixture.worktree) != NULL);
18-
cl_assert(git_repository_workdir(fixture.worktree) != NULL);
43+
assert_worktree_valid(fixture.worktree, COMMON_REPO, WORKTREE_REPO);
1944

20-
cl_assert(!fixture.repo->is_worktree);
21-
cl_assert(fixture.worktree->is_worktree);
45+
cleanup_fixture_worktree(&fixture);
46+
}
47+
48+
void test_worktree_open__repository_through_workdir(void)
49+
{
50+
worktree_fixture fixture =
51+
WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
52+
git_repository *wt;
53+
54+
setup_fixture_worktree(&fixture);
55+
56+
cl_git_pass(git_repository_open(&wt, WORKTREE_REPO));
57+
assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
58+
59+
git_repository_free(wt);
60+
cleanup_fixture_worktree(&fixture);
61+
}
62+
63+
void test_worktree_open__repository_through_gitlink(void)
64+
{
65+
worktree_fixture fixture =
66+
WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
67+
git_repository *wt;
68+
69+
setup_fixture_worktree(&fixture);
70+
71+
cl_git_pass(git_repository_open(&wt, WORKTREE_REPO "/.git"));
72+
assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
73+
74+
git_repository_free(wt);
75+
cleanup_fixture_worktree(&fixture);
76+
}
77+
78+
void test_worktree_open__repository_through_gitdir(void)
79+
{
80+
worktree_fixture fixture =
81+
WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
82+
git_buf gitdir_path = GIT_BUF_INIT;
83+
git_repository *wt;
84+
85+
setup_fixture_worktree(&fixture);
86+
87+
cl_git_pass(git_buf_joinpath(&gitdir_path, COMMON_REPO, ".git"));
88+
cl_git_pass(git_buf_joinpath(&gitdir_path, gitdir_path.ptr, "worktrees"));
89+
cl_git_pass(git_buf_joinpath(&gitdir_path, gitdir_path.ptr, "testrepo-worktree"));
90+
91+
cl_git_pass(git_repository_open(&wt, gitdir_path.ptr));
92+
assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
2293

94+
git_buf_free(&gitdir_path);
95+
git_repository_free(wt);
2396
cleanup_fixture_worktree(&fixture);
2497
}
2598

0 commit comments

Comments
 (0)