diff options
author | Marco Miller <marco.miller@ericsson.com> | 2016-05-06 13:18:38 -0400 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2016-05-21 01:14:06 +0200 |
commit | 1f86350c5a97d8c6966fe1146d649eb5cbc60f53 (patch) | |
tree | 5f440bdc4216044e356d9fba904971f1b469dc08 /org.eclipse.jgit.test | |
parent | ba0dfe1ae2c6d0e3370822eedb0ffd2eeef0d8e2 (diff) | |
download | jgit-1f86350c5a97d8c6966fe1146d649eb5cbc60f53.tar.gz jgit-1f86350c5a97d8c6966fe1146d649eb5cbc60f53.zip |
Support git config [include] section with absolute path(s)
As per [1], but limited to absolute paths indeed. No support yet for
tilde or $HOME expansion. Support for the --[no-]includes options
([1]) is not part of this commit scope either, but those options'
defaults are in effect as described in [1].
[1] https://git-scm.com/docs/git-config
Included path can be a config file that includes other path-s in turn.
An exception is thrown if too many recursions (circular includes)
happen because of ill-specified config files.
Change-Id: I700bd7b7e1625eb7de0180f220c707d8e7b0930b
Signed-off-by: Marco Miller <marco.miller@ericsson.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/META-INF/MANIFEST.MF | 1 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java | 83 |
2 files changed, 84 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/META-INF/MANIFEST.MF b/org.eclipse.jgit.test/META-INF/MANIFEST.MF index af357740cd..0dc6b61586 100644 --- a/org.eclipse.jgit.test/META-INF/MANIFEST.MF +++ b/org.eclipse.jgit.test/META-INF/MANIFEST.MF @@ -51,6 +51,7 @@ Import-Package: com.googlecode.javaewah;version="[0.7.9,0.8.0)", org.hamcrest;version="[1.1.0,2.0.0)", org.junit;version="[4.4.0,5.0.0)", org.junit.experimental.theories;version="[4.4.0,5.0.0)", + org.junit.rules;version="[4.11.0,5.0.0)", org.junit.runner;version="[4.4.0,5.0.0)", org.junit.runners;version="[4.11.0,5.0.0)", org.slf4j;version="[1.7.2,2.0.0)" 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 6c6100e116..6d07d34d3a 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 @@ -56,6 +56,9 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.text.MessageFormat; import java.util.Arrays; import java.util.Iterator; @@ -64,18 +67,29 @@ import java.util.Set; import org.eclipse.jgit.api.MergeCommand.FastForwardMode; import org.eclipse.jgit.errors.ConfigInvalidException; +import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.junit.MockSystemReader; import org.eclipse.jgit.merge.MergeConfig; +import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.SystemReader; import org.junit.After; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; /** * Test reading of git config */ public class ConfigTest { + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + @After public void tearDown() { SystemReader.setInstance(null); @@ -745,6 +759,75 @@ public class ConfigTest { assertTrue(c.getBoolean("foo", "bar", false)); } + @Test + public void testIncludeInvalidName() throws ConfigInvalidException { + expectedEx.expect(ConfigInvalidException.class); + expectedEx.expectMessage(JGitText.get().invalidLineInConfigFile); + parse("[include]\nbar\n"); + } + + @Test + public void testIncludeNoValue() throws ConfigInvalidException { + expectedEx.expect(ConfigInvalidException.class); + expectedEx.expectMessage(JGitText.get().invalidLineInConfigFile); + parse("[include]\npath\n"); + } + + @Test + public void testIncludeEmptyValue() throws ConfigInvalidException { + expectedEx.expect(ConfigInvalidException.class); + expectedEx.expectMessage(JGitText.get().invalidLineInConfigFile); + parse("[include]\npath=\n"); + } + + @Test + public void testIncludeValuePathNotFound() throws ConfigInvalidException { + String notFound = "/not/found"; + expectedEx.expect(ConfigInvalidException.class); + expectedEx.expectMessage(notFound); + parse("[include]\npath=" + notFound + "\n"); + } + + @Test + public void testIncludeTooManyRecursions() throws IOException { + File config = tmp.newFile("config"); + String include = "[include]\npath=" + config.toPath() + "\n"; + Files.write(config.toPath(), include.getBytes()); + FileBasedConfig fbConfig = new FileBasedConfig(null, config, + FS.DETECTED); + try { + fbConfig.load(); + fail(); + } catch (ConfigInvalidException cie) { + assertEquals(JGitText.get().tooManyIncludeRecursions, + cie.getCause().getMessage()); + } + } + + @Test + public void testInclude() throws IOException, ConfigInvalidException { + File config = tmp.newFile("config"); + File more = tmp.newFile("config.more"); + File other = tmp.newFile("config.other"); + + String fooBar = "[foo]\nbar=true\n"; + String includeMore = "[include]\npath=" + more.toPath() + "\n"; + String includeOther = "path=" + other.toPath() + "\n"; + String fooPlus = fooBar + includeMore + includeOther; + Files.write(config.toPath(), fooPlus.getBytes()); + + String fooMore = "[foo]\nmore=bar\n"; + Files.write(more.toPath(), fooMore.getBytes()); + + String otherMore = "[other]\nmore=bar\n"; + Files.write(other.toPath(), otherMore.getBytes()); + + Config parsed = parse("[include]\npath=" + config.toPath() + "\n"); + assertTrue(parsed.getBoolean("foo", "bar", false)); + assertEquals("bar", parsed.getString("foo", null, "more")); + assertEquals("bar", parsed.getString("other", null, "more")); + } + private static void assertReadLong(long exp) throws ConfigInvalidException { assertReadLong(exp, String.valueOf(exp)); } |