Do not read ~/.gitconfig during JUnit tests
PATCH 1/1 Do not read ~/.gitconfig during JUnit tests
Extend the SystemReader interface to add the responsability to get the user's global configuration.
This extension is used in the JUnit tests to provide a custom global configuration instance independant from ~/.gitconfig.
Signed-off-by Yann Simon <[email protected]>
---
This should close the issue #42.
.../org/spearce/jgit/lib/RepositoryConfigTest.java | 8 +++----- .../org/spearce/jgit/lib/RepositoryTestCase.java | 19 +++++++++++++++++++ .../src/org/spearce/jgit/lib/RepositoryConfig.java | 7 +++++-- .../src/org/spearce/jgit/util/SystemReader.java | 10 +++++++++- 4 files changed, 36 insertions(+), 8 deletions(-)
org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java +3 -5
@@ -128,12 +128,10 @@ public void test007_readUserInfos() throws IOException { hostname = "localhost"; } - final File globalConfig = writeTrashFile("global.config", ""); final File localConfig = writeTrashFile("local.config", ""); System.clearProperty(Constants.OS_USER_NAME_KEY); - RepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig); - RepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig); + RepositoryConfig localRepositoryConfig = new RepositoryConfig(userGitConfig, localConfig); fakeSystemReader.values.clear(); String authorName;
@@ -164,8 +162,8 @@ public void test007_readUserInfos() throws IOException { assertEquals("author@email", authorEmail); // the values are defined in the global configuration - globalRepositoryConfig.setString("user", null, "name", "global username"); - globalRepositoryConfig.setString("user", null, "email", "author@globalemail"); + userGitConfig.setString("user", null, "name", "global username"); + userGitConfig.setString("user", null, "email", "author@globalemail"); authorName = localRepositoryConfig.getAuthorName(); authorEmail = localRepositoryConfig.getAuthorEmail(); assertEquals("global username", authorName);
org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java +19 -0
@@ -89,12 +89,19 @@ protected static class FakeSystemReader implements SystemReader { Map<String, String> values = new HashMap<String, String>(); + RepositoryConfig userGitConfig; public String getenv(String variable) { return values.get(variable); } public String getProperty(String key) { return values.get(key); } + public RepositoryConfig openUserConfig() { + return userGitConfig; + } + public void setUserGitConfig(RepositoryConfig userGitConfig) { + this.userGitConfig = userGitConfig; + } } /**
@@ -227,6 +234,13 @@ protected static void checkFile(File f, final String checkData) } protected Repository db; + + /** + * mock user's global configuration used instead ~/.gitconfig. + * This configuration can be modified by the tests without any + * effect for ~/.gitconfig. + */ + protected RepositoryConfig userGitConfig; private static Thread shutdownhook; private static List<Runnable> shutDownCleanups = new ArrayList<Runnable>(); private static int testcount;
@@ -257,6 +271,11 @@ public void run() { }; Runtime.getRuntime().addShutdownHook(shutdownhook); } + + final File userGitConfigFile = new File(trash_git, "usergitconfig").getAbsoluteFile(); + userGitConfig = new RepositoryConfig(null, userGitConfigFile); + fakeSystemReader.setUserGitConfig(userGitConfig); + db = new Repository(trash_git); db.create();
org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +5 -2
@@ -73,12 +73,12 @@ public class RepositoryConfig { /** * Obtain a new configuration instance for ~/.gitconfig. - * + * * @return a new configuration instance to read the user's global * configuration file from their home directory. */ public static RepositoryConfig openUserConfig() { - return new RepositoryConfig(null, new File(FS.userHome(), ".gitconfig")); + return systemReader.openUserConfig(); } private final RepositoryConfig baseConfig;
@@ -113,6 +113,9 @@ public String getenv(String variable) { public String getProperty(String key) { return System.getProperty(key); } + public RepositoryConfig openUserConfig() { + return new RepositoryConfig(null, new File(FS.userHome(), ".gitconfig")); + } }; RepositoryConfig(final Repository repo) {
org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java +9 -1
@@ -37,11 +37,14 @@ package org.spearce.jgit.util; +import org.spearce.jgit.lib.RepositoryConfig; + /** * Interface to read values from the system. * <p> * When writing unit tests, extending this interface with a custom class - * permits to simulate an access to a system variable or property. + * permits to simulate an access to a system variable or property and + * permits to control the user's global configuration. * </p> */ public interface SystemReader {
@@ -56,4 +59,9 @@ * @return value of the system property */ String getProperty(String key); + + /** + * @return the git configuration found in the user home + */ + RepositoryConfig openUserConfig(); }
Signature · 1 line
1.6.1.2
Quoted 9 lines · Yann Simon
Extend the SystemReader interface to add the responsability to get the user's global configuration.
This extension is used in the JUnit tests to provide a custom global configuration instance independant from ~/.gitconfig.Signed-off-by Yann Simon <[email protected]>---
This should close the issue #42.
Thanks.
Signature · 1 line
Shawn.