global.c 5.07 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3 4 5

void test_config_global__initialize(void)
{
6
	git_str path = GIT_STR_INIT;
7

8
	cl_git_pass(git_futils_mkdir_r("home", 0777));
9
	cl_git_pass(git_fs_path_prettify(&path, "home", NULL));
10 11 12
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));

13
	cl_git_pass(git_futils_mkdir_r("xdg/git", 0777));
14
	cl_git_pass(git_fs_path_prettify(&path, "xdg/git", NULL));
15
	cl_git_pass(git_libgit2_opts(
16
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
17

18
	cl_git_pass(git_futils_mkdir_r("etc", 0777));
19
	cl_git_pass(git_fs_path_prettify(&path, "etc", NULL));
20
	cl_git_pass(git_libgit2_opts(
21
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
22

23
	git_str_dispose(&path);
24 25 26 27
}

void test_config_global__cleanup(void)
{
28
	cl_sandbox_set_search_path_defaults();
29 30 31
	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));
	cl_git_pass(git_futils_rmdir_r("etc", NULL, GIT_RMDIR_REMOVE_FILES));
32 33 34 35 36
}

void test_config_global__open_global(void)
{
	git_config *cfg, *global, *selected, *dummy;
37 38 39
	int32_t value;

	cl_git_mkfile("home/.gitconfig", "[global]\n  test = 4567\n");
40 41

	cl_git_pass(git_config_open_default(&cfg));
42 43 44
	cl_git_pass(git_config_get_int32(&value, cfg, "global.test"));
	cl_assert_equal_i(4567, value);

45
	cl_git_pass(git_config_open_level(&global, cfg, GIT_CONFIG_LEVEL_GLOBAL));
46 47 48
	cl_git_pass(git_config_get_int32(&value, global, "global.test"));
	cl_assert_equal_i(4567, value);

49
	cl_git_fail(git_config_open_level(&dummy, cfg, GIT_CONFIG_LEVEL_XDG));
50

51
	cl_git_pass(git_config_open_global(&selected, cfg));
52 53
	cl_git_pass(git_config_get_int32(&value, selected, "global.test"));
	cl_assert_equal_i(4567, value);
54 55 56 57 58 59

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

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
void test_config_global__open_symlinked_global(void)
{
#ifndef GIT_WIN32
	git_config *cfg;
	int32_t value;

	cl_git_mkfile("home/.gitconfig.linked", "[global]\n  test = 4567\n");
	cl_must_pass(symlink(".gitconfig.linked", "home/.gitconfig"));

	cl_git_pass(git_config_open_default(&cfg));
	cl_git_pass(git_config_get_int32(&value, cfg, "global.test"));
	cl_assert_equal_i(4567, value);

	git_config_free(cfg);
#endif
}

77 78 79 80 81 82
void test_config_global__lock_missing_global_config(void)
{
	git_config *cfg;
	git_config_entry *entry;
	git_transaction *transaction;

83
	(void)p_unlink("home/.gitconfig"); /* No global config */
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

	cl_git_pass(git_config_open_default(&cfg));
	cl_git_pass(git_config_lock(&transaction, cfg));
	cl_git_pass(git_config_set_string(cfg, "assertion.fail", "boom"));
	cl_git_pass(git_transaction_commit(transaction));
	git_transaction_free(transaction);

	/* cfg is updated */
	cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail"));
	cl_assert_equal_s("boom", entry->value);

	git_config_entry_free(entry);
	git_config_free(cfg);

	/* We can reread the new value from the global config */
	cl_git_pass(git_config_open_default(&cfg));
	cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail"));
	cl_assert_equal_s("boom", entry->value);

	git_config_entry_free(entry);
	git_config_free(cfg);
}

107 108 109
void test_config_global__open_xdg(void)
{
	git_config *cfg, *xdg, *selected;
110
	const char *str = "teststring";
111
	const char *key = "this.variable";
112
	git_buf buf = {0};
113

114
	cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n  test = 1\n");
115 116 117 118 119 120

	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));
121 122
	cl_git_pass(git_config_get_string_buf(&buf, selected, key));
	cl_assert_equal_s(str, buf.ptr);
123

124
	git_buf_dispose(&buf);
125 126 127 128
	git_config_free(selected);
	git_config_free(xdg);
	git_config_free(cfg);
}
129 130 131 132 133

void test_config_global__open_programdata(void)
{
	git_config *cfg;
	git_repository *repo;
134 135
	git_buf dir_path = GIT_BUF_INIT;
	git_str config_path = GIT_STR_INIT;
136 137
	git_buf var_contents = GIT_BUF_INIT;

138
	if (cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE"))
139 140
		cl_skip();

141
	cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH,
142
		GIT_CONFIG_LEVEL_PROGRAMDATA, &dir_path));
143

144
	if (!git_fs_path_isdir(dir_path.ptr))
145
		cl_git_pass(p_mkdir(dir_path.ptr, 0777));
146

147
	cl_git_pass(git_str_joinpath(&config_path, dir_path.ptr, "config"));
148 149 150 151

	cl_git_pass(git_config_open_ondisk(&cfg, config_path.ptr));
	cl_git_pass(git_config_set_string(cfg, "programdata.var", "even higher level"));

152
	git_str_dispose(&config_path);
153 154 155 156 157 158 159
	git_config_free(cfg);

	git_config_open_default(&cfg);
	cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
	cl_assert_equal_s("even higher level", var_contents.ptr);

	git_config_free(cfg);
160
	git_buf_dispose(&var_contents);
161 162 163 164 165 166 167

	cl_git_pass(git_repository_init(&repo, "./foo.git", true));
	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
	cl_assert_equal_s("even higher level", var_contents.ptr);

	git_config_free(cfg);
168
	git_buf_dispose(&dir_path);
169
	git_buf_dispose(&var_contents);
170 171 172
	git_repository_free(repo);
	cl_fixture_cleanup("./foo.git");
}