1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.eclipse.jgit.lib;
17
18 import static java.nio.charset.StandardCharsets.UTF_8;
19 import static java.util.concurrent.TimeUnit.DAYS;
20 import static java.util.concurrent.TimeUnit.HOURS;
21 import static java.util.concurrent.TimeUnit.MICROSECONDS;
22 import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 import static java.util.concurrent.TimeUnit.MINUTES;
24 import static java.util.concurrent.TimeUnit.NANOSECONDS;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.eclipse.jgit.util.FileUtils.pathToString;
27 import static org.junit.Assert.assertArrayEquals;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertSame;
32 import static org.junit.Assert.assertThrows;
33 import static org.junit.Assert.assertTrue;
34 import static org.junit.Assert.fail;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.text.MessageFormat;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.Iterator;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.Set;
48 import java.util.concurrent.TimeUnit;
49 import java.util.function.Consumer;
50
51 import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
52 import org.eclipse.jgit.errors.ConfigInvalidException;
53 import org.eclipse.jgit.internal.JGitText;
54 import org.eclipse.jgit.junit.JGitTestUtil;
55 import org.eclipse.jgit.junit.MockSystemReader;
56 import org.eclipse.jgit.merge.MergeConfig;
57 import org.eclipse.jgit.storage.file.FileBasedConfig;
58 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
59 import org.eclipse.jgit.transport.RefSpec;
60 import org.eclipse.jgit.util.FS;
61 import org.eclipse.jgit.util.SystemReader;
62 import org.junit.After;
63 import org.junit.Rule;
64 import org.junit.Test;
65 import org.junit.rules.TemporaryFolder;
66
67
68
69
70 @SuppressWarnings("boxing")
71 public class ConfigTest {
72
73 private static final char WS = '\u2002';
74
75 private static final String REFS_ORIGIN = "+refs/heads/*:refs/remotes/origin/*";
76
77 private static final String REFS_UPSTREAM = "+refs/heads/*:refs/remotes/upstream/*";
78
79 private static final String REFS_BACKUP = "+refs/heads/*:refs/remotes/backup/*";
80
81 @Rule
82 public TemporaryFolder tmp = new TemporaryFolder();
83
84 @After
85 public void tearDown() {
86 SystemReader.setInstance(null);
87 }
88
89 @Test
90 public void test001_ReadBareKey() throws ConfigInvalidException {
91 final Config c = parse("[foo]\nbar\n");
92 assertTrue(c.getBoolean("foo", null, "bar", false));
93 assertEquals("", c.getString("foo", null, "bar"));
94 }
95
96 @Test
97 public void test002_ReadWithSubsection() throws ConfigInvalidException {
98 final Config c = parse("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
99 assertTrue(c.getBoolean("foo", "zip", "bar", false));
100 assertEquals("", c.getString("foo","zip", "bar"));
101 assertFalse(c.getBoolean("foo", "zap", "bar", true));
102 assertEquals("false", c.getString("foo", "zap", "bar"));
103 assertEquals(3, c.getInt("foo", "zap", "n", 4));
104 assertEquals(4, c.getInt("foo", "zap","m", 4));
105 }
106
107 @Test
108 public void test003_PutRemote() {
109 final Config c = new Config();
110 c.setString("sec", "ext", "name", "value");
111 c.setString("sec", "ext", "name2", "value2");
112 final String expText = "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n";
113 assertEquals(expText, c.toText());
114 }
115
116 @Test
117 public void test004_PutGetSimple() {
118 Config c = new Config();
119 c.setString("my", null, "somename", "false");
120 assertEquals("false", c.getString("my", null, "somename"));
121 assertEquals("[my]\n\tsomename = false\n", c.toText());
122 }
123
124 @Test
125 public void test005_PutGetStringList() {
126 Config c = new Config();
127 final LinkedList<String> values = new LinkedList<>();
128 values.add("value1");
129 values.add("value2");
130 c.setStringList("my", null, "somename", values);
131
132 final Object[] expArr = values.toArray();
133 final String[] actArr = c.getStringList("my", null, "somename");
134 assertArrayEquals(expArr, actArr);
135
136 final String expText = "[my]\n\tsomename = value1\n\tsomename = value2\n";
137 assertEquals(expText, c.toText());
138 }
139
140 @Test
141 public void test006_readCaseInsensitive() throws ConfigInvalidException {
142 final Config c = parse("[Foo]\nBar\n");
143 assertTrue(c.getBoolean("foo", null, "bar", false));
144 assertEquals("", c.getString("foo", null, "bar"));
145 }
146
147 @Test
148 public void test007_readUserConfig() {
149 final MockSystemReader mockSystemReader = new MockSystemReader();
150 SystemReader.setInstance(mockSystemReader);
151 final String hostname = mockSystemReader.getHostname();
152 final Config userGitConfig = mockSystemReader.openUserConfig(null,
153 FS.DETECTED);
154 final Config localConfig = new Config(userGitConfig);
155 mockSystemReader.clearProperties();
156
157 String authorName;
158 String authorEmail;
159
160
161 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
162 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
163 assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
164 assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
165 assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
166 assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
167
168
169 mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
170 localConfig.uncache(UserConfig.KEY);
171 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
172 assertEquals("os user name", authorName);
173 assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
174
175 if (hostname != null && hostname.length() != 0) {
176 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
177 assertEquals("os user name@" + hostname, authorEmail);
178 }
179 assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
180
181
182 mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
183 mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
184 localConfig.uncache(UserConfig.KEY);
185 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
186 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
187 assertEquals("git author name", authorName);
188 assertEquals("author@email", authorEmail);
189 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
190 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
191
192
193
194
195 mockSystemReader.clearProperties();
196 userGitConfig.setString("user", null, "name", "global username");
197 userGitConfig.setString("user", null, "email", "author@globalemail");
198 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
199 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
200 assertEquals("global username", authorName);
201 assertEquals("author@globalemail", authorEmail);
202 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
203 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
204
205
206 localConfig.setString("user", null, "name", "local username");
207 localConfig.setString("user", null, "email", "author@localemail");
208 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
209 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
210 assertEquals("local username", authorName);
211 assertEquals("author@localemail", authorEmail);
212 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
213 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
214
215 authorName = localConfig.get(UserConfig.KEY).getCommitterName();
216 authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
217 assertEquals("local username", authorName);
218 assertEquals("author@localemail", authorEmail);
219 assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
220 assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
221
222
223 mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY,
224 "git author name");
225 mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY,
226 "author@email");
227 localConfig.setString("user", null, "name", "local username");
228 localConfig.setString("user", null, "email", "author@localemail");
229 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
230 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
231 assertEquals("git author name", authorName);
232 assertEquals("author@email", authorEmail);
233 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
234 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
235 }
236
237 @Test
238 public void testReadUserConfigWithInvalidCharactersStripped() {
239 final MockSystemReader mockSystemReader = new MockSystemReader();
240 final Config localConfig = new Config(mockSystemReader.openUserConfig(
241 null, FS.DETECTED));
242
243 localConfig.setString("user", null, "name", "foo<bar");
244 localConfig.setString("user", null, "email", "baz>\nqux@example.com");
245
246 UserConfig userConfig = localConfig.get(UserConfig.KEY);
247 assertEquals("foobar", userConfig.getAuthorName());
248 assertEquals("bazqux@example.com", userConfig.getAuthorEmail());
249 }
250
251 @Test
252 public void testReadBoolean_TrueFalse1() throws ConfigInvalidException {
253 final Config c = parse("[s]\na = true\nb = false\n");
254 assertEquals("true", c.getString("s", null, "a"));
255 assertEquals("false", c.getString("s", null, "b"));
256
257 assertTrue(c.getBoolean("s", "a", false));
258 assertFalse(c.getBoolean("s", "b", true));
259 }
260
261 @Test
262 public void testReadBoolean_TrueFalse2() throws ConfigInvalidException {
263 final Config c = parse("[s]\na = TrUe\nb = fAlSe\n");
264 assertEquals("TrUe", c.getString("s", null, "a"));
265 assertEquals("fAlSe", c.getString("s", null, "b"));
266
267 assertTrue(c.getBoolean("s", "a", false));
268 assertFalse(c.getBoolean("s", "b", true));
269 }
270
271 @Test
272 public void testReadBoolean_YesNo1() throws ConfigInvalidException {
273 final Config c = parse("[s]\na = yes\nb = no\n");
274 assertEquals("yes", c.getString("s", null, "a"));
275 assertEquals("no", c.getString("s", null, "b"));
276
277 assertTrue(c.getBoolean("s", "a", false));
278 assertFalse(c.getBoolean("s", "b", true));
279 }
280
281 @Test
282 public void testReadBoolean_YesNo2() throws ConfigInvalidException {
283 final Config c = parse("[s]\na = yEs\nb = NO\n");
284 assertEquals("yEs", c.getString("s", null, "a"));
285 assertEquals("NO", c.getString("s", null, "b"));
286
287 assertTrue(c.getBoolean("s", "a", false));
288 assertFalse(c.getBoolean("s", "b", true));
289 }
290
291 @Test
292 public void testReadBoolean_OnOff1() throws ConfigInvalidException {
293 final Config c = parse("[s]\na = on\nb = off\n");
294 assertEquals("on", c.getString("s", null, "a"));
295 assertEquals("off", c.getString("s", null, "b"));
296
297 assertTrue(c.getBoolean("s", "a", false));
298 assertFalse(c.getBoolean("s", "b", true));
299 }
300
301 @Test
302 public void testReadBoolean_OnOff2() throws ConfigInvalidException {
303 final Config c = parse("[s]\na = ON\nb = OFF\n");
304 assertEquals("ON", c.getString("s", null, "a"));
305 assertEquals("OFF", c.getString("s", null, "b"));
306
307 assertTrue(c.getBoolean("s", "a", false));
308 assertFalse(c.getBoolean("s", "b", true));
309 }
310
311 enum TestEnum {
312 ONE_TWO;
313 }
314
315 @Test
316 public void testGetEnum() throws ConfigInvalidException {
317 Config c = parse("[s]\na = ON\nb = input\nc = true\nd = off\n");
318 assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "a",
319 CoreConfig.AutoCRLF.FALSE));
320
321 assertSame(CoreConfig.AutoCRLF.INPUT, c.getEnum("s", null, "b",
322 CoreConfig.AutoCRLF.FALSE));
323
324 assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "c",
325 CoreConfig.AutoCRLF.FALSE));
326
327 assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
328 CoreConfig.AutoCRLF.TRUE));
329
330 c = new Config();
331 assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
332 CoreConfig.AutoCRLF.FALSE));
333
334 c = parse("[s \"b\"]\n\tc = one two\n");
335 assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
336
337 c = parse("[s \"b\"]\n\tc = one-two\n");
338 assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
339 }
340
341 @Test
342 public void testGetInvalidEnum() throws ConfigInvalidException {
343 Config c = parse("[a]\n\tb = invalid\n");
344 try {
345 c.getEnum("a", null, "b", TestEnum.ONE_TWO);
346 fail();
347 } catch (IllegalArgumentException e) {
348 assertEquals("Invalid value: a.b=invalid", e.getMessage());
349 }
350
351 c = parse("[a \"b\"]\n\tc = invalid\n");
352 try {
353 c.getEnum("a", "b", "c", TestEnum.ONE_TWO);
354 fail();
355 } catch (IllegalArgumentException e) {
356 assertEquals("Invalid value: a.b.c=invalid", e.getMessage());
357 }
358 }
359
360 @Test
361 public void testSetEnum() {
362 final Config c = new Config();
363 c.setEnum("s", "b", "c", TestEnum.ONE_TWO);
364 assertEquals("[s \"b\"]\n\tc = one two\n", c.toText());
365 }
366
367 @Test
368 public void testGetFastForwardMergeoptions() throws ConfigInvalidException {
369 Config c = new Config(null);
370 assertSame(FastForwardMode.FF, c.getEnum(
371 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
372 ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
373 MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
374 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
375 c = parse("[branch \"side\"]\n\tmergeoptions = --ff-only\n");
376 assertSame(FastForwardMode.FF_ONLY, c.getEnum(
377 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
378 ConfigConstants.CONFIG_KEY_MERGEOPTIONS,
379 FastForwardMode.FF_ONLY));
380 mergeConfig = c.get(MergeConfig.getParser("side"));
381 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
382 c = parse("[branch \"side\"]\n\tmergeoptions = --ff\n");
383 assertSame(FastForwardMode.FF, c.getEnum(
384 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
385 ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
386 mergeConfig = c.get(MergeConfig.getParser("side"));
387 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
388 c = parse("[branch \"side\"]\n\tmergeoptions = --no-ff\n");
389 assertSame(FastForwardMode.NO_FF, c.getEnum(
390 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
391 ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.NO_FF));
392 mergeConfig = c.get(MergeConfig.getParser("side"));
393 assertSame(FastForwardMode.NO_FF, mergeConfig.getFastForwardMode());
394 }
395
396 @Test
397 public void testSetFastForwardMergeoptions() {
398 final Config c = new Config();
399 c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF);
400 assertEquals("[branch \"side\"]\n\tmergeoptions = --ff\n", c.toText());
401 c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF_ONLY);
402 assertEquals("[branch \"side\"]\n\tmergeoptions = --ff-only\n",
403 c.toText());
404 c.setEnum("branch", "side", "mergeoptions", FastForwardMode.NO_FF);
405 assertEquals("[branch \"side\"]\n\tmergeoptions = --no-ff\n",
406 c.toText());
407 }
408
409 @Test
410 public void testGetFastForwardMerge() throws ConfigInvalidException {
411 Config c = new Config(null);
412 assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
413 ConfigConstants.CONFIG_KEY_MERGE, null,
414 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
415 MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
416 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
417 c = parse("[merge]\n\tff = only\n");
418 assertSame(FastForwardMode.Merge.ONLY, c.getEnum(
419 ConfigConstants.CONFIG_KEY_MERGE, null,
420 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.ONLY));
421 mergeConfig = c.get(MergeConfig.getParser("side"));
422 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
423 c = parse("[merge]\n\tff = true\n");
424 assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
425 ConfigConstants.CONFIG_KEY_MERGE, null,
426 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
427 mergeConfig = c.get(MergeConfig.getParser("side"));
428 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
429 c = parse("[merge]\n\tff = false\n");
430 assertSame(FastForwardMode.Merge.FALSE, c.getEnum(
431 ConfigConstants.CONFIG_KEY_MERGE, null,
432 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.FALSE));
433 mergeConfig = c.get(MergeConfig.getParser("side"));
434 assertSame(FastForwardMode.NO_FF, mergeConfig.getFastForwardMode());
435 }
436
437 @Test
438 public void testCombinedMergeOptions() throws ConfigInvalidException {
439 Config c = new Config(null);
440 MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
441 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
442 assertTrue(mergeConfig.isCommit());
443 assertFalse(mergeConfig.isSquash());
444
445 c = parse("[merge]\n\tff = false\n"
446 + "[branch \"side\"]\n\tmergeoptions = --ff-only\n");
447 mergeConfig = c.get(MergeConfig.getParser("side"));
448 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
449 assertTrue(mergeConfig.isCommit());
450 assertFalse(mergeConfig.isSquash());
451
452 c = parse("[merge]\n\tff = only\n"
453 + "[branch \"side\"]\n\tmergeoptions = --squash\n");
454 mergeConfig = c.get(MergeConfig.getParser("side"));
455 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
456 assertTrue(mergeConfig.isCommit());
457 assertTrue(mergeConfig.isSquash());
458
459 c = parse("[merge]\n\tff = false\n"
460 + "[branch \"side\"]\n\tmergeoptions = --ff-only --no-commit\n");
461 mergeConfig = c.get(MergeConfig.getParser("side"));
462 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
463 assertFalse(mergeConfig.isCommit());
464 assertFalse(mergeConfig.isSquash());
465 }
466
467 @Test
468 public void testSetFastForwardMerge() {
469 final Config c = new Config();
470 c.setEnum("merge", null, "ff",
471 FastForwardMode.Merge.valueOf(FastForwardMode.FF));
472 assertEquals("[merge]\n\tff = true\n", c.toText());
473 c.setEnum("merge", null, "ff",
474 FastForwardMode.Merge.valueOf(FastForwardMode.FF_ONLY));
475 assertEquals("[merge]\n\tff = only\n", c.toText());
476 c.setEnum("merge", null, "ff",
477 FastForwardMode.Merge.valueOf(FastForwardMode.NO_FF));
478 assertEquals("[merge]\n\tff = false\n", c.toText());
479 }
480
481 @Test
482 public void testReadLong() throws ConfigInvalidException {
483 assertReadLong(1L);
484 assertReadLong(-1L);
485 assertReadLong(Long.MIN_VALUE);
486 assertReadLong(Long.MAX_VALUE);
487 assertReadLong(4L * 1024 * 1024 * 1024, "4g");
488 assertReadLong(3L * 1024 * 1024, "3 m");
489 assertReadLong(8L * 1024, "8 k");
490
491 try {
492 assertReadLong(-1, "1.5g");
493 fail("incorrectly accepted 1.5g");
494 } catch (IllegalArgumentException e) {
495 assertEquals("Invalid integer value: s.a=1.5g", e.getMessage());
496 }
497 }
498
499 @Test
500 public void testBooleanWithNoValue() throws ConfigInvalidException {
501 Config c = parse("[my]\n\tempty\n");
502 assertEquals("", c.getString("my", null, "empty"));
503 assertEquals(1, c.getStringList("my", null, "empty").length);
504 assertEquals("", c.getStringList("my", null, "empty")[0]);
505 assertTrue(c.getBoolean("my", "empty", false));
506 assertEquals("[my]\n\tempty\n", c.toText());
507 }
508
509 @Test
510 public void testUnsetBranchSection() throws ConfigInvalidException {
511 Config c = parse(""
512 + "[branch \"keep\"]\n"
513 + " merge = master.branch.to.keep.in.the.file\n"
514 + "\n"
515 + "[branch \"remove\"]\n"
516 + " merge = this.will.get.deleted\n"
517 + " remote = origin-for-some-long-gone-place\n"
518 + "\n"
519 + "[core-section-not-to-remove-in-test]\n"
520 + " packedGitLimit = 14\n");
521 c.unsetSection("branch", "does.not.exist");
522 c.unsetSection("branch", "remove");
523 assertEquals(""
524 + "[branch \"keep\"]\n"
525 + " merge = master.branch.to.keep.in.the.file\n"
526 + "\n"
527 + "[core-section-not-to-remove-in-test]\n"
528 + " packedGitLimit = 14\n", c.toText());
529 }
530
531 @Test
532 public void testUnsetSingleSection() throws ConfigInvalidException {
533 Config c = parse(""
534 + "[branch \"keep\"]\n"
535 + " merge = master.branch.to.keep.in.the.file\n"
536 + "\n"
537 + "[single]\n"
538 + " merge = this.will.get.deleted\n"
539 + " remote = origin-for-some-long-gone-place\n"
540 + "\n"
541 + "[core-section-not-to-remove-in-test]\n"
542 + " packedGitLimit = 14\n");
543 c.unsetSection("single", null);
544 assertEquals(""
545 + "[branch \"keep\"]\n"
546 + " merge = master.branch.to.keep.in.the.file\n"
547 + "\n"
548 + "[core-section-not-to-remove-in-test]\n"
549 + " packedGitLimit = 14\n", c.toText());
550 }
551
552 @Test
553 public void test008_readSectionNames() throws ConfigInvalidException {
554 final Config c = parse("[a]\n [B]\n");
555 Set<String> sections = c.getSections();
556 assertTrue("Sections should contain \"a\"", sections.contains("a"));
557 assertTrue("Sections should contain \"b\"", sections.contains("b"));
558 }
559
560 @Test
561 public void test009_readNamesInSection() throws ConfigInvalidException {
562 String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
563 + "filemode = false\n" + "logAllRefUpdates = true\n";
564 final Config c = parse(configString);
565 Set<String> names = c.getNames("core");
566 assertEquals("Core section size", 3, names.size());
567 assertTrue("Core section should contain \"filemode\"", names
568 .contains("filemode"));
569
570 assertTrue("Core section should contain \"repositoryFormatVersion\"",
571 names.contains("repositoryFormatVersion"));
572
573 assertTrue("Core section should contain \"repositoryformatversion\"",
574 names.contains("repositoryformatversion"));
575
576 Iterator<String> itr = names.iterator();
577 assertEquals("filemode", itr.next());
578 assertEquals("logAllRefUpdates", itr.next());
579 assertEquals("repositoryFormatVersion", itr.next());
580 assertFalse(itr.hasNext());
581 }
582
583 @Test
584 public void test_ReadNamesInSectionRecursive()
585 throws ConfigInvalidException {
586 String baseConfigString = "[core]\n" + "logAllRefUpdates = true\n";
587 String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
588 + "filemode = false\n";
589 final Config c = parse(configString, parse(baseConfigString));
590 Set<String> names = c.getNames("core", true);
591 assertEquals("Core section size", 3, names.size());
592 assertTrue("Core section should contain \"filemode\"",
593 names.contains("filemode"));
594 assertTrue("Core section should contain \"repositoryFormatVersion\"",
595 names.contains("repositoryFormatVersion"));
596 assertTrue("Core section should contain \"logAllRefUpdates\"",
597 names.contains("logAllRefUpdates"));
598 assertTrue("Core section should contain \"logallrefupdates\"",
599 names.contains("logallrefupdates"));
600
601 Iterator<String> itr = names.iterator();
602 assertEquals("filemode", itr.next());
603 assertEquals("repositoryFormatVersion", itr.next());
604 assertEquals("logAllRefUpdates", itr.next());
605 assertFalse(itr.hasNext());
606 }
607
608 @Test
609 public void test010_readNamesInSubSection() throws ConfigInvalidException {
610 String configString = "[a \"sub1\"]\n"
611 + "x = 0\n"
612 + "y = false\n"
613 + "z = true\n"
614 + "[a \"sub2\"]\n"
615 + "a=0\n"
616 + "b=1\n";
617 final Config c = parse(configString);
618 Set<String> names = c.getNames("a", "sub1");
619 assertEquals("Subsection size", 3, names.size());
620 assertTrue("Subsection should contain \"x\"", names.contains("x"));
621 assertTrue("Subsection should contain \"y\"", names.contains("y"));
622 assertTrue("Subsection should contain \"z\"", names.contains("z"));
623 names = c.getNames("a", "sub2");
624 assertEquals("Subsection size", 2, names.size());
625 assertTrue("Subsection should contain \"a\"", names.contains("a"));
626 assertTrue("Subsection should contain \"b\"", names.contains("b"));
627 }
628
629 @Test
630 public void readNamesInSubSectionRecursive() throws ConfigInvalidException {
631 String baseConfigString = "[a \"sub1\"]\n"
632 + "x = 0\n"
633 + "y = false\n"
634 + "[a \"sub2\"]\n"
635 + "A=0\n";
636 String configString = "[a \"sub1\"]\n"
637 + "z = true\n"
638 + "[a \"sub2\"]\n"
639 + "B=1\n";
640 final Config c = parse(configString, parse(baseConfigString));
641 Set<String> names = c.getNames("a", "sub1", true);
642 assertEquals("Subsection size", 3, names.size());
643 assertTrue("Subsection should contain \"x\"", names.contains("x"));
644 assertTrue("Subsection should contain \"y\"", names.contains("y"));
645 assertTrue("Subsection should contain \"z\"", names.contains("z"));
646 names = c.getNames("a", "sub2", true);
647 assertEquals("Subsection size", 2, names.size());
648 assertTrue("Subsection should contain \"A\"", names.contains("A"));
649 assertTrue("Subsection should contain \"a\"", names.contains("a"));
650 assertTrue("Subsection should contain \"B\"", names.contains("B"));
651 }
652
653
654 @Test
655 public void testNoFinalNewline() throws ConfigInvalidException {
656 Config c = parse("[a]\n"
657 + "x = 0\n"
658 + "y = 1");
659 assertEquals("0", c.getString("a", null, "x"));
660 assertEquals("1", c.getString("a", null, "y"));
661 }
662
663 @Test
664 public void testExplicitlySetEmptyString() throws Exception {
665 Config c = new Config();
666 c.setString("a", null, "x", "0");
667 c.setString("a", null, "y", "");
668
669 assertEquals("0", c.getString("a", null, "x"));
670 assertEquals(0, c.getInt("a", null, "x", 1));
671
672 assertEquals("", c.getString("a", null, "y"));
673 assertArrayEquals(new String[]{""}, c.getStringList("a", null, "y"));
674 assertEquals(1, c.getInt("a", null, "y", 1));
675
676 assertNull(c.getString("a", null, "z"));
677 assertArrayEquals(new String[]{}, c.getStringList("a", null, "z"));
678 }
679
680 @Test
681 public void testParsedEmptyString() throws Exception {
682 Config c = parse("[a]\n"
683 + "x = 0\n"
684 + "y =\n");
685
686 assertEquals("0", c.getString("a", null, "x"));
687 assertEquals(0, c.getInt("a", null, "x", 1));
688
689 assertNull(c.getString("a", null, "y"));
690 assertArrayEquals(new String[]{null}, c.getStringList("a", null, "y"));
691 assertEquals(1, c.getInt("a", null, "y", 1));
692
693 assertNull(c.getString("a", null, "z"));
694 assertArrayEquals(new String[]{}, c.getStringList("a", null, "z"));
695 }
696
697 @Test
698 public void testSetStringListWithEmptyValue() throws Exception {
699 Config c = new Config();
700 c.setStringList("a", null, "x", Arrays.asList(""));
701 assertArrayEquals(new String[]{""}, c.getStringList("a", null, "x"));
702 }
703
704 @Test
705 public void testEmptyValueAtEof() throws Exception {
706 String text = "[a]\nx =";
707 Config c = parse(text);
708 assertNull(c.getString("a", null, "x"));
709 assertArrayEquals(new String[]{null},
710 c.getStringList("a", null, "x"));
711 c = parse(text + "\n");
712 assertNull(c.getString("a", null, "x"));
713 assertArrayEquals(new String[]{null},
714 c.getStringList("a", null, "x"));
715 }
716
717 @Test
718 public void testReadMultipleValuesForName() throws ConfigInvalidException {
719 Config c = parse("[foo]\nbar=false\nbar=true\n");
720 assertTrue(c.getBoolean("foo", "bar", false));
721 }
722
723 @Test
724 public void testIncludeInvalidName() {
725 assertThrows(JGitText.get().invalidLineInConfigFile,
726 ConfigInvalidException.class, () -> parse("[include]\nbar\n"));
727 }
728
729 @Test
730 public void testIncludeNoValue() {
731 assertThrows(JGitText.get().invalidLineInConfigFile,
732 ConfigInvalidException.class, () -> parse("[include]\npath\n"));
733 }
734
735 @Test
736 public void testIncludeEmptyValue() {
737 assertThrows(JGitText.get().invalidLineInConfigFile,
738 ConfigInvalidException.class,
739 () -> parse("[include]\npath=\n"));
740 }
741
742 @Test
743 public void testIncludeValuePathNotFound() throws ConfigInvalidException {
744
745 String notFound = "/not/found";
746 Config parsed = parse("[include]\npath=" + notFound + "\n");
747 assertEquals(1, parsed.getSections().size());
748 assertEquals(notFound, parsed.getString("include", null, "path"));
749 }
750
751 @Test
752 public void testIncludeValuePathWithTilde() throws ConfigInvalidException {
753
754
755 String notSupported = "~/someFile";
756 Config parsed = parse("[include]\npath=" + notSupported + "\n");
757 assertEquals(1, parsed.getSections().size());
758 assertEquals(notSupported, parsed.getString("include", null, "path"));
759 }
760
761 @Test
762 public void testIncludeValuePathRelative() throws ConfigInvalidException {
763
764
765 String notSupported = "someRelativeFile";
766 Config parsed = parse("[include]\npath=" + notSupported + "\n");
767 assertEquals(1, parsed.getSections().size());
768 assertEquals(notSupported, parsed.getString("include", null, "path"));
769 }
770
771 @Test
772 public void testIncludeTooManyRecursions() throws IOException {
773 File config = tmp.newFile("config");
774 String include = "[include]\npath=" + pathToString(config) + "\n";
775 Files.write(config.toPath(), include.getBytes(UTF_8));
776 try {
777 loadConfig(config);
778 fail();
779 } catch (ConfigInvalidException cie) {
780 for (Throwable t = cie; t != null; t = t.getCause()) {
781 if (t.getMessage()
782 .equals(JGitText.get().tooManyIncludeRecursions)) {
783 return;
784 }
785 }
786 fail("Expected to find expected exception message: "
787 + JGitText.get().tooManyIncludeRecursions);
788 }
789 }
790
791 @Test
792 public void testIncludeIsNoop() throws IOException, ConfigInvalidException {
793 File config = tmp.newFile("config");
794
795 String fooBar = "[foo]\nbar=true\n";
796 Files.write(config.toPath(), fooBar.getBytes(UTF_8));
797
798 Config parsed = parse("[include]\npath=" + pathToString(config) + "\n");
799 assertFalse(parsed.getBoolean("foo", "bar", false));
800 }
801
802 @Test
803 public void testIncludeCaseInsensitiveSection()
804 throws IOException, ConfigInvalidException {
805 File included = tmp.newFile("included");
806 String content = "[foo]\nbar=true\n";
807 Files.write(included.toPath(), content.getBytes(UTF_8));
808
809 File config = tmp.newFile("config");
810 content = "[Include]\npath=" + pathToString(included) + "\n";
811 Files.write(config.toPath(), content.getBytes(UTF_8));
812
813 FileBasedConfig fbConfig = loadConfig(config);
814 assertTrue(fbConfig.getBoolean("foo", "bar", false));
815 }
816
817 @Test
818 public void testIncludeCaseInsensitiveKey()
819 throws IOException, ConfigInvalidException {
820 File included = tmp.newFile("included");
821 String content = "[foo]\nbar=true\n";
822 Files.write(included.toPath(), content.getBytes(UTF_8));
823
824 File config = tmp.newFile("config");
825 content = "[include]\nPath=" + pathToString(included) + "\n";
826 Files.write(config.toPath(), content.getBytes(UTF_8));
827
828 FileBasedConfig fbConfig = loadConfig(config);
829 assertTrue(fbConfig.getBoolean("foo", "bar", false));
830 }
831
832 @Test
833 public void testIncludeExceptionContainsLine() {
834 try {
835 parse("[include]\npath=\n");
836 fail("Expected ConfigInvalidException");
837 } catch (ConfigInvalidException e) {
838 assertTrue(
839 "Expected to find the problem line in the exception message",
840 e.getMessage().contains("include.path"));
841 }
842 }
843
844 @Test
845 public void testIncludeExceptionContainsFile() throws IOException {
846 File included = tmp.newFile("included");
847 String includedPath = pathToString(included);
848 String content = "[include]\npath=\n";
849 Files.write(included.toPath(), content.getBytes(UTF_8));
850
851 File config = tmp.newFile("config");
852 String include = "[include]\npath=" + includedPath + "\n";
853 Files.write(config.toPath(), include.getBytes(UTF_8));
854 try {
855 loadConfig(config);
856 fail("Expected ConfigInvalidException");
857 } catch (ConfigInvalidException e) {
858
859
860 for (Throwable t = e; t != null; t = t.getCause()) {
861 if (t.getMessage().contains(includedPath)) {
862 return;
863 }
864 }
865 fail("Expected to find the path in the exception message: "
866 + includedPath);
867 }
868 }
869
870 @Test
871 public void testIncludeSetValueMustNotTouchIncludedLines1()
872 throws IOException, ConfigInvalidException {
873 File includedFile = createAllTypesIncludedContent();
874
875 File configFile = tmp.newFile("config");
876 String content = createAllTypesSampleContent("Alice Parker", false, 11,
877 21, 31, CoreConfig.AutoCRLF.FALSE,
878 "+refs/heads/*:refs/remotes/origin/*") + "\n[include]\npath="
879 + pathToString(includedFile);
880 Files.write(configFile.toPath(), content.getBytes(UTF_8));
881
882 FileBasedConfig fbConfig = loadConfig(configFile);
883 assertValuesAsIncluded(fbConfig, REFS_ORIGIN, REFS_UPSTREAM);
884 assertSections(fbConfig, "user", "core", "remote", "include");
885
886 setAllValuesNew(fbConfig);
887 assertValuesAsIsSaveLoad(fbConfig, config -> {
888 assertValuesAsIncluded(config, REFS_BACKUP, REFS_UPSTREAM);
889 assertSections(fbConfig, "user", "core", "remote", "include");
890 });
891 }
892
893 @Test
894 public void testIncludeSetValueMustNotTouchIncludedLines2()
895 throws IOException, ConfigInvalidException {
896 File includedFile = createAllTypesIncludedContent();
897
898 File configFile = tmp.newFile("config");
899 String content = "[include]\npath=" + pathToString(includedFile) + "\n"
900 + createAllTypesSampleContent("Alice Parker", false, 11, 21, 31,
901 CoreConfig.AutoCRLF.FALSE,
902 "+refs/heads/*:refs/remotes/origin/*");
903 Files.write(configFile.toPath(), content.getBytes(UTF_8));
904
905 FileBasedConfig fbConfig = loadConfig(configFile);
906 assertValuesAsConfig(fbConfig, REFS_UPSTREAM, REFS_ORIGIN);
907 assertSections(fbConfig, "include", "user", "core", "remote");
908
909 setAllValuesNew(fbConfig);
910 assertValuesAsIsSaveLoad(fbConfig, config -> {
911 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
912 assertSections(fbConfig, "include", "user", "core", "remote");
913 });
914 }
915
916 @Test
917 public void testIncludeSetValueOnFileWithJustContainsInclude()
918 throws IOException, ConfigInvalidException {
919 File includedFile = createAllTypesIncludedContent();
920
921 File configFile = tmp.newFile("config");
922 String content = "[include]\npath=" + pathToString(includedFile);
923 Files.write(configFile.toPath(), content.getBytes(UTF_8));
924
925 FileBasedConfig fbConfig = loadConfig(configFile);
926 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
927 assertSections(fbConfig, "include", "user", "core", "remote");
928
929 setAllValuesNew(fbConfig);
930 assertValuesAsIsSaveLoad(fbConfig, config -> {
931 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
932 assertSections(fbConfig, "include", "user", "core", "remote");
933 });
934 }
935
936 @Test
937 public void testIncludeSetValueOnFileWithJustEmptySection1()
938 throws IOException, ConfigInvalidException {
939 File includedFile = createAllTypesIncludedContent();
940
941 File configFile = tmp.newFile("config");
942 String content = "[user]\n[include]\npath="
943 + pathToString(includedFile);
944 Files.write(configFile.toPath(), content.getBytes(UTF_8));
945
946 FileBasedConfig fbConfig = loadConfig(configFile);
947 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
948 assertSections(fbConfig, "user", "include", "core", "remote");
949
950 setAllValuesNew(fbConfig);
951 assertValuesAsIsSaveLoad(fbConfig, config -> {
952 assertValuesAsNewWithName(config, "Alice Muller", REFS_UPSTREAM,
953 REFS_BACKUP);
954 assertSections(fbConfig, "user", "include", "core", "remote");
955 });
956 }
957
958 @Test
959 public void testIncludeSetValueOnFileWithJustEmptySection2()
960 throws IOException, ConfigInvalidException {
961 File includedFile = createAllTypesIncludedContent();
962
963 File configFile = tmp.newFile("config");
964 String content = "[include]\npath=" + pathToString(includedFile)
965 + "\n[user]";
966 Files.write(configFile.toPath(), content.getBytes(UTF_8));
967
968 FileBasedConfig fbConfig = loadConfig(configFile);
969 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
970 assertSections(fbConfig, "include", "user", "core", "remote");
971
972 setAllValuesNew(fbConfig);
973 assertValuesAsIsSaveLoad(fbConfig, config -> {
974 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
975 assertSections(fbConfig, "include", "user", "core", "remote");
976 });
977 }
978
979 @Test
980 public void testIncludeSetValueOnFileWithJustExistingSection1()
981 throws IOException, ConfigInvalidException {
982 File includedFile = createAllTypesIncludedContent();
983
984 File configFile = tmp.newFile("config");
985 String content = "[user]\nemail=alice@home\n[include]\npath="
986 + pathToString(includedFile);
987 Files.write(configFile.toPath(), content.getBytes(UTF_8));
988
989 FileBasedConfig fbConfig = loadConfig(configFile);
990 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
991 assertSections(fbConfig, "user", "include", "core", "remote");
992
993 setAllValuesNew(fbConfig);
994 assertValuesAsIsSaveLoad(fbConfig, config -> {
995 assertValuesAsNewWithName(config, "Alice Muller", REFS_UPSTREAM,
996 REFS_BACKUP);
997 assertSections(fbConfig, "user", "include", "core", "remote");
998 });
999 }
1000
1001 @Test
1002 public void testIncludeSetValueOnFileWithJustExistingSection2()
1003 throws IOException, ConfigInvalidException {
1004 File includedFile = createAllTypesIncludedContent();
1005
1006 File configFile = tmp.newFile("config");
1007 String content = "[include]\npath=" + pathToString(includedFile)
1008 + "\n[user]\nemail=alice@home\n";
1009 Files.write(configFile.toPath(), content.getBytes(UTF_8));
1010
1011 FileBasedConfig fbConfig = loadConfig(configFile);
1012 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
1013 assertSections(fbConfig, "include", "user", "core", "remote");
1014
1015 setAllValuesNew(fbConfig);
1016 assertValuesAsIsSaveLoad(fbConfig, config -> {
1017 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
1018 assertSections(fbConfig, "include", "user", "core", "remote");
1019 });
1020 }
1021
1022 @Test
1023 public void testIncludeUnsetSectionMustNotTouchIncludedLines()
1024 throws IOException, ConfigInvalidException {
1025 File includedFile = tmp.newFile("included");
1026 RefSpec includedRefSpec = new RefSpec(REFS_UPSTREAM);
1027 String includedContent = "[remote \"origin\"]\n" + "fetch="
1028 + includedRefSpec;
1029 Files.write(includedFile.toPath(), includedContent.getBytes(UTF_8));
1030
1031 File configFile = tmp.newFile("config");
1032 RefSpec refSpec = new RefSpec(REFS_ORIGIN);
1033 String content = "[include]\npath=" + pathToString(includedFile) + "\n"
1034 + "[remote \"origin\"]\n" + "fetch=" + refSpec;
1035 Files.write(configFile.toPath(), content.getBytes(UTF_8));
1036
1037 FileBasedConfig fbConfig = loadConfig(configFile);
1038
1039 Consumer<FileBasedConfig> assertion = config -> {
1040 assertEquals(Arrays.asList(includedRefSpec, refSpec),
1041 config.getRefSpecs("remote", "origin", "fetch"));
1042 };
1043 assertion.accept(fbConfig);
1044
1045 fbConfig.unsetSection("remote", "origin");
1046 assertValuesAsIsSaveLoad(fbConfig, config -> {
1047 assertEquals(Collections.singletonList(includedRefSpec),
1048 config.getRefSpecs("remote", "origin", "fetch"));
1049 });
1050 }
1051
1052 private File createAllTypesIncludedContent() throws IOException {
1053 File includedFile = tmp.newFile("included");
1054 String includedContent = createAllTypesSampleContent("Alice Muller",
1055 true, 10, 20, 30, CoreConfig.AutoCRLF.TRUE,
1056 "+refs/heads/*:refs/remotes/upstream/*");
1057 Files.write(includedFile.toPath(), includedContent.getBytes(UTF_8));
1058 return includedFile;
1059 }
1060
1061 private static void assertValuesAsIsSaveLoad(FileBasedConfig fbConfig,
1062 Consumer<FileBasedConfig> assertion)
1063 throws IOException, ConfigInvalidException {
1064 assertion.accept(fbConfig);
1065
1066 fbConfig.save();
1067 assertion.accept(fbConfig);
1068
1069 fbConfig = loadConfig(fbConfig.getFile());
1070 assertion.accept(fbConfig);
1071 }
1072
1073 private static void setAllValuesNew(Config config) {
1074 config.setString("user", null, "name", "Alice Bauer");
1075 config.setBoolean("core", null, "fileMode", false);
1076 config.setInt("core", null, "deltaBaseCacheLimit", 12);
1077 config.setLong("core", null, "packedGitLimit", 22);
1078 config.setLong("core", null, "repositoryCacheExpireAfter", 32);
1079 config.setEnum("core", null, "autocrlf", CoreConfig.AutoCRLF.FALSE);
1080 config.setString("remote", "origin", "fetch",
1081 "+refs/heads/*:refs/remotes/backup/*");
1082 }
1083
1084 private static void assertValuesAsIncluded(Config config, String... refs) {
1085 assertAllTypesSampleContent("Alice Muller", true, 10, 20, 30,
1086 CoreConfig.AutoCRLF.TRUE, config, refs);
1087 }
1088
1089 private static void assertValuesAsConfig(Config config, String... refs) {
1090 assertAllTypesSampleContent("Alice Parker", false, 11, 21, 31,
1091 CoreConfig.AutoCRLF.FALSE, config, refs);
1092 }
1093
1094 private static void assertValuesAsNew(Config config, String... refs) {
1095 assertValuesAsNewWithName(config, "Alice Bauer", refs);
1096 }
1097
1098 private static void assertValuesAsNewWithName(Config config, String name,
1099 String... refs) {
1100 assertAllTypesSampleContent(name, false, 12, 22, 32,
1101 CoreConfig.AutoCRLF.FALSE, config, refs);
1102 }
1103
1104 private static void assertSections(Config config, String... sections) {
1105 assertEquals(Arrays.asList(sections),
1106 new ArrayList<>(config.getSections()));
1107 }
1108
1109 private static String createAllTypesSampleContent(String name,
1110 boolean fileMode, int deltaBaseCacheLimit, long packedGitLimit,
1111 long repositoryCacheExpireAfter, CoreConfig.AutoCRLF autoCRLF,
1112 String fetchRefSpec) {
1113 final StringBuilder builder = new StringBuilder();
1114 builder.append("[user]\n");
1115 builder.append("name=");
1116 builder.append(name);
1117 builder.append("\n");
1118
1119 builder.append("[core]\n");
1120 builder.append("fileMode=");
1121 builder.append(fileMode);
1122 builder.append("\n");
1123
1124 builder.append("deltaBaseCacheLimit=");
1125 builder.append(deltaBaseCacheLimit);
1126 builder.append("\n");
1127
1128 builder.append("packedGitLimit=");
1129 builder.append(packedGitLimit);
1130 builder.append("\n");
1131
1132 builder.append("repositoryCacheExpireAfter=");
1133 builder.append(repositoryCacheExpireAfter);
1134 builder.append("\n");
1135
1136 builder.append("autocrlf=");
1137 builder.append(autoCRLF.name());
1138 builder.append("\n");
1139
1140 builder.append("[remote \"origin\"]\n");
1141 builder.append("fetch=");
1142 builder.append(fetchRefSpec);
1143 builder.append("\n");
1144 return builder.toString();
1145 }
1146
1147 private static void assertAllTypesSampleContent(String name,
1148 boolean fileMode, int deltaBaseCacheLimit, long packedGitLimit,
1149 long repositoryCacheExpireAfter, CoreConfig.AutoCRLF autoCRLF,
1150 Config config, String... fetchRefSpecs) {
1151 assertEquals(name, config.getString("user", null, "name"));
1152 assertEquals(fileMode,
1153 config.getBoolean("core", "fileMode", !fileMode));
1154 assertEquals(deltaBaseCacheLimit,
1155 config.getInt("core", "deltaBaseCacheLimit", -1));
1156 assertEquals(packedGitLimit,
1157 config.getLong("core", "packedGitLimit", -1));
1158 assertEquals(repositoryCacheExpireAfter, config.getTimeUnit("core",
1159 null, "repositoryCacheExpireAfter", -1, MILLISECONDS));
1160 assertEquals(autoCRLF, config.getEnum("core", null, "autocrlf",
1161 CoreConfig.AutoCRLF.INPUT));
1162 final List<RefSpec> refspecs = new ArrayList<>();
1163 for (String fetchRefSpec : fetchRefSpecs) {
1164 refspecs.add(new RefSpec(fetchRefSpec));
1165 }
1166
1167 assertEquals(refspecs, config.getRefSpecs("remote", "origin", "fetch"));
1168 }
1169
1170 private static void assertReadLong(long exp) throws ConfigInvalidException {
1171 assertReadLong(exp, String.valueOf(exp));
1172 }
1173
1174 private static void assertReadLong(long exp, String act)
1175 throws ConfigInvalidException {
1176 final Config c = parse("[s]\na = " + act + "\n");
1177 assertEquals(exp, c.getLong("s", null, "a", 0L));
1178 }
1179
1180 static Config parse(String content)
1181 throws ConfigInvalidException {
1182 return parse(content, null);
1183 }
1184
1185 private static Config parse(String content, Config baseConfig)
1186 throws ConfigInvalidException {
1187 final Config c = new Config(baseConfig);
1188 c.fromText(content);
1189 return c;
1190 }
1191
1192 @Test
1193 public void testTimeUnit() throws ConfigInvalidException {
1194 assertEquals(0, parseTime("0", NANOSECONDS));
1195 assertEquals(2, parseTime("2ns", NANOSECONDS));
1196 assertEquals(200, parseTime("200 nanoseconds", NANOSECONDS));
1197
1198 assertEquals(0, parseTime("0", MICROSECONDS));
1199 assertEquals(2, parseTime("2us", MICROSECONDS));
1200 assertEquals(2, parseTime("2000 nanoseconds", MICROSECONDS));
1201 assertEquals(200, parseTime("200 microseconds", MICROSECONDS));
1202
1203 assertEquals(0, parseTime("0", MILLISECONDS));
1204 assertEquals(2, parseTime("2ms", MILLISECONDS));
1205 assertEquals(2, parseTime("2000microseconds", MILLISECONDS));
1206 assertEquals(200, parseTime("200 milliseconds", MILLISECONDS));
1207
1208 assertEquals(0, parseTime("0s", SECONDS));
1209 assertEquals(2, parseTime("2s", SECONDS));
1210 assertEquals(231, parseTime("231sec", SECONDS));
1211 assertEquals(1, parseTime("1second", SECONDS));
1212 assertEquals(300, parseTime("300 seconds", SECONDS));
1213
1214 assertEquals(2, parseTime("2m", MINUTES));
1215 assertEquals(2, parseTime("2min", MINUTES));
1216 assertEquals(1, parseTime("1 minute", MINUTES));
1217 assertEquals(10, parseTime("10 minutes", MINUTES));
1218
1219 assertEquals(5, parseTime("5h", HOURS));
1220 assertEquals(5, parseTime("5hr", HOURS));
1221 assertEquals(1, parseTime("1hour", HOURS));
1222 assertEquals(48, parseTime("48hours", HOURS));
1223
1224 assertEquals(5, parseTime("5 h", HOURS));
1225 assertEquals(5, parseTime("5 hr", HOURS));
1226 assertEquals(1, parseTime("1 hour", HOURS));
1227 assertEquals(48, parseTime("48 hours", HOURS));
1228 assertEquals(48, parseTime("48 \t \r hours", HOURS));
1229
1230 assertEquals(4, parseTime("4d", DAYS));
1231 assertEquals(1, parseTime("1day", DAYS));
1232 assertEquals(14, parseTime("14days", DAYS));
1233
1234 assertEquals(7, parseTime("1w", DAYS));
1235 assertEquals(7, parseTime("1week", DAYS));
1236 assertEquals(14, parseTime("2w", DAYS));
1237 assertEquals(14, parseTime("2weeks", DAYS));
1238
1239 assertEquals(30, parseTime("1mon", DAYS));
1240 assertEquals(30, parseTime("1month", DAYS));
1241 assertEquals(60, parseTime("2mon", DAYS));
1242 assertEquals(60, parseTime("2months", DAYS));
1243
1244 assertEquals(365, parseTime("1y", DAYS));
1245 assertEquals(365, parseTime("1year", DAYS));
1246 assertEquals(365 * 2, parseTime("2years", DAYS));
1247 }
1248
1249 private long parseTime(String value, TimeUnit unit)
1250 throws ConfigInvalidException {
1251 Config c = parse("[a]\na=" + value + "\n");
1252 return c.getTimeUnit("a", null, "a", 0, unit);
1253 }
1254
1255 @Test
1256 public void testTimeUnitDefaultValue() throws ConfigInvalidException {
1257
1258 assertEquals(20, parse("[a]\na=0\n").getTimeUnit("a", null, "b", 20,
1259 MILLISECONDS));
1260
1261 assertEquals(20, parse("[a]\na=\" \"\n").getTimeUnit("a", null, "a", 20,
1262 MILLISECONDS));
1263
1264
1265 assertEquals(20, parse("[a]\na=test\n").getTimeUnit("a", null, "a", 20,
1266 MILLISECONDS));
1267 }
1268
1269 @Test
1270 public void testTimeUnitInvalid() {
1271 assertThrows("Invalid time unit value: a.a=1 monttthhh",
1272 IllegalArgumentException.class,
1273 () -> parseTime("1 monttthhh", DAYS));
1274 }
1275
1276 @Test
1277 public void testTimeUnitInvalidWithSection() throws ConfigInvalidException {
1278 Config c = parse("[a \"b\"]\na=1 monttthhh\n");
1279 assertThrows("Invalid time unit value: a.b.a=1 monttthhh",
1280 IllegalArgumentException.class,
1281 () -> c.getTimeUnit("a", "b", "a", 0, DAYS));
1282 }
1283
1284 @Test
1285 public void testTimeUnitNegative() {
1286 assertThrows(IllegalArgumentException.class,
1287 () -> parseTime("-1", MILLISECONDS));
1288 }
1289
1290 @Test
1291 public void testEscapeSpacesOnly() throws ConfigInvalidException {
1292
1293 assertEquals("", Config.escapeValue(""));
1294
1295 assertValueRoundTrip(" ", "\" \"");
1296 assertValueRoundTrip(" ", "\" \"");
1297 }
1298
1299 @Test
1300 public void testEscapeLeadingSpace() throws ConfigInvalidException {
1301 assertValueRoundTrip("x", "x");
1302 assertValueRoundTrip(" x", "\" x\"");
1303 assertValueRoundTrip(" x", "\" x\"");
1304 }
1305
1306 @Test
1307 public void testEscapeTrailingSpace() throws ConfigInvalidException {
1308 assertValueRoundTrip("x", "x");
1309 assertValueRoundTrip("x ","\"x \"");
1310 assertValueRoundTrip("x ","\"x \"");
1311 }
1312
1313 @Test
1314 public void testEscapeLeadingAndTrailingSpace()
1315 throws ConfigInvalidException {
1316 assertValueRoundTrip(" x ", "\" x \"");
1317 assertValueRoundTrip(" x ", "\" x \"");
1318 assertValueRoundTrip(" x ", "\" x \"");
1319 assertValueRoundTrip(" x ", "\" x \"");
1320 }
1321
1322 @Test
1323 public void testNoEscapeInternalSpaces() throws ConfigInvalidException {
1324 assertValueRoundTrip("x y");
1325 assertValueRoundTrip("x y");
1326 assertValueRoundTrip("x y");
1327 assertValueRoundTrip("x y z");
1328 assertValueRoundTrip("x " + WS + " y");
1329 }
1330
1331 @Test
1332 public void testNoEscapeSpecialCharacters() throws ConfigInvalidException {
1333 assertValueRoundTrip("x\\y", "x\\\\y");
1334 assertValueRoundTrip("x\"y", "x\\\"y");
1335 assertValueRoundTrip("x\ny", "x\\ny");
1336 assertValueRoundTrip("x\ty", "x\\ty");
1337 assertValueRoundTrip("x\by", "x\\by");
1338 }
1339
1340 @Test
1341 public void testParseLiteralBackspace() throws ConfigInvalidException {
1342
1343
1344 assertEquals("x\by", parseEscapedValue("x\by"));
1345 }
1346
1347 @Test
1348 public void testEscapeCommentCharacters() throws ConfigInvalidException {
1349 assertValueRoundTrip("x#y", "\"x#y\"");
1350 assertValueRoundTrip("x;y", "\"x;y\"");
1351 }
1352
1353 @Test
1354 public void testEscapeValueInvalidCharacters() {
1355 assertIllegalArgumentException(() -> Config.escapeSubsection("x\0y"));
1356 }
1357
1358 @Test
1359 public void testEscapeSubsectionInvalidCharacters() {
1360 assertIllegalArgumentException(() -> Config.escapeSubsection("x\ny"));
1361 assertIllegalArgumentException(() -> Config.escapeSubsection("x\0y"));
1362 }
1363
1364 @Test
1365 public void testParseMultipleQuotedRegions() throws ConfigInvalidException {
1366 assertEquals("b a z; \n", parseEscapedValue("b\" a\"\" z; \\n\""));
1367 }
1368
1369 @Test
1370 public void testParseComments() throws ConfigInvalidException {
1371 assertEquals("baz", parseEscapedValue("baz; comment"));
1372 assertEquals("baz", parseEscapedValue("baz# comment"));
1373 assertEquals("baz", parseEscapedValue("baz ; comment"));
1374 assertEquals("baz", parseEscapedValue("baz # comment"));
1375
1376 assertEquals("baz", parseEscapedValue("baz ; comment"));
1377 assertEquals("baz", parseEscapedValue("baz # comment"));
1378 assertEquals("baz", parseEscapedValue("baz " + WS + " ; comment"));
1379 assertEquals("baz", parseEscapedValue("baz " + WS + " # comment"));
1380
1381 assertEquals("baz ", parseEscapedValue("\"baz \"; comment"));
1382 assertEquals("baz ", parseEscapedValue("\"baz \"# comment"));
1383 assertEquals("baz ", parseEscapedValue("\"baz \" ; comment"));
1384 assertEquals("baz ", parseEscapedValue("\"baz \" # comment"));
1385 }
1386
1387 @Test
1388 public void testEscapeSubsection() throws ConfigInvalidException {
1389 assertSubsectionRoundTrip("", "\"\"");
1390 assertSubsectionRoundTrip("x", "\"x\"");
1391 assertSubsectionRoundTrip(" x", "\" x\"");
1392 assertSubsectionRoundTrip("x ", "\"x \"");
1393 assertSubsectionRoundTrip(" x ", "\" x \"");
1394 assertSubsectionRoundTrip("x y", "\"x y\"");
1395 assertSubsectionRoundTrip("x y", "\"x y\"");
1396 assertSubsectionRoundTrip("x\\y", "\"x\\\\y\"");
1397 assertSubsectionRoundTrip("x\"y", "\"x\\\"y\"");
1398
1399
1400 assertSubsectionRoundTrip("x\by", "\"x\by\"");
1401 assertSubsectionRoundTrip("x\ty", "\"x\ty\"");
1402 }
1403
1404 @Test
1405 public void testParseInvalidValues() {
1406 assertInvalidValue(JGitText.get().newlineInQuotesNotAllowed, "x\"\n\"y");
1407 assertInvalidValue(JGitText.get().endOfFileInEscape, "x\\");
1408 assertInvalidValue(
1409 MessageFormat.format(JGitText.get().badEscape, 'q'), "x\\q");
1410 }
1411
1412 @Test
1413 public void testParseInvalidSubsections() {
1414 assertInvalidSubsection(
1415 JGitText.get().newlineInQuotesNotAllowed, "\"x\ny\"");
1416 }
1417
1418 @Test
1419 public void testDropBackslashFromInvalidEscapeSequenceInSubsectionName()
1420 throws ConfigInvalidException {
1421 assertEquals("x0", parseEscapedSubsection("\"x\\0\""));
1422 assertEquals("xq", parseEscapedSubsection("\"x\\q\""));
1423
1424 assertEquals("xb", parseEscapedSubsection("\"x\\b\""));
1425 assertEquals("xn", parseEscapedSubsection("\"x\\n\""));
1426 assertEquals("xt", parseEscapedSubsection("\"x\\t\""));
1427 }
1428
1429 @Test
1430 public void testInvalidGroupHeader() {
1431 assertThrows(JGitText.get().badGroupHeader,
1432 ConfigInvalidException.class,
1433 () -> parse("[foo \"bar\" ]\nfoo=bar\n"));
1434 }
1435
1436 @Test
1437 public void testCrLf() throws ConfigInvalidException {
1438 assertEquals("true", parseEscapedValue("true\r\n"));
1439 }
1440
1441 @Test
1442 public void testLfContinuation() throws ConfigInvalidException {
1443 assertEquals("true", parseEscapedValue("tr\\\nue"));
1444 }
1445
1446 @Test
1447 public void testCrCharContinuation() {
1448 assertThrows("Bad escape: \\u000d", ConfigInvalidException.class,
1449 () -> parseEscapedValue("tr\\\rue"));
1450 }
1451
1452 @Test
1453 public void testCrEOFContinuation() {
1454 assertThrows("Bad escape: \\u000d", ConfigInvalidException.class,
1455 () -> parseEscapedValue("tr\\\r"));
1456 }
1457
1458 @Test
1459 public void testCrLfContinuation() throws ConfigInvalidException {
1460 assertEquals("true", parseEscapedValue("tr\\\r\nue"));
1461 }
1462
1463 @Test
1464 public void testWhitespaceContinuation() throws ConfigInvalidException {
1465 assertEquals("tr ue", parseEscapedValue("tr \\\n ue"));
1466 assertEquals("tr ue", parseEscapedValue("tr \\\r\n ue"));
1467 }
1468
1469 @Test
1470 public void testCommitTemplateEmptyConfig()
1471 throws ConfigInvalidException, IOException {
1472
1473 Config config = new Config(null);
1474 assertNull(config.get(CommitConfig.KEY).getCommitTemplatePath());
1475 assertNull(config.get(CommitConfig.KEY)
1476 .getCommitTemplateContent(null));
1477 }
1478
1479 @Test
1480 public void testCommitTemplateConfig()
1481 throws ConfigInvalidException, IOException {
1482
1483 File workTree = tmp.newFolder("dummy-worktree");
1484 File tempFile = tmp.newFile("testCommitTemplate-");
1485 Repository repo = FileRepositoryBuilder.create(workTree);
1486 String templateContent = "content of the template";
1487 JGitTestUtil.write(tempFile, templateContent);
1488 String expectedTemplatePath = tempFile.getPath();
1489
1490 Config config = parse(
1491 "[commit]\n\ttemplate = "
1492 + Config.escapeValue(expectedTemplatePath) + "\n");
1493
1494 String templatePath = config.get(CommitConfig.KEY)
1495 .getCommitTemplatePath();
1496 String commitEncoding = config.get(CommitConfig.KEY)
1497 .getCommitEncoding();
1498 assertEquals(expectedTemplatePath, templatePath);
1499 assertEquals(templateContent,
1500 config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
1501 assertNull("no commitEncoding has been set so it must be null",
1502 commitEncoding);
1503 }
1504
1505 @Test
1506 public void testCommitTemplateConfigRelativePath()
1507 throws ConfigInvalidException, IOException {
1508
1509 File workTree = tmp.newFolder("dummy-worktree");
1510 File tempFile = tmp.newFile("testCommitTemplate-");
1511 String templateContent = "content of the template";
1512 JGitTestUtil.write(tempFile, templateContent);
1513 String expectedTemplatePath = "../" + tempFile.getName();
1514
1515 Config config = parse(
1516 "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
1517
1518 String templatePath = config.get(CommitConfig.KEY)
1519 .getCommitTemplatePath();
1520 String commitEncoding = config.get(CommitConfig.KEY)
1521 .getCommitEncoding();
1522 assertEquals(expectedTemplatePath, templatePath);
1523 assertEquals(templateContent, config.get(CommitConfig.KEY)
1524 .getCommitTemplateContent(
1525 new RepositoryBuilder().setWorkTree(workTree).build()));
1526 assertNull("no commitEncoding has been set so it must be null",
1527 commitEncoding);
1528 }
1529
1530 @Test
1531 public void testCommitTemplateEncoding()
1532 throws ConfigInvalidException, IOException {
1533 Config config = new Config(null);
1534 File workTree = tmp.newFolder("dummy-worktree");
1535 Repository repo = FileRepositoryBuilder.create(workTree);
1536 File tempFile = tmp.newFile("testCommitTemplate-");
1537 String templateContent = "content of the template";
1538 JGitTestUtil.write(tempFile, templateContent);
1539 String expectedTemplatePath = tempFile.getPath();
1540 config = parse("[i18n]\n\tcommitEncoding = utf-8\n"
1541 + "[commit]\n\ttemplate = "
1542 + Config.escapeValue(expectedTemplatePath) + "\n");
1543 assertEquals(templateContent,
1544 config.get(CommitConfig.KEY).getCommitTemplateContent(repo));
1545 String commitEncoding = config.get(CommitConfig.KEY)
1546 .getCommitEncoding();
1547 assertEquals("commitEncoding has been set to utf-8 it must be utf-8",
1548 "utf-8", commitEncoding);
1549 }
1550
1551 @Test(expected = ConfigInvalidException.class)
1552 public void testCommitTemplateWithInvalidEncoding()
1553 throws ConfigInvalidException, IOException {
1554 Config config = new Config(null);
1555 File workTree = tmp.newFolder("dummy-worktree");
1556 File tempFile = tmp.newFile("testCommitTemplate-");
1557 Repository repo = FileRepositoryBuilder.create(workTree);
1558 String templateContent = "content of the template";
1559 JGitTestUtil.write(tempFile, templateContent);
1560 config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n"
1561 + "[commit]\n\ttemplate = "
1562 + Config.escapeValue(tempFile.getPath()) + "\n");
1563 config.get(CommitConfig.KEY).getCommitTemplateContent(repo);
1564 }
1565
1566 @Test(expected = FileNotFoundException.class)
1567 public void testCommitTemplateWithInvalidPath()
1568 throws ConfigInvalidException, IOException {
1569 Config config = new Config(null);
1570 File workTree = tmp.newFolder("dummy-worktree");
1571 File tempFile = tmp.newFile("testCommitTemplate-");
1572 Repository repo = FileRepositoryBuilder.create(workTree);
1573 String templateContent = "content of the template";
1574 JGitTestUtil.write(tempFile, templateContent);
1575
1576 String expectedTemplatePath = "~/nonExistingTemplate";
1577 config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
1578 String templatePath = config.get(CommitConfig.KEY)
1579 .getCommitTemplatePath();
1580 assertEquals(expectedTemplatePath, templatePath);
1581 config.get(CommitConfig.KEY).getCommitTemplateContent(repo);
1582 }
1583
1584 private static void assertValueRoundTrip(String value)
1585 throws ConfigInvalidException {
1586 assertValueRoundTrip(value, value);
1587 }
1588
1589 private static void assertValueRoundTrip(String value, String expectedEscaped)
1590 throws ConfigInvalidException {
1591 String escaped = Config.escapeValue(value);
1592 assertEquals("escape failed;", expectedEscaped, escaped);
1593 assertEquals("parse failed;", value, parseEscapedValue(escaped));
1594 }
1595
1596 private static String parseEscapedValue(String escapedValue)
1597 throws ConfigInvalidException {
1598 String text = "[foo]\nbar=" + escapedValue;
1599 Config c = parse(text);
1600 return c.getString("foo", null, "bar");
1601 }
1602
1603 private static void assertInvalidValue(String expectedMessage,
1604 String escapedValue) {
1605 try {
1606 parseEscapedValue(escapedValue);
1607 fail("expected ConfigInvalidException");
1608 } catch (ConfigInvalidException e) {
1609 assertEquals(expectedMessage, e.getMessage());
1610 }
1611 }
1612
1613 private static void assertSubsectionRoundTrip(String subsection,
1614 String expectedEscaped) throws ConfigInvalidException {
1615 String escaped = Config.escapeSubsection(subsection);
1616 assertEquals("escape failed;", expectedEscaped, escaped);
1617 assertEquals("parse failed;", subsection, parseEscapedSubsection(escaped));
1618 }
1619
1620 private static String parseEscapedSubsection(String escapedSubsection)
1621 throws ConfigInvalidException {
1622 String text = "[foo " + escapedSubsection + "]\nbar = value";
1623 Config c = parse(text);
1624 Set<String> subsections = c.getSubsections("foo");
1625 assertEquals("only one section", 1, subsections.size());
1626 return subsections.iterator().next();
1627 }
1628
1629 private static void assertIllegalArgumentException(Runnable r) {
1630 try {
1631 r.run();
1632 fail("expected IllegalArgumentException");
1633 } catch (IllegalArgumentException e) {
1634
1635 }
1636 }
1637
1638 private static void assertInvalidSubsection(String expectedMessage,
1639 String escapedSubsection) {
1640 try {
1641 parseEscapedSubsection(escapedSubsection);
1642 fail("expected ConfigInvalidException");
1643 } catch (ConfigInvalidException e) {
1644 assertEquals(expectedMessage, e.getMessage());
1645 }
1646 }
1647
1648 private static FileBasedConfig loadConfig(File file)
1649 throws IOException, ConfigInvalidException {
1650 final FileBasedConfig config = new FileBasedConfig(null, file,
1651 FS.DETECTED);
1652 config.load();
1653 return config;
1654 }
1655 }