Blame tests/worktree/open.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "worktree.h"
Packit Service 20376f
#include "worktree_helpers.h"
Packit Service 20376f
Packit Service 20376f
#define COMMON_REPO "testrepo"
Packit Service 20376f
#define WORKTREE_REPO "testrepo-worktree"
Packit Service 20376f
Packit Service 20376f
static worktree_fixture fixture =
Packit Service 20376f
	WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO);
Packit Service 20376f
Packit Service 20376f
static void assert_worktree_valid(git_repository *wt, const char *parentdir, const char *wtdir)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_assert(wt->is_worktree);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), wtdir));
Packit Service 20376f
	cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
Packit Service 20376f
	cl_git_pass(git_path_to_dir(&path));
Packit Service 20376f
	cl_assert_equal_s(wt->workdir, path.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
Packit Service 20376f
	cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
Packit Service 20376f
	cl_assert_equal_s(wt->gitlink, path.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), parentdir));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr, ".git"));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "worktrees"));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr, wtdir));
Packit Service 20376f
	cl_git_pass(git_path_prettify(&path, path.ptr, NULL));
Packit Service 20376f
	cl_git_pass(git_path_to_dir(&path));
Packit Service 20376f
	cl_assert_equal_s(wt->gitdir, path.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	setup_fixture_worktree(&fixture);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	cleanup_fixture_worktree(&fixture);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__repository(void)
Packit Service 20376f
{
Packit Service 20376f
	assert_worktree_valid(fixture.worktree, COMMON_REPO, WORKTREE_REPO);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__repository_through_workdir(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *wt;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&wt, WORKTREE_REPO));
Packit Service 20376f
	assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
Packit Service 20376f
Packit Service 20376f
	git_repository_free(wt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__repository_through_gitlink(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *wt;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&wt, WORKTREE_REPO "/.git"));
Packit Service 20376f
	assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
Packit Service 20376f
Packit Service 20376f
	git_repository_free(wt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__repository_through_gitdir(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf gitdir_path = GIT_BUF_INIT;
Packit Service 20376f
	git_repository *wt;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&gitdir_path, COMMON_REPO, ".git"));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&gitdir_path, gitdir_path.ptr, "worktrees"));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&gitdir_path, gitdir_path.ptr, "testrepo-worktree"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&wt, gitdir_path.ptr));
Packit Service 20376f
	assert_worktree_valid(wt, COMMON_REPO, WORKTREE_REPO);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&gitdir_path);
Packit Service 20376f
	git_repository_free(wt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__open_discovered_worktree(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_discover(&path,
Packit Service 20376f
		git_repository_workdir(fixture.worktree), false, NULL));
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, path.ptr));
Packit Service 20376f
	cl_assert_equal_s(git_repository_workdir(fixture.worktree),
Packit Service 20376f
		git_repository_workdir(repo));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__repository_with_nonexistent_parent(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
Packit Service 20376f
	cleanup_fixture_worktree(&fixture);
Packit Service 20376f
Packit Service 20376f
	cl_fixture_sandbox(WORKTREE_REPO);
Packit Service 20376f
	cl_git_pass(p_chdir(WORKTREE_REPO));
Packit Service 20376f
	cl_git_pass(cl_rename(".gitted", ".git"));
Packit Service 20376f
	cl_git_pass(p_chdir(".."));
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_repository_open(&repo, WORKTREE_REPO));
Packit Service 20376f
Packit Service 20376f
	cl_fixture_cleanup(WORKTREE_REPO);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__open_from_repository(void)
Packit Service 20376f
{
Packit Service 20376f
	git_worktree *opened, *lookedup;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_worktree_open_from_repository(&opened, fixture.worktree));
Packit Service 20376f
	cl_git_pass(git_worktree_lookup(&lookedup, fixture.repo, WORKTREE_REPO));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(opened->name, lookedup->name);
Packit Service 20376f
	cl_assert_equal_s(opened->gitdir_path, lookedup->gitdir_path);
Packit Service 20376f
	cl_assert_equal_s(opened->gitlink_path, lookedup->gitlink_path);
Packit Service 20376f
	cl_assert_equal_s(opened->parent_path, lookedup->parent_path);
Packit Service 20376f
	cl_assert_equal_s(opened->commondir_path, lookedup->commondir_path);
Packit Service 20376f
	cl_assert_equal_i(opened->locked, lookedup->locked);
Packit Service 20376f
Packit Service 20376f
	git_worktree_free(opened);
Packit Service 20376f
	git_worktree_free(lookedup);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_worktree_open__open_from_nonworktree_fails(void)
Packit Service 20376f
{
Packit Service 20376f
	git_worktree *wt;
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_worktree_open_from_repository(&wt, fixture.repo));
Packit Service 20376f
}