global.c 2.26 KB
Newer Older
1 2 3 4 5 6 7 8
#include "clar_libgit2.h"
#include "buffer.h"
#include "fileops.h"

void test_config_global__initialize(void)
{
	git_buf path = GIT_BUF_INIT;

9
	cl_assert_equal_i(0, p_mkdir("home", 0777));
10 11 12 13
	cl_git_pass(git_path_prettify(&path, "home", NULL));
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));

14 15 16
	cl_assert_equal_i(0, p_mkdir("xdg", 0777));
	cl_assert_equal_i(0, p_mkdir("xdg/git", 0777));
	cl_git_pass(git_path_prettify(&path, "xdg/git", NULL));
17
	cl_git_pass(git_libgit2_opts(
18
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
19

20 21
	cl_assert_equal_i(0, p_mkdir("etc", 0777));
	cl_git_pass(git_path_prettify(&path, "etc", NULL));
22
	cl_git_pass(git_libgit2_opts(
23
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
24 25 26 27 28 29 30 31

	git_buf_free(&path);
}

void test_config_global__cleanup(void)
{
	cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
	cl_git_pass(git_futils_rmdir_r("xdg", NULL, GIT_RMDIR_REMOVE_FILES));
32 33 34 35 36
	cl_git_pass(git_futils_rmdir_r("etc", NULL, GIT_RMDIR_REMOVE_FILES));

	git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, NULL);
	git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, NULL);
	git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL);
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
}

void test_config_global__open_global(void)
{
	git_config *cfg, *global, *selected, *dummy;

	cl_git_pass(git_config_open_default(&cfg));
	cl_git_pass(git_config_open_level(&global, cfg, GIT_CONFIG_LEVEL_GLOBAL));
	cl_git_fail(git_config_open_level(&dummy, cfg, GIT_CONFIG_LEVEL_XDG));
	cl_git_pass(git_config_open_global(&selected, cfg));

	git_config_free(selected);
	git_config_free(global);
	git_config_free(cfg);
}

void test_config_global__open_xdg(void)
{
	git_config *cfg, *xdg, *selected;
	const char *val, *str = "teststring";
	const char *key = "this.variable";

59
	cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n  test = 1\n");
60 61 62 63 64 65 66 67 68 69 70 71 72

	cl_git_pass(git_config_open_default(&cfg));
	cl_git_pass(git_config_open_level(&xdg, cfg, GIT_CONFIG_LEVEL_XDG));
	cl_git_pass(git_config_open_global(&selected, cfg));

	cl_git_pass(git_config_set_string(xdg, key, str));
	cl_git_pass(git_config_get_string(&val, selected, key));
	cl_assert_equal_s(str, val);

	git_config_free(selected);
	git_config_free(xdg);
	git_config_free(cfg);
}