{"thread":{"id":"18506","subject":"[PATCH JGIT] Do not read ~/.gitconfig during JUnit tests","startedAt":"2009-03-24T13:36:40Z","lastAt":"2009-03-26T17:27:46Z","messageCount":2,"participants":["Yann Simon","Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"109211","messageId":"49C8E1E8.5020706@gmail.com","threadId":"18506","inReplyTo":null,"subject":"[PATCH JGIT] Do not read ~/.gitconfig during JUnit tests","fromName":"Yann Simon","fromEmail":"yann.simon.fr@gmail.com","sentAt":"2009-03-24T13:36:40Z","receivedAt":"2009-03-24T13:36:40Z","isPatch":true,"sender":{"key":"yann.simon.fr@gmail.com","avatar":"https://gravatar.com/avatar/2d926895d27ac988c5c8e591887e5a6a4c7036390403c74fce92519119b887a0?d=mp&s=160"},"body":"Extend the SystemReader interface to add the responsability\nto get the user's global configuration.\nThis extension is used in the JUnit tests to provide a\ncustom global configuration instance independant\nfrom ~/.gitconfig.\n\nSigned-off-by: Yann Simon <yann.simon.fr@gmail.com>\n---\nThis should close the issue #42.\n\n .../org/spearce/jgit/lib/RepositoryConfigTest.java |    8 +++-----\n .../org/spearce/jgit/lib/RepositoryTestCase.java   |   19 +++++++++++++++++++\n .../src/org/spearce/jgit/lib/RepositoryConfig.java |    7 +++++--\n .../src/org/spearce/jgit/util/SystemReader.java    |   10 +++++++++-\n 4 files changed, 36 insertions(+), 8 deletions(-)\n\ndiff --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\nindex 259bc05..4b5314c 100644\n--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java\n@@ -128,12 +128,10 @@ public void test007_readUserInfos() throws IOException {\n \t\t\thostname = \"localhost\";\n \t\t}\n \n-\t\tfinal File globalConfig = writeTrashFile(\"global.config\", \"\");\n \t\tfinal File localConfig = writeTrashFile(\"local.config\", \"\");\n \t\tSystem.clearProperty(Constants.OS_USER_NAME_KEY);\n \n-\t\tRepositoryConfig globalRepositoryConfig = new RepositoryConfig(null, globalConfig);\n-\t\tRepositoryConfig localRepositoryConfig = new RepositoryConfig(globalRepositoryConfig, localConfig);\n+\t\tRepositoryConfig localRepositoryConfig = new RepositoryConfig(userGitConfig, localConfig);\n \t\tfakeSystemReader.values.clear();\n \n \t\tString authorName;\n@@ -164,8 +162,8 @@ public void test007_readUserInfos() throws IOException {\n \t\tassertEquals(\"author@email\", authorEmail);\n \n \t\t// the values are defined in the global configuration\n-\t\tglobalRepositoryConfig.setString(\"user\", null, \"name\", \"global username\");\n-\t\tglobalRepositoryConfig.setString(\"user\", null, \"email\", \"author@globalemail\");\n+\t\tuserGitConfig.setString(\"user\", null, \"name\", \"global username\");\n+\t\tuserGitConfig.setString(\"user\", null, \"email\", \"author@globalemail\");\n \t\tauthorName = localRepositoryConfig.getAuthorName();\n \t\tauthorEmail = localRepositoryConfig.getAuthorEmail();\n \t\tassertEquals(\"global username\", authorName);\ndiff --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\nindex 5d8c056..588daf4 100644\n--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java\n@@ -89,12 +89,19 @@\n \n \tprotected static class FakeSystemReader implements SystemReader {\n \t\tMap<String, String> values = new HashMap<String, String>();\n+\t\tRepositoryConfig userGitConfig;\n \t\tpublic String getenv(String variable) {\n \t\t\treturn values.get(variable);\n \t\t}\n \t\tpublic String getProperty(String key) {\n \t\t\treturn values.get(key);\n \t\t}\n+\t\tpublic RepositoryConfig openUserConfig() {\n+\t\t\treturn userGitConfig;\n+\t\t}\n+\t\tpublic void setUserGitConfig(RepositoryConfig userGitConfig) {\n+\t\t\tthis.userGitConfig = userGitConfig;\n+\t\t}\n \t}\n \n \t/**\n@@ -227,6 +234,13 @@ protected static void checkFile(File f, final String checkData)\n \t}\n \n \tprotected Repository db;\n+\n+\t/**\n+\t * mock user's global configuration used instead ~/.gitconfig.\n+\t * This configuration can be modified by the tests without any\n+\t * effect for ~/.gitconfig.\n+\t */\n+\tprotected RepositoryConfig userGitConfig;\n \tprivate static Thread shutdownhook;\n \tprivate static List<Runnable> shutDownCleanups = new ArrayList<Runnable>();\n \tprivate static int testcount;\n@@ -257,6 +271,11 @@ public void run() {\n \t\t\t};\n \t\t\tRuntime.getRuntime().addShutdownHook(shutdownhook);\n \t\t}\n+\n+\t\tfinal File userGitConfigFile = new File(trash_git, \"usergitconfig\").getAbsoluteFile();\n+\t\tuserGitConfig = new RepositoryConfig(null, userGitConfigFile);\n+\t\tfakeSystemReader.setUserGitConfig(userGitConfig);\n+\n \t\tdb = new Repository(trash_git);\n \t\tdb.create();\n \ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\nindex 8d19c1b..87fc254 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java\n@@ -73,12 +73,12 @@\n public class RepositoryConfig {\n \t/**\n \t * Obtain a new configuration instance for ~/.gitconfig.\n-\t * \n+\t *\n \t * @return a new configuration instance to read the user's global\n \t *         configuration file from their home directory.\n \t */\n \tpublic static RepositoryConfig openUserConfig() {\n-\t\treturn new RepositoryConfig(null, new File(FS.userHome(), \".gitconfig\"));\n+\t\treturn systemReader.openUserConfig();\n \t}\n \n \tprivate final RepositoryConfig baseConfig;\n@@ -113,6 +113,9 @@ public String getenv(String variable) {\n \t\tpublic String getProperty(String key) {\n \t\t\treturn System.getProperty(key);\n \t\t}\n+\t\tpublic RepositoryConfig openUserConfig() {\n+\t\t\treturn new RepositoryConfig(null, new File(FS.userHome(), \".gitconfig\"));\n+\t\t}\n \t};\n \n \tRepositoryConfig(final Repository repo) {\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java\nindex 9187504..32c2e20 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java\n@@ -37,11 +37,14 @@\n \n package org.spearce.jgit.util;\n \n+import org.spearce.jgit.lib.RepositoryConfig;\n+\n /**\n  * Interface to read values from the system.\n  * <p>\n  * When writing unit tests, extending this interface with a custom class\n- * permits to simulate an access to a system variable or property.\n+ * permits to simulate an access to a system variable or property and\n+ * permits to control the user's global configuration.\n  * </p>\n  */\n public interface SystemReader {\n@@ -56,4 +59,9 @@\n \t * @return value of the system property\n \t */\n \tString getProperty(String key);\n+\n+\t/**\n+\t * @return the git configuration found in the user home\n+\t */\n+\tRepositoryConfig openUserConfig();\n }\n-- \n1.6.1.2\n"},{"id":"109558","messageId":"20090326172746.GG23521@spearce.org","threadId":"18506","inReplyTo":"49C8E1E8.5020706@gmail.com","subject":"Re: [PATCH JGIT] Do not read ~/.gitconfig during JUnit tests","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-03-26T17:27:46Z","receivedAt":"2009-03-26T17:27:46Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Yann Simon <yann.simon.fr@gmail.com> wrote:\n> Extend the SystemReader interface to add the responsability\n> to get the user's global configuration.\n> This extension is used in the JUnit tests to provide a\n> custom global configuration instance independant\n> from ~/.gitconfig.\n> \n> Signed-off-by: Yann Simon <yann.simon.fr@gmail.com>\n> ---\n> This should close the issue #42.\n\nThanks.\n \n-- \nShawn.\n"}]}