]>
git.proxmox.com Git - libgit2.git/blob - tests/config/write.c
1 #include "clar_libgit2.h"
5 void test_config_write__initialize(void)
7 cl_fixture_sandbox("config/config9");
8 cl_fixture_sandbox("config/config15");
9 cl_fixture_sandbox("config/config17");
12 void test_config_write__cleanup(void)
14 cl_fixture_cleanup("config9");
15 cl_fixture_cleanup("config15");
16 cl_fixture_cleanup("config17");
19 void test_config_write__replace_value(void)
23 int64_t l
, expected
= +9223372036854775803;
25 /* By freeing the config, we make sure we flush the values */
26 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
27 cl_git_pass(git_config_set_int32(cfg
, "core.dummy", 5));
30 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
31 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.dummy"));
35 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
36 cl_git_pass(git_config_set_int32(cfg
, "core.dummy", 1));
39 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
40 cl_git_pass(git_config_set_int64(cfg
, "core.verylong", expected
));
43 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
44 cl_git_pass(git_config_get_int64(&l
, cfg
, "core.verylong"));
45 cl_assert(l
== expected
);
48 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
49 cl_must_fail(git_config_get_int32(&i
, cfg
, "core.verylong"));
52 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
53 cl_git_pass(git_config_set_int64(cfg
, "core.verylong", 1));
57 void test_config_write__delete_value(void)
62 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
63 cl_git_pass(git_config_set_int32(cfg
, "core.dummy", 5));
66 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
67 cl_git_pass(git_config_delete_entry(cfg
, "core.dummy"));
70 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
71 cl_assert(git_config_get_int32(&i
, cfg
, "core.dummy") == GIT_ENOTFOUND
);
72 cl_git_pass(git_config_set_int32(cfg
, "core.dummy", 1));
77 * At the beginning of the test:
78 * - config9 has: core.dummy2=42
79 * - config15 has: core.dummy2=7
81 void test_config_write__delete_value_at_specific_level(void)
83 git_config
*cfg
, *cfg_specific
;
86 cl_git_pass(git_config_open_ondisk(&cfg
, "config15"));
87 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.dummy2"));
91 cl_git_pass(git_config_new(&cfg
));
92 cl_git_pass(git_config_add_file_ondisk(cfg
, "config9",
93 GIT_CONFIG_LEVEL_LOCAL
, 0));
94 cl_git_pass(git_config_add_file_ondisk(cfg
, "config15",
95 GIT_CONFIG_LEVEL_GLOBAL
, 0));
97 cl_git_pass(git_config_open_level(&cfg_specific
, cfg
, GIT_CONFIG_LEVEL_GLOBAL
));
99 cl_git_pass(git_config_delete_entry(cfg_specific
, "core.dummy2"));
100 git_config_free(cfg
);
102 cl_git_pass(git_config_open_ondisk(&cfg
, "config15"));
103 cl_assert(git_config_get_int32(&i
, cfg
, "core.dummy2") == GIT_ENOTFOUND
);
104 cl_git_pass(git_config_set_int32(cfg
, "core.dummy2", 7));
106 git_config_free(cfg_specific
);
107 git_config_free(cfg
);
111 * This test exposes a bug where duplicate empty section headers could prevent
112 * deletion of config entries.
114 void test_config_write__delete_value_with_duplicate_header(void)
116 const char *file_name
= "config-duplicate-header";
117 const char *entry_name
= "remote.origin.url";
119 git_config_entry
*entry
;
121 /* This config can occur after removing and re-adding the origin remote */
122 const char *file_content
=
123 "[remote \"origin\"]\n" \
124 "[branch \"master\"]\n" \
125 " remote = \"origin\"\n" \
126 "[remote \"origin\"]\n" \
129 /* Write the test config and make sure the expected entry exists */
130 cl_git_mkfile(file_name
, file_content
);
131 cl_git_pass(git_config_open_ondisk(&cfg
, file_name
));
132 cl_git_pass(git_config_get_entry(&entry
, cfg
, entry_name
));
134 /* Delete that entry */
135 cl_git_pass(git_config_delete_entry(cfg
, entry_name
));
137 /* Reopen the file and make sure the entry no longer exists */
138 git_config_entry_free(entry
);
139 git_config_free(cfg
);
140 cl_git_pass(git_config_open_ondisk(&cfg
, file_name
));
141 cl_git_fail(git_config_get_entry(&entry
, cfg
, entry_name
));
144 git_config_entry_free(entry
);
145 git_config_free(cfg
);
148 void test_config_write__write_subsection(void)
151 git_buf buf
= GIT_BUF_INIT
;
153 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
154 cl_git_pass(git_config_set_string(cfg
, "my.own.var", "works"));
155 git_config_free(cfg
);
157 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
158 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "my.own.var"));
159 cl_assert_equal_s("works", git_buf_cstr(&buf
));
162 git_config_free(cfg
);
165 void test_config_write__delete_inexistent(void)
169 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
170 cl_assert(git_config_delete_entry(cfg
, "core.imaginary") == GIT_ENOTFOUND
);
171 git_config_free(cfg
);
174 void test_config_write__value_containing_quotes(void)
177 git_buf buf
= GIT_BUF_INIT
;
179 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
180 cl_git_pass(git_config_set_string(cfg
, "core.somevar", "this \"has\" quotes"));
181 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "core.somevar"));
182 cl_assert_equal_s("this \"has\" quotes", git_buf_cstr(&buf
));
184 git_config_free(cfg
);
186 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
187 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "core.somevar"));
188 cl_assert_equal_s("this \"has\" quotes", git_buf_cstr(&buf
));
190 git_config_free(cfg
);
192 /* The code path for values that already exist is different, check that one as well */
193 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
194 cl_git_pass(git_config_set_string(cfg
, "core.somevar", "this also \"has\" quotes"));
195 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "core.somevar"));
196 cl_assert_equal_s("this also \"has\" quotes", git_buf_cstr(&buf
));
198 git_config_free(cfg
);
200 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
201 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "core.somevar"));
202 cl_assert_equal_s("this also \"has\" quotes", git_buf_cstr(&buf
));
204 git_config_free(cfg
);
207 void test_config_write__escape_value(void)
210 git_buf buf
= GIT_BUF_INIT
;
212 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
213 cl_git_pass(git_config_set_string(cfg
, "core.somevar", "this \"has\" quotes and \t"));
214 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "core.somevar"));
215 cl_assert_equal_s("this \"has\" quotes and \t", git_buf_cstr(&buf
));
217 git_config_free(cfg
);
219 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
220 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "core.somevar"));
221 cl_assert_equal_s("this \"has\" quotes and \t", git_buf_cstr(&buf
));
223 git_config_free(cfg
);
226 void test_config_write__add_value_at_specific_level(void)
228 git_config
*cfg
, *cfg_specific
;
230 int64_t l
, expected
= +9223372036854775803;
231 git_buf buf
= GIT_BUF_INIT
;
233 // open config15 as global level config file
234 cl_git_pass(git_config_new(&cfg
));
235 cl_git_pass(git_config_add_file_ondisk(cfg
, "config9",
236 GIT_CONFIG_LEVEL_LOCAL
, 0));
237 cl_git_pass(git_config_add_file_ondisk(cfg
, "config15",
238 GIT_CONFIG_LEVEL_GLOBAL
, 0));
240 cl_git_pass(git_config_open_level(&cfg_specific
, cfg
, GIT_CONFIG_LEVEL_GLOBAL
));
242 cl_git_pass(git_config_set_int32(cfg_specific
, "core.int32global", 28));
243 cl_git_pass(git_config_set_int64(cfg_specific
, "core.int64global", expected
));
244 cl_git_pass(git_config_set_bool(cfg_specific
, "core.boolglobal", true));
245 cl_git_pass(git_config_set_string(cfg_specific
, "core.stringglobal", "I'm a global config value!"));
246 git_config_free(cfg_specific
);
247 git_config_free(cfg
);
249 // open config15 as local level config file
250 cl_git_pass(git_config_open_ondisk(&cfg
, "config15"));
252 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.int32global"));
253 cl_assert_equal_i(28, i
);
254 cl_git_pass(git_config_get_int64(&l
, cfg
, "core.int64global"));
255 cl_assert(l
== expected
);
256 cl_git_pass(git_config_get_bool(&i
, cfg
, "core.boolglobal"));
257 cl_assert_equal_b(true, i
);
258 cl_git_pass(git_config_get_string_buf(&buf
, cfg
, "core.stringglobal"));
259 cl_assert_equal_s("I'm a global config value!", git_buf_cstr(&buf
));
262 git_config_free(cfg
);
265 void test_config_write__add_value_at_file_with_no_clrf_at_the_end(void)
270 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
271 cl_git_pass(git_config_set_int32(cfg
, "core.newline", 7));
272 git_config_free(cfg
);
274 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
275 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.newline"));
276 cl_assert_equal_i(7, i
);
278 git_config_free(cfg
);
281 void test_config_write__add_section_at_file_with_no_clrf_at_the_end(void)
286 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
287 cl_git_pass(git_config_set_int32(cfg
, "diff.context", 10));
288 git_config_free(cfg
);
290 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
291 cl_git_pass(git_config_get_int32(&i
, cfg
, "diff.context"));
292 cl_assert_equal_i(10, i
);
294 git_config_free(cfg
);
297 void test_config_write__add_value_which_needs_quotes(void)
299 git_config
*cfg
, *base
;
306 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
307 cl_git_pass(git_config_set_string(cfg
, "core.startwithspace", " Something"));
308 cl_git_pass(git_config_set_string(cfg
, "core.endwithspace", "Something "));
309 cl_git_pass(git_config_set_string(cfg
, "core.containscommentchar1", "some#thing"));
310 cl_git_pass(git_config_set_string(cfg
, "core.containscommentchar2", "some;thing"));
311 cl_git_pass(git_config_set_string(cfg
, "core.startwhithsapceandcontainsdoublequote", " some\"thing"));
312 git_config_free(cfg
);
314 cl_git_pass(git_config_open_ondisk(&base
, "config17"));
315 cl_git_pass(git_config_snapshot(&cfg
, base
));
316 cl_git_pass(git_config_get_string(&str1
, cfg
, "core.startwithspace"));
317 cl_assert_equal_s(" Something", str1
);
318 cl_git_pass(git_config_get_string(&str2
, cfg
, "core.endwithspace"));
319 cl_assert_equal_s("Something ", str2
);
320 cl_git_pass(git_config_get_string(&str3
, cfg
, "core.containscommentchar1"));
321 cl_assert_equal_s("some#thing", str3
);
322 cl_git_pass(git_config_get_string(&str4
, cfg
, "core.containscommentchar2"));
323 cl_assert_equal_s("some;thing", str4
);
324 cl_git_pass(git_config_get_string(&str5
, cfg
, "core.startwhithsapceandcontainsdoublequote"));
325 cl_assert_equal_s(" some\"thing", str5
);
326 git_config_free(cfg
);
327 git_config_free(base
);
330 void test_config_write__can_set_a_value_to_NULL(void)
332 git_repository
*repository
;
335 repository
= cl_git_sandbox_init("testrepo.git");
337 cl_git_pass(git_repository_config(&config
, repository
));
338 cl_git_fail(git_config_set_string(config
, "a.b.c", NULL
));
339 git_config_free(config
);
341 cl_git_sandbox_cleanup();
344 void test_config_write__can_set_an_empty_value(void)
346 git_repository
*repository
;
350 repository
= cl_git_sandbox_init("testrepo.git");
351 cl_git_pass(git_repository_config(&config
, repository
));
353 cl_git_pass(git_config_set_string(config
, "core.somevar", ""));
354 cl_git_pass(git_config_get_string_buf(&buf
, config
, "core.somevar"));
355 cl_assert_equal_s("", buf
.ptr
);
358 git_config_free(config
);
359 cl_git_sandbox_cleanup();
362 void test_config_write__updating_a_locked_config_file_returns_ELOCKED(void)
366 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
368 cl_git_mkfile("config9.lock", "[core]\n");
370 cl_git_fail_with(git_config_set_string(cfg
, "core.dump", "boom"), GIT_ELOCKED
);
372 git_config_free(cfg
);
375 void test_config_write__outside_change(void)
379 const char *filename
= "config-ext-change";
381 cl_git_mkfile(filename
, "[old]\nvalue = 5\n");
383 cl_git_pass(git_config_open_ondisk(&cfg
, filename
));
385 cl_git_pass(git_config_get_int32(&tmp
, cfg
, "old.value"));
387 /* Change the value on the file itself (simulate external process) */
388 cl_git_mkfile(filename
, "[old]\nvalue = 6\n");
390 cl_git_pass(git_config_set_int32(cfg
, "new.value", 7));
392 cl_git_pass(git_config_get_int32(&tmp
, cfg
, "old.value"));
393 cl_assert_equal_i(6, tmp
);
395 git_config_free(cfg
);
398 void test_config_write__to_empty_file(void)
401 const char *filename
= "config-file";
402 git_buf result
= GIT_BUF_INIT
;
404 cl_git_mkfile(filename
, "");
405 cl_git_pass(git_config_open_ondisk(&cfg
, filename
));
406 cl_git_pass(git_config_set_string(cfg
, "section.name", "value"));
407 git_config_free(cfg
);
409 cl_git_pass(git_futils_readbuffer(&result
, "config-file"));
410 cl_assert_equal_s("[section]\n\tname = value\n", result
.ptr
);
412 git_buf_free(&result
);
415 void test_config_write__to_file_with_only_comment(void)
418 const char *filename
= "config-file";
419 git_buf result
= GIT_BUF_INIT
;
421 cl_git_mkfile(filename
, "\n\n");
422 cl_git_pass(git_config_open_ondisk(&cfg
, filename
));
423 cl_git_pass(git_config_set_string(cfg
, "section.name", "value"));
424 git_config_free(cfg
);
426 cl_git_pass(git_futils_readbuffer(&result
, "config-file"));
427 cl_assert_equal_s("\n\n[section]\n\tname = value\n", result
.ptr
);
429 git_buf_free(&result
);