]>
git.proxmox.com Git - libgit2.git/blob - tests/config/write.c
1 #include "clar_libgit2.h"
3 void test_config_write__initialize(void)
5 cl_fixture_sandbox("config/config9");
6 cl_fixture_sandbox("config/config15");
7 cl_fixture_sandbox("config/config17");
10 void test_config_write__cleanup(void)
12 cl_fixture_cleanup("config9");
13 cl_fixture_cleanup("config15");
14 cl_fixture_cleanup("config17");
17 void test_config_write__replace_value(void)
21 int64_t l
, expected
= +9223372036854775803;
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));
28 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
29 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.dummy"));
33 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
34 cl_git_pass(git_config_set_int32(cfg
, "core.dummy", 1));
37 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
38 cl_git_pass(git_config_set_int64(cfg
, "core.verylong", expected
));
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
);
46 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
47 cl_must_fail(git_config_get_int32(&i
, cfg
, "core.verylong"));
50 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
51 cl_git_pass(git_config_set_int64(cfg
, "core.verylong", 1));
55 void test_config_write__delete_value(void)
60 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
61 cl_git_pass(git_config_set_int32(cfg
, "core.dummy", 5));
64 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
65 cl_git_pass(git_config_delete_entry(cfg
, "core.dummy"));
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));
75 * At the beginning of the test:
76 * - config9 has: core.dummy2=42
77 * - config15 has: core.dummy2=7
79 void test_config_write__delete_value_at_specific_level(void)
81 git_config
*cfg
, *cfg_specific
;
84 cl_git_pass(git_config_open_ondisk(&cfg
, "config15"));
85 cl_git_pass(git_config_get_int32(&i
, cfg
, "core.dummy2"));
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));
95 cl_git_pass(git_config_open_level(&cfg_specific
, cfg
, GIT_CONFIG_LEVEL_GLOBAL
));
97 cl_git_pass(git_config_delete_entry(cfg_specific
, "core.dummy2"));
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));
104 git_config_free(cfg_specific
);
105 git_config_free(cfg
);
108 void test_config_write__write_subsection(void)
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
);
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
);
123 void test_config_write__delete_inexistent(void)
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
);
132 void test_config_write__value_containing_quotes(void)
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
);
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
);
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
);
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
);
161 void test_config_write__escape_value(void)
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
);
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
);
178 void test_config_write__add_value_at_specific_level(void)
180 git_config
*cfg
, *cfg_specific
;
182 int64_t l
, expected
= +9223372036854775803;
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));
192 cl_git_pass(git_config_open_level(&cfg_specific
, cfg
, GIT_CONFIG_LEVEL_GLOBAL
));
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
);
201 // open config15 as local level config file
202 cl_git_pass(git_config_open_ondisk(&cfg
, "config15"));
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
);
213 git_config_free(cfg
);
216 void test_config_write__add_value_at_file_with_no_clrf_at_the_end(void)
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
);
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
);
229 git_config_free(cfg
);
232 void test_config_write__add_section_at_file_with_no_clrf_at_the_end(void)
237 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
238 cl_git_pass(git_config_set_int32(cfg
, "diff.context", 10));
239 git_config_free(cfg
);
241 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
242 cl_git_pass(git_config_get_int32(&i
, cfg
, "diff.context"));
243 cl_assert_equal_i(10, i
);
245 git_config_free(cfg
);
248 void test_config_write__add_value_which_needs_quotes(void)
257 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
258 cl_git_pass(git_config_set_string(cfg
, "core.startwithspace", " Something"));
259 cl_git_pass(git_config_set_string(cfg
, "core.endwithspace", "Something "));
260 cl_git_pass(git_config_set_string(cfg
, "core.containscommentchar1", "some#thing"));
261 cl_git_pass(git_config_set_string(cfg
, "core.containscommentchar2", "some;thing"));
262 cl_git_pass(git_config_set_string(cfg
, "core.startwhithsapceandcontainsdoublequote", " some\"thing"));
263 git_config_free(cfg
);
265 cl_git_pass(git_config_open_ondisk(&cfg
, "config17"));
266 cl_git_pass(git_config_get_string(&str1
, cfg
, "core.startwithspace"));
267 cl_assert_equal_s(" Something", str1
);
268 cl_git_pass(git_config_get_string(&str2
, cfg
, "core.endwithspace"));
269 cl_assert_equal_s("Something ", str2
);
270 cl_git_pass(git_config_get_string(&str3
, cfg
, "core.containscommentchar1"));
271 cl_assert_equal_s("some#thing", str3
);
272 cl_git_pass(git_config_get_string(&str4
, cfg
, "core.containscommentchar2"));
273 cl_assert_equal_s("some;thing", str4
);
274 cl_git_pass(git_config_get_string(&str5
, cfg
, "core.startwhithsapceandcontainsdoublequote"));
275 cl_assert_equal_s(" some\"thing", str5
);
276 git_config_free(cfg
);
279 void test_config_write__can_set_a_value_to_NULL(void)
281 git_repository
*repository
;
284 repository
= cl_git_sandbox_init("testrepo.git");
286 cl_git_pass(git_repository_config(&config
, repository
));
287 cl_git_fail(git_config_set_string(config
, "a.b.c", NULL
));
288 git_config_free(config
);
290 cl_git_sandbox_cleanup();
293 void test_config_write__can_set_an_empty_value(void)
295 git_repository
*repository
;
299 repository
= cl_git_sandbox_init("testrepo.git");
300 cl_git_pass(git_repository_config(&config
, repository
));
302 cl_git_pass(git_config_set_string(config
, "core.somevar", ""));
303 cl_git_pass(git_config_get_string(&str
, config
, "core.somevar"));
304 cl_assert_equal_s(str
, "");
306 git_config_free(config
);
307 cl_git_sandbox_cleanup();
310 void test_config_write__updating_a_locked_config_file_returns_ELOCKED(void)
314 cl_git_pass(git_config_open_ondisk(&cfg
, "config9"));
316 cl_git_mkfile("config9.lock", "[core]\n");
318 cl_git_fail_with(git_config_set_string(cfg
, "core.dump", "boom"), GIT_ELOCKED
);
320 git_config_free(cfg
);
323 void test_config_write__outside_change(void)
327 const char *filename
= "config-ext-change";
329 cl_git_mkfile(filename
, "[old]\nvalue = 5\n");
331 cl_git_pass(git_config_open_ondisk(&cfg
, filename
));
333 cl_git_pass(git_config_get_int32(&tmp
, cfg
, "old.value"));
335 /* Change the value on the file itself (simulate external process) */
336 cl_git_mkfile(filename
, "[old]\nvalue = 6\n");
338 cl_git_pass(git_config_set_int32(cfg
, "new.value", 7));
340 cl_git_pass(git_config_get_int32(&tmp
, cfg
, "old.value"));
341 cl_assert_equal_i(6, tmp
);
343 cl_git_pass(git_config_refresh(cfg
));
345 cl_git_pass(git_config_get_int32(&tmp
, cfg
, "old.value"));
346 cl_assert_equal_i(6, tmp
);
348 git_config_free(cfg
);