ソースを参照

Run Git in tests without user's ".gitconfig".

Risto Kankkunen 13 年 前
コミット
685cf860af

+ 19 - 6
dulwich/tests/__init__.py

@@ -35,17 +35,30 @@ else:
     import unittest2 as unittest
     from unittest2 import SkipTest, TestCase as _TestCase
 
+def get_safe_env(env=None):
+    """Returns the environment "env" (or a copy of "os.environ" by default)
+    modified to avoid side-effects caused by user's ~/.gitconfig"""
+
+    if env is None:
+        env = os.environ.copy()
+    # On Windows it's not enough to set "HOME" to a non-existing
+    # directory. Git.cmd takes the first existing directory out of
+    # "%HOME%", "%HOMEDRIVE%%HOMEPATH%" and "%USERPROFILE%".
+    for e in 'HOME', 'HOMEPATH', 'USERPROFILE':
+        env[e] = '/nosuchdir'
+    return env
 
 class TestCase(_TestCase):
     def makeSafeEnv(self):
-        """Modifies HOME to point to a non-existing directory to avoid
+        """Modifies "os.environ" for the duration of a test case 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"
+        for e in 'HOME', 'HOMEPATH', 'USERPROFILE':
+            if e in os.environ:
+                self.addCleanup(os.environ.__setitem__, e, os.environ[e])
+            else:
+                self.addCleanup(os.environ.__delitem__, e)
+            os.environ[e] = '/nosuchdir'
 
 
 class BlackboxTestCase(TestCase):

+ 3 - 1
dulwich/tests/compat/test_client.py

@@ -43,6 +43,7 @@ from dulwich import (
     repo,
     )
 from dulwich.tests import (
+    get_safe_env,
     SkipTest,
     )
 
@@ -255,7 +256,7 @@ class TestSSHVendor(object):
     def connect_ssh(host, command, username=None, port=None):
         cmd, path = command[0].replace("'", '').split(' ')
         cmd = cmd.split('-', 1)
-        p = subprocess.Popen(cmd + [path], stdin=subprocess.PIPE,
+        p = subprocess.Popen(cmd + [path], env=get_safe_env(), stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         return client.SubprocessWrapper(p)
 
@@ -285,6 +286,7 @@ class DulwichSubprocessClientTest(CompatTestCase, DulwichClientTestBase):
     def setUp(self):
         CompatTestCase.setUp(self)
         DulwichClientTestBase.setUp(self)
+        self.makeSafeEnv()
 
     def tearDown(self):
         DulwichClientTestBase.tearDown(self)

+ 5 - 1
dulwich/tests/compat/utils.py

@@ -30,6 +30,7 @@ from dulwich.repo import Repo
 from dulwich.protocol import TCP_GIT_PORT
 
 from dulwich.tests import (
+    get_safe_env,
     SkipTest,
     TestCase,
     )
@@ -117,13 +118,16 @@ def run_git(args, git_path=_DEFAULT_GIT, input=None, capture_stdout=False,
         False, None will be returned as stdout contents.
     :raise OSError: if the git executable was not found.
     """
+
+    env = get_safe_env(popen_kwargs.pop('env', None))
+
     args = [git_path] + args
     popen_kwargs['stdin'] = subprocess.PIPE
     if capture_stdout:
         popen_kwargs['stdout'] = subprocess.PIPE
     else:
         popen_kwargs.pop('stdout', None)
-    p = subprocess.Popen(args, **popen_kwargs)
+    p = subprocess.Popen(args, env=env, **popen_kwargs)
     stdout, stderr = p.communicate(input=input)
     return (p.returncode, stdout)