Browse Source

Don't look up "HOME" environment variable unconditionally to avoid KeyError
if it's not defined, like on Windows which usually doesn't have "HOME", but
"HOMEDRIVE" and "HOMEPATH" instead.

Risto Kankkunen 13 years ago
parent
commit
5b8a6675e8
3 changed files with 17 additions and 8 deletions
  1. 14 2
      dulwich/tests/__init__.py
  2. 1 2
      dulwich/tests/test_config.py
  3. 2 4
      dulwich/tests/test_repository.py

+ 14 - 2
dulwich/tests/__init__.py

@@ -30,10 +30,22 @@ import tempfile
 if sys.version_info >= (2, 7):
     # If Python itself provides an exception, use that
     import unittest
-    from unittest import SkipTest, TestCase
+    from unittest import SkipTest, TestCase as _TestCase
 else:
     import unittest2 as unittest
-    from unittest2 import SkipTest, TestCase
+    from unittest2 import SkipTest, TestCase as _TestCase
+
+
+class TestCase(_TestCase):
+    def makeSafeEnv(self):
+        """Modifies HOME to point to a non-existing directory to avoid
+        side-effects caused by user's ~/.gitconfig"""
+
+        if "HOME" in os.environ:
+            self.addCleanup(os.environ.__setitem__, "HOME", os.environ["HOME"])
+        else:
+            self.addCleanup(os.environ.__delitem__, "HOME")
+        os.environ["HOME"] = "/nonexistant"
 
 
 class BlackboxTestCase(TestCase):

+ 1 - 2
dulwich/tests/test_config.py

@@ -171,8 +171,7 @@ class ConfigDictTests(TestCase):
 class StackedConfigTests(TestCase):
 
     def test_default_backends(self):
-        self.addCleanup(os.environ.__setitem__, "HOME", os.environ["HOME"])
-        os.environ["HOME"] = "/nonexistant"
+        self.makeSafeEnv()
         StackedConfig.default_backends()
 
 

+ 2 - 4
dulwich/tests/test_repository.py

@@ -320,8 +320,7 @@ class RepositoryTests(TestCase):
         self.assertIsInstance(r.get_config(), Config)
 
     def test_get_config_stack(self):
-        self.addCleanup(os.environ.__setitem__, "HOME", os.environ["HOME"])
-        os.environ["HOME"] = "/nonexistant"
+        self.makeSafeEnv()
         r = self._repo = open_repo('ooo_merge.git')
         self.assertIsInstance(r.get_config_stack(), Config)
 
@@ -460,8 +459,7 @@ class BuildRepoTests(TestCase):
         self.assertEquals("iso8859-1", r[commit_sha].encoding)
 
     def test_commit_config_identity(self):
-        self.addCleanup(os.environ.__setitem__, "HOME", os.environ["HOME"])
-        os.environ["HOME"] = "/nonexistant"
+        self.makeSafeEnv()
         # commit falls back to the users' identity if it wasn't specified
         r = self._repo
         c = r.get_config()