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