From e12c6b58ee0284cbdf34bf325deb561a52d10340 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Sat, 27 Jan 2018 16:06:15 +0100 Subject: Minor improvements in git config file inclusions * Section and key names in git config files are case-insensitive. * If an include directive is invalid, include the line in the exception message. * If inclusion of the included file fails, put the file name into the exception message so that the user knows in which file the problem is. Change-Id: If920943af7ff93f5321b3d315dfec5222091256c Signed-off-by: Thomas Wolf --- .../tst/org/eclipse/jgit/lib/ConfigTest.java | 84 +++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit.test') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java index fb1ee8cadb..7862005ebc 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java @@ -808,8 +808,14 @@ public class ConfigTest { fbConfig.load(); fail(); } catch (ConfigInvalidException cie) { - assertEquals(JGitText.get().tooManyIncludeRecursions, - cie.getCause().getMessage()); + for (Throwable t = cie; t != null; t = t.getCause()) { + if (t.getMessage() + .equals(JGitText.get().tooManyIncludeRecursions)) { + return; + } + } + fail("Expected to find expected exception message: " + + JGitText.get().tooManyIncludeRecursions); } } @@ -824,6 +830,80 @@ public class ConfigTest { assertFalse(parsed.getBoolean("foo", "bar", false)); } + @Test + public void testIncludeCaseInsensitiveSection() + throws IOException, ConfigInvalidException { + File included = tmp.newFile("included"); + String content = "[foo]\nbar=true\n"; + Files.write(included.toPath(), content.getBytes()); + + File config = tmp.newFile("config"); + content = "[Include]\npath=" + pathToString(included) + "\n"; + Files.write(config.toPath(), content.getBytes()); + + FileBasedConfig fbConfig = new FileBasedConfig(null, config, + FS.DETECTED); + fbConfig.load(); + assertTrue(fbConfig.getBoolean("foo", "bar", false)); + } + + @Test + public void testIncludeCaseInsensitiveKey() + throws IOException, ConfigInvalidException { + File included = tmp.newFile("included"); + String content = "[foo]\nbar=true\n"; + Files.write(included.toPath(), content.getBytes()); + + File config = tmp.newFile("config"); + content = "[include]\nPath=" + pathToString(included) + "\n"; + Files.write(config.toPath(), content.getBytes()); + + FileBasedConfig fbConfig = new FileBasedConfig(null, config, + FS.DETECTED); + fbConfig.load(); + assertTrue(fbConfig.getBoolean("foo", "bar", false)); + } + + @Test + public void testIncludeExceptionContainsLine() { + try { + parse("[include]\npath=\n"); + fail("Expected ConfigInvalidException"); + } catch (ConfigInvalidException e) { + assertTrue( + "Expected to find the problem line in the exception message", + e.getMessage().contains("include.path")); + } + } + + @Test + public void testIncludeExceptionContainsFile() throws IOException { + File included = tmp.newFile("included"); + String includedPath = pathToString(included); + String content = "[include]\npath=\n"; + Files.write(included.toPath(), content.getBytes()); + + File config = tmp.newFile("config"); + String include = "[include]\npath=" + includedPath + "\n"; + Files.write(config.toPath(), include.getBytes()); + FileBasedConfig fbConfig = new FileBasedConfig(null, config, + FS.DETECTED); + try { + fbConfig.load(); + fail("Expected ConfigInvalidException"); + } catch (ConfigInvalidException e) { + // Check that there is some exception in the chain that contains + // includedPath + for (Throwable t = e; t != null; t = t.getCause()) { + if (t.getMessage().contains(includedPath)) { + return; + } + } + fail("Expected to find the path in the exception message: " + + includedPath); + } + } + private static void assertReadLong(long exp) throws ConfigInvalidException { assertReadLong(exp, String.valueOf(exp)); } -- cgit v1.2.3