Skip to content

Commit d092112

Browse files
committed
tests: add a helper to build sandbox subpaths quickly
1 parent 83c35f7 commit d092112

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

tests/clar_libgit2.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,35 @@ const char* cl_git_path_url(const char *path)
319319
return url;
320320
}
321321

322+
const char *cl_git_sandbox_path(int is_dir, ...)
323+
{
324+
const char *path = NULL;
325+
static char _temp[GIT_PATH_MAX];
326+
git_buf buf = GIT_BUF_INIT;
327+
va_list arg;
328+
329+
cl_git_pass(git_buf_sets(&buf, clar_sandbox_path()));
330+
331+
va_start(arg, is_dir);
332+
333+
while ((path = va_arg(arg, const char *)) != NULL) {
334+
cl_git_pass(git_buf_joinpath(&buf, buf.ptr, path));
335+
}
336+
va_end(arg);
337+
338+
cl_git_pass(git_path_prettify(&buf, buf.ptr, NULL));
339+
if (is_dir)
340+
git_path_to_dir(&buf);
341+
342+
/* make sure we won't truncate */
343+
cl_assert(git_buf_len(&buf) < sizeof(_temp));
344+
git_buf_copy_cstr(_temp, sizeof(_temp), &buf);
345+
346+
git_buf_dispose(&buf);
347+
348+
return _temp;
349+
}
350+
322351
typedef struct {
323352
const char *filename;
324353
size_t filename_len;

tests/clar_libgit2.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ git_repository *cl_git_sandbox_init_new(const char *name);
181181
void cl_git_sandbox_cleanup(void);
182182
git_repository *cl_git_sandbox_reopen(void);
183183

184+
/*
185+
* build a sandbox-relative from path segments
186+
* is_dir will add a trailing slash
187+
* vararg must be a NULL-terminated char * list
188+
*/
189+
const char *cl_git_sandbox_path(int is_dir, ...);
190+
184191
/* Local-repo url helpers */
185192
const char* cl_git_fixture_url(const char *fixturename);
186193
const char* cl_git_path_url(const char *path);

tests/worktree/open.c

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,11 @@ static worktree_fixture fixture =
1111

1212
static void assert_worktree_valid(git_repository *wt, const char *parentdir, const char *wtdir)
1313
{
14-
git_buf path = GIT_BUF_INIT;
15-
1614
cl_assert(wt->is_worktree);
1715

18-
cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), wtdir));
19-
cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
20-
cl_git_pass(git_path_to_dir(&path));
21-
cl_assert_equal_s(wt->workdir, path.ptr);
22-
23-
cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
24-
cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
25-
cl_assert_equal_s(wt->gitlink, path.ptr);
26-
27-
cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), parentdir));
28-
cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
29-
cl_git_pass(git_buf_joinpath(&path, path.ptr, "worktrees"));
30-
cl_git_pass(git_buf_joinpath(&path, path.ptr, wtdir));
31-
cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
32-
cl_git_pass(git_path_to_dir(&path));
33-
cl_assert_equal_s(wt->gitdir, path.ptr);
34-
35-
git_buf_dispose(&path);
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));
3619
}
3720

3821
void test_worktree_open__initialize(void)

0 commit comments

Comments
 (0)