]> git.proxmox.com Git - libgit2.git/blame - tests/config/global.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / config / global.c
CommitLineData
5d831887 1#include "clar_libgit2.h"
22a2d3d5 2#include "futils.h"
5d831887
CMN
3
4void test_config_global__initialize(void)
5{
e579e0f7 6 git_str path = GIT_STR_INIT;
5d831887 7
ac2fba0e 8 cl_git_pass(git_futils_mkdir_r("home", 0777));
e579e0f7 9 cl_git_pass(git_fs_path_prettify(&path, "home", NULL));
5d831887
CMN
10 cl_git_pass(git_libgit2_opts(
11 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, path.ptr));
12
ac2fba0e 13 cl_git_pass(git_futils_mkdir_r("xdg/git", 0777));
e579e0f7 14 cl_git_pass(git_fs_path_prettify(&path, "xdg/git", NULL));
5d831887 15 cl_git_pass(git_libgit2_opts(
1c8de380 16 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_XDG, path.ptr));
5d831887 17
ac2fba0e 18 cl_git_pass(git_futils_mkdir_r("etc", 0777));
e579e0f7 19 cl_git_pass(git_fs_path_prettify(&path, "etc", NULL));
5d831887 20 cl_git_pass(git_libgit2_opts(
1c8de380 21 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_SYSTEM, path.ptr));
5d831887 22
e579e0f7 23 git_str_dispose(&path);
5d831887
CMN
24}
25
26void test_config_global__cleanup(void)
27{
8487e237 28 cl_sandbox_set_search_path_defaults();
22a2d3d5
UG
29 cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));
30 cl_git_pass(git_futils_rmdir_r("xdg", NULL, GIT_RMDIR_REMOVE_FILES));
31 cl_git_pass(git_futils_rmdir_r("etc", NULL, GIT_RMDIR_REMOVE_FILES));
5d831887
CMN
32}
33
34void test_config_global__open_global(void)
35{
36 git_config *cfg, *global, *selected, *dummy;
22a2d3d5
UG
37 int32_t value;
38
39 cl_git_mkfile("home/.gitconfig", "[global]\n test = 4567\n");
5d831887
CMN
40
41 cl_git_pass(git_config_open_default(&cfg));
22a2d3d5
UG
42 cl_git_pass(git_config_get_int32(&value, cfg, "global.test"));
43 cl_assert_equal_i(4567, value);
44
5d831887 45 cl_git_pass(git_config_open_level(&global, cfg, GIT_CONFIG_LEVEL_GLOBAL));
22a2d3d5
UG
46 cl_git_pass(git_config_get_int32(&value, global, "global.test"));
47 cl_assert_equal_i(4567, value);
48
5d831887 49 cl_git_fail(git_config_open_level(&dummy, cfg, GIT_CONFIG_LEVEL_XDG));
22a2d3d5 50
5d831887 51 cl_git_pass(git_config_open_global(&selected, cfg));
22a2d3d5
UG
52 cl_git_pass(git_config_get_int32(&value, selected, "global.test"));
53 cl_assert_equal_i(4567, value);
5d831887
CMN
54
55 git_config_free(selected);
56 git_config_free(global);
57 git_config_free(cfg);
58}
59
22a2d3d5
UG
60void test_config_global__open_symlinked_global(void)
61{
62#ifndef GIT_WIN32
63 git_config *cfg;
64 int32_t value;
65
66 cl_git_mkfile("home/.gitconfig.linked", "[global]\n test = 4567\n");
67 cl_must_pass(symlink(".gitconfig.linked", "home/.gitconfig"));
68
69 cl_git_pass(git_config_open_default(&cfg));
70 cl_git_pass(git_config_get_int32(&value, cfg, "global.test"));
71 cl_assert_equal_i(4567, value);
72
73 git_config_free(cfg);
74#endif
75}
76
6147f643
PP
77void test_config_global__lock_missing_global_config(void)
78{
79 git_config *cfg;
80 git_config_entry *entry;
81 git_transaction *transaction;
82
22a2d3d5 83 (void)p_unlink("home/.gitconfig"); /* No global config */
6147f643
PP
84
85 cl_git_pass(git_config_open_default(&cfg));
86 cl_git_pass(git_config_lock(&transaction, cfg));
87 cl_git_pass(git_config_set_string(cfg, "assertion.fail", "boom"));
88 cl_git_pass(git_transaction_commit(transaction));
89 git_transaction_free(transaction);
90
91 /* cfg is updated */
92 cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail"));
93 cl_assert_equal_s("boom", entry->value);
94
95 git_config_entry_free(entry);
96 git_config_free(cfg);
97
98 /* We can reread the new value from the global config */
99 cl_git_pass(git_config_open_default(&cfg));
100 cl_git_pass(git_config_get_entry(&entry, cfg, "assertion.fail"));
101 cl_assert_equal_s("boom", entry->value);
102
103 git_config_entry_free(entry);
104 git_config_free(cfg);
105}
106
5d831887
CMN
107void test_config_global__open_xdg(void)
108{
109 git_config *cfg, *xdg, *selected;
9a97f49e 110 const char *str = "teststring";
5d831887 111 const char *key = "this.variable";
9a97f49e 112 git_buf buf = {0};
5d831887 113
1c8de380 114 cl_git_mkfile("xdg/git/config", "# XDG config\n[core]\n test = 1\n");
5d831887
CMN
115
116 cl_git_pass(git_config_open_default(&cfg));
117 cl_git_pass(git_config_open_level(&xdg, cfg, GIT_CONFIG_LEVEL_XDG));
118 cl_git_pass(git_config_open_global(&selected, cfg));
119
120 cl_git_pass(git_config_set_string(xdg, key, str));
9a97f49e
CMN
121 cl_git_pass(git_config_get_string_buf(&buf, selected, key));
122 cl_assert_equal_s(str, buf.ptr);
5d831887 123
ac3d33df 124 git_buf_dispose(&buf);
5d831887
CMN
125 git_config_free(selected);
126 git_config_free(xdg);
127 git_config_free(cfg);
128}
8c7c5fa5
CMN
129
130void test_config_global__open_programdata(void)
131{
8c7c5fa5
CMN
132 git_config *cfg;
133 git_repository *repo;
e579e0f7
MB
134 git_buf dir_path = GIT_BUF_INIT;
135 git_str config_path = GIT_STR_INIT;
8c7c5fa5
CMN
136 git_buf var_contents = GIT_BUF_INIT;
137
c8fab201 138 if (cl_is_env_set("GITTEST_INVASIVE_FS_STRUCTURE"))
8c7c5fa5
CMN
139 cl_skip();
140
6f7c4118 141 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH,
e579e0f7 142 GIT_CONFIG_LEVEL_PROGRAMDATA, &dir_path));
6f7c4118 143
e579e0f7
MB
144 if (!git_fs_path_isdir(dir_path.ptr))
145 cl_git_pass(p_mkdir(dir_path.ptr, 0777));
6f7c4118 146
e579e0f7 147 cl_git_pass(git_str_joinpath(&config_path, dir_path.ptr, "config"));
8c7c5fa5
CMN
148
149 cl_git_pass(git_config_open_ondisk(&cfg, config_path.ptr));
150 cl_git_pass(git_config_set_string(cfg, "programdata.var", "even higher level"));
151
e579e0f7 152 git_str_dispose(&config_path);
8c7c5fa5
CMN
153 git_config_free(cfg);
154
155 git_config_open_default(&cfg);
156 cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
157 cl_assert_equal_s("even higher level", var_contents.ptr);
158
159 git_config_free(cfg);
ac3d33df 160 git_buf_dispose(&var_contents);
8c7c5fa5
CMN
161
162 cl_git_pass(git_repository_init(&repo, "./foo.git", true));
163 cl_git_pass(git_repository_config(&cfg, repo));
164 cl_git_pass(git_config_get_string_buf(&var_contents, cfg, "programdata.var"));
165 cl_assert_equal_s("even higher level", var_contents.ptr);
166
167 git_config_free(cfg);
e579e0f7 168 git_buf_dispose(&dir_path);
ac3d33df 169 git_buf_dispose(&var_contents);
8c7c5fa5
CMN
170 git_repository_free(repo);
171 cl_fixture_cleanup("./foo.git");
172}