Blame tests/submodule/open.c

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