]> git.proxmox.com Git - libgit2.git/blob - tests/config/write.c
Merge pull request #1962 from libgit2/rename-tests
[libgit2.git] / tests / config / write.c
1 #include "clar_libgit2.h"
2
3 void test_config_write__initialize(void)
4 {
5 cl_fixture_sandbox("config/config9");
6 cl_fixture_sandbox("config/config15");
7 cl_fixture_sandbox("config/config17");
8 }
9
10 void test_config_write__cleanup(void)
11 {
12 cl_fixture_cleanup("config9");
13 cl_fixture_cleanup("config15");
14 cl_fixture_cleanup("config17");
15 }
16
17 void test_config_write__replace_value(void)
18 {
19 git_config *cfg;
20 int i;
21 int64_t l, expected = +9223372036854775803;
22
23 /* By freeing the config, we make sure we flush the values */
24 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
25 cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5));
26 git_config_free(cfg);
27
28 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
29 cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy"));
30 cl_assert(i == 5);
31 git_config_free(cfg);
32
33 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
34 cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1));
35 git_config_free(cfg);
36
37 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
38 cl_git_pass(git_config_set_int64(cfg, "core.verylong", expected));
39 git_config_free(cfg);
40
41 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
42 cl_git_pass(git_config_get_int64(&l, cfg, "core.verylong"));
43 cl_assert(l == expected);
44 git_config_free(cfg);
45
46 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
47 cl_must_fail(git_config_get_int32(&i, cfg, "core.verylong"));
48 git_config_free(cfg);
49
50 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
51 cl_git_pass(git_config_set_int64(cfg, "core.verylong", 1));
52 git_config_free(cfg);
53 }
54
55 void test_config_write__delete_value(void)
56 {
57 git_config *cfg;
58 int32_t i;
59
60 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
61 cl_git_pass(git_config_set_int32(cfg, "core.dummy", 5));
62 git_config_free(cfg);
63
64 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
65 cl_git_pass(git_config_delete_entry(cfg, "core.dummy"));
66 git_config_free(cfg);
67
68 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
69 cl_assert(git_config_get_int32(&i, cfg, "core.dummy") == GIT_ENOTFOUND);
70 cl_git_pass(git_config_set_int32(cfg, "core.dummy", 1));
71 git_config_free(cfg);
72 }
73
74 /*
75 * At the beginning of the test:
76 * - config9 has: core.dummy2=42
77 * - config15 has: core.dummy2=7
78 */
79 void test_config_write__delete_value_at_specific_level(void)
80 {
81 git_config *cfg, *cfg_specific;
82 int32_t i;
83
84 cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
85 cl_git_pass(git_config_get_int32(&i, cfg, "core.dummy2"));
86 cl_assert(i == 7);
87 git_config_free(cfg);
88
89 cl_git_pass(git_config_new(&cfg));
90 cl_git_pass(git_config_add_file_ondisk(cfg, "config9",
91 GIT_CONFIG_LEVEL_LOCAL, 0));
92 cl_git_pass(git_config_add_file_ondisk(cfg, "config15",
93 GIT_CONFIG_LEVEL_GLOBAL, 0));
94
95 cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));
96
97 cl_git_pass(git_config_delete_entry(cfg_specific, "core.dummy2"));
98 git_config_free(cfg);
99
100 cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
101 cl_assert(git_config_get_int32(&i, cfg, "core.dummy2") == GIT_ENOTFOUND);
102 cl_git_pass(git_config_set_int32(cfg, "core.dummy2", 7));
103
104 git_config_free(cfg_specific);
105 git_config_free(cfg);
106 }
107
108 void test_config_write__write_subsection(void)
109 {
110 git_config *cfg;
111 const char *str;
112
113 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
114 cl_git_pass(git_config_set_string(cfg, "my.own.var", "works"));
115 git_config_free(cfg);
116
117 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
118 cl_git_pass(git_config_get_string(&str, cfg, "my.own.var"));
119 cl_assert_equal_s("works", str);
120 git_config_free(cfg);
121 }
122
123 void test_config_write__delete_inexistent(void)
124 {
125 git_config *cfg;
126
127 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
128 cl_assert(git_config_delete_entry(cfg, "core.imaginary") == GIT_ENOTFOUND);
129 git_config_free(cfg);
130 }
131
132 void test_config_write__value_containing_quotes(void)
133 {
134 git_config *cfg;
135 const char* str;
136
137 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
138 cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes"));
139 cl_git_pass(git_config_get_string(&str, cfg, "core.somevar"));
140 cl_assert_equal_s(str, "this \"has\" quotes");
141 git_config_free(cfg);
142
143 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
144 cl_git_pass(git_config_get_string(&str, cfg, "core.somevar"));
145 cl_assert_equal_s(str, "this \"has\" quotes");
146 git_config_free(cfg);
147
148 /* The code path for values that already exist is different, check that one as well */
149 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
150 cl_git_pass(git_config_set_string(cfg, "core.somevar", "this also \"has\" quotes"));
151 cl_git_pass(git_config_get_string(&str, cfg, "core.somevar"));
152 cl_assert_equal_s(str, "this also \"has\" quotes");
153 git_config_free(cfg);
154
155 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
156 cl_git_pass(git_config_get_string(&str, cfg, "core.somevar"));
157 cl_assert_equal_s(str, "this also \"has\" quotes");
158 git_config_free(cfg);
159 }
160
161 void test_config_write__escape_value(void)
162 {
163 git_config *cfg;
164 const char* str;
165
166 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
167 cl_git_pass(git_config_set_string(cfg, "core.somevar", "this \"has\" quotes and \t"));
168 cl_git_pass(git_config_get_string(&str, cfg, "core.somevar"));
169 cl_assert_equal_s(str, "this \"has\" quotes and \t");
170 git_config_free(cfg);
171
172 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
173 cl_git_pass(git_config_get_string(&str, cfg, "core.somevar"));
174 cl_assert_equal_s(str, "this \"has\" quotes and \t");
175 git_config_free(cfg);
176 }
177
178 void test_config_write__add_value_at_specific_level(void)
179 {
180 git_config *cfg, *cfg_specific;
181 int i;
182 int64_t l, expected = +9223372036854775803;
183 const char *s;
184
185 // open config15 as global level config file
186 cl_git_pass(git_config_new(&cfg));
187 cl_git_pass(git_config_add_file_ondisk(cfg, "config9",
188 GIT_CONFIG_LEVEL_LOCAL, 0));
189 cl_git_pass(git_config_add_file_ondisk(cfg, "config15",
190 GIT_CONFIG_LEVEL_GLOBAL, 0));
191
192 cl_git_pass(git_config_open_level(&cfg_specific, cfg, GIT_CONFIG_LEVEL_GLOBAL));
193
194 cl_git_pass(git_config_set_int32(cfg_specific, "core.int32global", 28));
195 cl_git_pass(git_config_set_int64(cfg_specific, "core.int64global", expected));
196 cl_git_pass(git_config_set_bool(cfg_specific, "core.boolglobal", true));
197 cl_git_pass(git_config_set_string(cfg_specific, "core.stringglobal", "I'm a global config value!"));
198 git_config_free(cfg_specific);
199 git_config_free(cfg);
200
201 // open config15 as local level config file
202 cl_git_pass(git_config_open_ondisk(&cfg, "config15"));
203
204 cl_git_pass(git_config_get_int32(&i, cfg, "core.int32global"));
205 cl_assert_equal_i(28, i);
206 cl_git_pass(git_config_get_int64(&l, cfg, "core.int64global"));
207 cl_assert(l == expected);
208 cl_git_pass(git_config_get_bool(&i, cfg, "core.boolglobal"));
209 cl_assert_equal_b(true, i);
210 cl_git_pass(git_config_get_string(&s, cfg, "core.stringglobal"));
211 cl_assert_equal_s("I'm a global config value!", s);
212
213 git_config_free(cfg);
214 }
215
216 void test_config_write__add_value_at_file_with_no_clrf_at_the_end(void)
217 {
218 git_config *cfg;
219 int i;
220
221 cl_git_pass(git_config_open_ondisk(&cfg, "config17"));
222 cl_git_pass(git_config_set_int32(cfg, "core.newline", 7));
223 git_config_free(cfg);
224
225 cl_git_pass(git_config_open_ondisk(&cfg, "config17"));
226 cl_git_pass(git_config_get_int32(&i, cfg, "core.newline"));
227 cl_assert_equal_i(7, i);
228
229 git_config_free(cfg);
230 }
231
232 void test_config_write__add_value_which_needs_quotes(void)
233 {
234 git_config *cfg;
235 const char* str1;
236 const char* str2;
237 const char* str3;
238 const char* str4;
239 const char* str5;
240
241 cl_git_pass(git_config_open_ondisk(&cfg, "config17"));
242 cl_git_pass(git_config_set_string(cfg, "core.startwithspace", " Something"));
243 cl_git_pass(git_config_set_string(cfg, "core.endwithspace", "Something "));
244 cl_git_pass(git_config_set_string(cfg, "core.containscommentchar1", "some#thing"));
245 cl_git_pass(git_config_set_string(cfg, "core.containscommentchar2", "some;thing"));
246 cl_git_pass(git_config_set_string(cfg, "core.startwhithsapceandcontainsdoublequote", " some\"thing"));
247 git_config_free(cfg);
248
249 cl_git_pass(git_config_open_ondisk(&cfg, "config17"));
250 cl_git_pass(git_config_get_string(&str1, cfg, "core.startwithspace"));
251 cl_assert_equal_s(" Something", str1);
252 cl_git_pass(git_config_get_string(&str2, cfg, "core.endwithspace"));
253 cl_assert_equal_s("Something ", str2);
254 cl_git_pass(git_config_get_string(&str3, cfg, "core.containscommentchar1"));
255 cl_assert_equal_s("some#thing", str3);
256 cl_git_pass(git_config_get_string(&str4, cfg, "core.containscommentchar2"));
257 cl_assert_equal_s("some;thing", str4);
258 cl_git_pass(git_config_get_string(&str5, cfg, "core.startwhithsapceandcontainsdoublequote"));
259 cl_assert_equal_s(" some\"thing", str5);
260 git_config_free(cfg);
261 }
262
263 void test_config_write__can_set_a_value_to_NULL(void)
264 {
265 git_repository *repository;
266 git_config *config;
267
268 repository = cl_git_sandbox_init("testrepo.git");
269
270 cl_git_pass(git_repository_config(&config, repository));
271 cl_git_fail(git_config_set_string(config, "a.b.c", NULL));
272 git_config_free(config);
273
274 cl_git_sandbox_cleanup();
275 }
276
277 void test_config_write__can_set_an_empty_value(void)
278 {
279 git_repository *repository;
280 git_config *config;
281 const char * str;
282
283 repository = cl_git_sandbox_init("testrepo.git");
284 cl_git_pass(git_repository_config(&config, repository));
285
286 cl_git_pass(git_config_set_string(config, "core.somevar", ""));
287 cl_git_pass(git_config_get_string(&str, config, "core.somevar"));
288 cl_assert_equal_s(str, "");
289
290 git_config_free(config);
291 cl_git_sandbox_cleanup();
292 }
293
294 void test_config_write__updating_a_locked_config_file_returns_ELOCKED(void)
295 {
296 git_config *cfg;
297
298 cl_git_pass(git_config_open_ondisk(&cfg, "config9"));
299
300 cl_git_mkfile("config9.lock", "[core]\n");
301
302 cl_git_fail_with(git_config_set_string(cfg, "core.dump", "boom"), GIT_ELOCKED);
303
304 git_config_free(cfg);
305 }