diff options
author | Julian Ruppel <julian.ruppel@sap.com> | 2021-04-26 10:24:05 +0200 |
---|---|---|
committer | Julian Ruppel <julian.ruppel@sap.com> | 2021-07-21 15:50:02 -0400 |
commit | e2798413f9a2a8e7a701d1f7b5e5fac7399ed696 (patch) | |
tree | a6c0fd297dfc7b51cfa9d425f08e6d8d9b08261f /org.eclipse.jgit.test/tst/org/eclipse | |
parent | ca969ecc61fc9ae9edb703b5bdaa59b8a5bf733c (diff) | |
download | jgit-e2798413f9a2a8e7a701d1f7b5e5fac7399ed696.tar.gz jgit-e2798413f9a2a8e7a701d1f7b5e5fac7399ed696.zip |
Support commit.template config property
Adds functionality to read the git commit.template property. The
template content is read either via a default encoding or, if present,
via encoding specified by i18n.commitEncoding property.
Bug: 446355
Change-Id: I0c45db98e324ddff26a7e0262835f259d6528a86
Signed-off-by: Julian Ruppel <julian.ruppel@sap.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/ConfigTest.java | 103 |
1 files changed, 103 insertions, 0 deletions
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 327b554b48..fe3c1db502 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 @@ -34,6 +34,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; import java.text.MessageFormat; @@ -50,6 +51,7 @@ import java.util.function.Consumer; import org.eclipse.jgit.api.MergeCommand.FastForwardMode; import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.internal.JGitText; +import org.eclipse.jgit.junit.JGitTestUtil; import org.eclipse.jgit.junit.MockSystemReader; import org.eclipse.jgit.merge.MergeConfig; import org.eclipse.jgit.storage.file.FileBasedConfig; @@ -1463,6 +1465,107 @@ public class ConfigTest { assertEquals("tr ue", parseEscapedValue("tr \\\r\n ue")); } + @Test + public void testCommitTemplateEmptyConfig() + throws ConfigInvalidException, IOException { + // no values defined nowhere + Config config = new Config(null); + assertNull(config.get(CommitConfig.KEY).getCommitTemplatePath()); + assertNull(config.get(CommitConfig.KEY).getCommitTemplateContent()); + } + + @Test + public void testCommitTemplateConfig() + throws ConfigInvalidException, IOException { + + File tempFile = tmp.newFile("testCommitTemplate-"); + String templateContent = "content of the template"; + JGitTestUtil.write(tempFile, templateContent); + String expectedTemplatePath = tempFile.getPath(); + + Config config = parse( + "[commit]\n\ttemplate = " + expectedTemplatePath + "\n"); + + String templatePath = config.get(CommitConfig.KEY) + .getCommitTemplatePath(); + String commitEncoding = config.get(CommitConfig.KEY) + .getCommitEncoding(); + assertEquals(expectedTemplatePath, templatePath); + assertEquals(templateContent, + config.get(CommitConfig.KEY).getCommitTemplateContent()); + assertNull("no commitEncoding has been set so it must be null", + commitEncoding); + } + + @Test + public void testCommitTemplateEncoding() + throws ConfigInvalidException, IOException { + Config config = new Config(null); + File tempFile = tmp.newFile("testCommitTemplate-"); + String templateContent = "content of the template"; + JGitTestUtil.write(tempFile, templateContent); + String expectedTemplatePath = tempFile.getPath(); + config = parse("[i18n]\n\tcommitEncoding = utf-8\n" + + "[commit]\n\ttemplate = " + expectedTemplatePath + "\n"); + assertEquals(templateContent, + config.get(CommitConfig.KEY).getCommitTemplateContent()); + String commitEncoding = config.get(CommitConfig.KEY) + .getCommitEncoding(); + assertEquals("commitEncoding has been set to utf-8 it must be utf-8", + "utf-8", commitEncoding); + } + + @Test + public void testCommitTemplatePathInHomeDirecory() + throws ConfigInvalidException, IOException { + Config config = new Config(null); + File tempFile = tmp.newFile("testCommitTemplate-"); + String templateContent = "content of the template"; + JGitTestUtil.write(tempFile, templateContent); + // proper evaluation of the ~/ directory + String homeDir = System.getProperty("user.home"); + File tempFileInHomeDirectory = File.createTempFile("fileInHomeFolder", + ".tmp", new File(homeDir)); + tempFileInHomeDirectory.deleteOnExit(); + JGitTestUtil.write(tempFileInHomeDirectory, templateContent); + String expectedTemplatePath = tempFileInHomeDirectory.getPath() + .replace(homeDir, "~"); + config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n"); + String templatePath = config.get(CommitConfig.KEY) + .getCommitTemplatePath(); + assertEquals(expectedTemplatePath, templatePath); + assertEquals(templateContent, + config.get(CommitConfig.KEY).getCommitTemplateContent()); + } + + @Test(expected = ConfigInvalidException.class) + public void testCommitTemplateWithInvalidEncoding() + throws ConfigInvalidException, IOException { + Config config = new Config(null); + File tempFile = tmp.newFile("testCommitTemplate-"); + String templateContent = "content of the template"; + JGitTestUtil.write(tempFile, templateContent); + config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n" + + "[commit]\n\ttemplate = " + tempFile.getPath() + "\n"); + config.get(CommitConfig.KEY).getCommitTemplateContent(); + } + + @Test(expected = FileNotFoundException.class) + public void testCommitTemplateWithInvalidPath() + throws ConfigInvalidException, IOException { + Config config = new Config(null); + File tempFile = tmp.newFile("testCommitTemplate-"); + String templateContent = "content of the template"; + JGitTestUtil.write(tempFile, templateContent); + // commit message encoding + String expectedTemplatePath = "nonExistingTemplate"; + config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n"); + String templatePath = config.get(CommitConfig.KEY) + .getCommitTemplatePath(); + assertEquals(expectedTemplatePath, templatePath); + config.get(CommitConfig.KEY).getCommitTemplateContent(); + } + private static void assertValueRoundTrip(String value) throws ConfigInvalidException { assertValueRoundTrip(value, value); |