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