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 <yann.simon.fr@xxxxxxxxx> --- 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(-) diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java index 259bc05..4b5314c 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java @@ -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); diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java index 5d8c056..588daf4 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java @@ -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(); diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java index 8d19c1b..87fc254 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java @@ -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) { diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java index 9187504..32c2e20 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java +++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java @@ -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(); } -- 1.6.1.2 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html