Blame tests/repo/config.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "sysdir.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include <ctype.h>
Packit Service 20376f
Packit Service 20376f
static git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
void test_repo_config__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_fixture_sandbox("empty_standard_repo");
Packit Service 20376f
	cl_git_pass(cl_rename(
Packit Service 20376f
		"empty_standard_repo/.gitted", "empty_standard_repo/.git"));
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(&path);
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir("alternate", 0777));
Packit Service 20376f
	cl_git_pass(git_path_prettify(&path, "alternate", NULL));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_repo_config__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_sandbox_set_search_path_defaults();
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(
Packit Service 20376f
		git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
	cl_assert(!git_path_isdir("alternate"));
Packit Service 20376f
Packit Service 20376f
	cl_fixture_cleanup("empty_standard_repo");
Packit Service 20376f
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_repo_config__can_open_global_when_there_is_no_file(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_config *config, *global;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	cl_git_pass(git_repository_config(&config, repo));
Packit Service 20376f
	cl_git_pass(git_config_open_level(
Packit Service 20376f
		&global, config, GIT_CONFIG_LEVEL_GLOBAL));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_config_set_string(global, "test.set", "42"));
Packit Service 20376f
Packit Service 20376f
	git_config_free(global);
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_repo_config__can_open_missing_global_with_separators(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_config *config, *global;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_printf(
Packit Service 20376f
		&path, "%c%s", GIT_PATH_LIST_SEPARATOR, "dummy"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	cl_git_pass(git_repository_config(&config, repo));
Packit Service 20376f
	cl_git_pass(git_config_open_level(
Packit Service 20376f
		&global, config, GIT_CONFIG_LEVEL_GLOBAL));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_config_set_string(global, "test.set", "42"));
Packit Service 20376f
Packit Service 20376f
	git_config_free(global);
Packit Service 20376f
	git_config_free(config);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
Packit Service 20376f
void test_repo_config__read_with_no_configs_at_all(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	int val;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
Packit Service 20376f
Packit Service 20376f
	/* with none */
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
Packit Service 20376f
	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(GIT_ABBREV_DEFAULT, val);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* with no local config, just system */
Packit Service 20376f
Packit Service 20376f
	cl_sandbox_set_search_path_defaults();
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir("alternate/1", 0777));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "1"));
Packit Service 20376f
	cl_git_rewritefile("alternate/1/gitconfig", "[core]\n\tabbrev = 10\n");
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(10, val);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* with just xdg + system */
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir("alternate/2", 0777));
Packit Service 20376f
	path.ptr[path.size - 1] = '2';
Packit Service 20376f
	cl_git_rewritefile("alternate/2/config", "[core]\n\tabbrev = 20\n");
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(20, val);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* with global + xdg + system */
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_mkdir("alternate/3", 0777));
Packit Service 20376f
	path.ptr[path.size - 1] = '3';
Packit Service 20376f
	cl_git_rewritefile("alternate/3/.gitconfig", "[core]\n\tabbrev = 30\n");
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(30, val);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* with all configs */
Packit Service 20376f
Packit Service 20376f
	cl_git_rewritefile("empty_standard_repo/.git/config", "[core]\n\tabbrev = 40\n");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(40, val);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* with all configs but delete the files ? */
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(40, val);
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_unlink("empty_standard_repo/.git/config"));
Packit Service 20376f
	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_unlink("alternate/1/gitconfig"));
Packit Service 20376f
	cl_assert(!git_path_isfile("alternate/1/gitconfig"));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_unlink("alternate/2/config"));
Packit Service 20376f
	cl_assert(!git_path_isfile("alternate/2/config"));
Packit Service 20376f
Packit Service 20376f
	cl_must_pass(p_unlink("alternate/3/.gitconfig"));
Packit Service 20376f
	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
Packit Service 20376f
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(40, val);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	/* reopen */
Packit Service 20376f
Packit Service 20376f
	cl_assert(!git_path_isfile("empty_standard_repo/.git/config"));
Packit Service 20376f
	cl_assert(!git_path_isfile("alternate/3/.gitconfig"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
Packit Service 20376f
	git_repository__cvar_cache_clear(repo);
Packit Service 20376f
	val = -1;
Packit Service 20376f
	cl_git_pass(git_repository__cvar(&val, repo, GIT_CVAR_ABBREV));
Packit Service 20376f
	cl_assert_equal_i(7, val);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	cl_assert(!git_path_exists("empty_standard_repo/.git/config"));
Packit Service 20376f
	cl_assert(!git_path_exists("alternate/3/.gitconfig"));
Packit Service 20376f
}