Blame tests/submodule/open.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "submodule_helpers.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *g_parent;
Packit Service 20376f
static git_repository *g_child;
Packit Service 20376f
static git_submodule *g_module;
Packit Service 20376f
Packit Service 20376f
void test_submodule_open__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	g_parent = setup_fixture_submod2();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_submodule_open__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_submodule_free(g_module);
Packit Service 20376f
	git_repository_free(g_child);
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
	g_parent = NULL;
Packit Service 20376f
	g_child = NULL;
Packit Service 20376f
	g_module = NULL;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_sm_valid(git_repository *parent, git_repository *child, const char *sm_name)
Packit Service 20376f
{
Packit Service 20376f
	git_buf expected = GIT_BUF_INIT, actual = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* assert working directory */
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&expected, git_repository_workdir(parent), sm_name));
Packit Service 20376f
	cl_git_pass(git_path_prettify_dir(&expected, expected.ptr, NULL));
Packit Service 20376f
	cl_git_pass(git_buf_sets(&actual, git_repository_workdir(child)));
Packit Service 20376f
	cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL));
Packit Service 20376f
	cl_assert_equal_s(expected.ptr, actual.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(&expected);
Packit Service 20376f
	git_buf_clear(&actual);
Packit Service 20376f
Packit Service 20376f
	/* assert common directory */
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&expected, git_repository_commondir(parent), "modules"));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&expected, expected.ptr, sm_name));
Packit Service 20376f
	cl_git_pass(git_path_prettify_dir(&expected, expected.ptr, NULL));
Packit Service 20376f
	cl_git_pass(git_buf_sets(&actual, git_repository_commondir(child)));
Packit Service 20376f
	cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL));
Packit Service 20376f
	cl_assert_equal_s(expected.ptr, actual.ptr);
Packit Service 20376f
Packit Service 20376f
	/* assert git directory */
Packit Service 20376f
	cl_git_pass(git_buf_sets(&actual, git_repository_path(child)));
Packit Service 20376f
	cl_git_pass(git_path_prettify_dir(&actual, actual.ptr, NULL));
Packit Service 20376f
	cl_assert_equal_s(expected.ptr, actual.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&expected);
Packit Service 20376f
	git_buf_free(&actual);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_submodule_open__opening_via_lookup_succeeds(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_submodule_lookup(&g_module, g_parent, "sm_unchanged"));
Packit Service 20376f
	cl_git_pass(git_submodule_open(&g_child, g_module));
Packit Service 20376f
	assert_sm_valid(g_parent, g_child, "sm_unchanged");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_submodule_open__direct_open_succeeds(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_parent), "sm_unchanged"));
Packit Service 20376f
	cl_git_pass(git_repository_open(&g_child, path.ptr));
Packit Service 20376f
	assert_sm_valid(g_parent, g_child, "sm_unchanged");
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_submodule_open__direct_open_succeeds_for_broken_sm_with_gitdir(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * This is actually not a valid submodule, but we
Packit Service 20376f
	 * encountered at least one occasion where the gitdir
Packit Service 20376f
	 * file existed inside of a submodule's gitdir. As we are
Packit Service 20376f
	 * now able to open these submodules correctly, we still
Packit Service 20376f
	 * add a test for this.
Packit Service 20376f
	 */
Packit Service 20376f
	cl_git_mkfile("submod2/.git/modules/sm_unchanged/gitdir", ".git");
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_parent), "sm_unchanged"));
Packit Service 20376f
	cl_git_pass(git_repository_open(&g_child, path.ptr));
Packit Service 20376f
	assert_sm_valid(g_parent, g_child, "sm_unchanged");
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}