All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yann Simon <yann.simon.fr@gmail.com>
To: Robin Rosenberg <robin.rosenberg.lists@dewire.com>,
	"Shawn O. Pearce" <spearce@spearce.org>
Cc: git <git@vger.kernel.org>
Subject: [PATCH JGIT] Do not read ~/.gitconfig during JUnit tests
Date: Tue, 24 Mar 2009 14:36:40 +0100	[thread overview]
Message-ID: <49C8E1E8.5020706@gmail.com> (raw)

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@gmail.com>
---
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

             reply	other threads:[~2009-03-24 13:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-24 13:36 Yann Simon [this message]
2009-03-26 17:27 ` [PATCH JGIT] Do not read ~/.gitconfig during JUnit tests Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=49C8E1E8.5020706@gmail.com \
    --to=yann.simon.fr@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg.lists@dewire.com \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.