From f5af95cc9d6eab1551366b04ec29df8dfcf39ec9 Mon Sep 17 00:00:00 2001 From: Brett Randall Date: Sun, 17 Mar 2019 22:48:14 +1100 Subject: [PATCH] Added test for git.no_git_env(). Signed-off-by: Brett Randall --- pre_commit/git.py | 5 +++-- tests/git_test.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pre_commit/git.py b/pre_commit/git.py index 06c847f3..c24ca86e 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -18,7 +18,7 @@ def zsplit(s): return [] -def no_git_env(): +def no_git_env(_env=None): # Too many bugs dealing with environment variables and GIT: # https://github.com/pre-commit/pre-commit/issues/300 # In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running @@ -27,8 +27,9 @@ def no_git_env(): # while running pre-commit hooks in submodules. # GIT_DIR: Causes git clone to clone wrong thing # GIT_INDEX_FILE: Causes 'error invalid object ...' during commit + _env = _env if _env is not None else os.environ return { - k: v for k, v in os.environ.items() + k: v for k, v in _env.items() if not k.startswith('GIT_') or k in {'GIT_EXEC_PATH', 'GIT_SSH', 'GIT_SSH_COMMAND'} } diff --git a/tests/git_test.py b/tests/git_test.py index 43f1c156..299729db 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -173,3 +173,20 @@ def test_status_output_with_rename(in_git_dir): cmd_output('git', 'add', '--intent-to-add', 'c') assert git.intent_to_add_files() == ['c'] + + +def test_no_git_env(): + env = { + 'http_proxy': 'http://myproxy:80', + 'GIT_EXEC_PATH': '/some/git/exec/path', + 'GIT_SSH': '/usr/bin/ssh', + 'GIT_SSH_COMMAND': 'ssh -o', + 'GIT_DIR': '/none/shall/pass', + } + no_git_env = git.no_git_env(env) + assert no_git_env == { + 'http_proxy': 'http://myproxy:80', + 'GIT_EXEC_PATH': '/some/git/exec/path', + 'GIT_SSH': '/usr/bin/ssh', + 'GIT_SSH_COMMAND': 'ssh -o', + }