Merge pull request #972 from javabrett/test-no-git-env

Added test for git.no_git_env()
This commit is contained in:
Anthony Sottile 2019-03-17 17:48:27 -07:00 committed by GitHub
commit 71c238d4ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 21 deletions

View file

@ -10,6 +10,7 @@ repos:
- id: debug-statements
- id: name-tests-test
- id: requirements-txt-fixer
- id: double-quote-string-fixer
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.1
hooks:

View file

@ -20,18 +20,18 @@ def bool_errcheck(result, func, args):
GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(
("GetStdHandle", windll.kernel32), ((1, "nStdHandle"),),
('GetStdHandle', windll.kernel32), ((1, 'nStdHandle'),),
)
GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(
("GetConsoleMode", windll.kernel32),
((1, "hConsoleHandle"), (2, "lpMode")),
('GetConsoleMode', windll.kernel32),
((1, 'hConsoleHandle'), (2, 'lpMode')),
)
GetConsoleMode.errcheck = bool_errcheck
SetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, DWORD)(
("SetConsoleMode", windll.kernel32),
((1, "hConsoleHandle"), (1, "dwMode")),
('SetConsoleMode', windll.kernel32),
((1, 'hConsoleHandle'), (1, 'dwMode')),
)
SetConsoleMode.errcheck = bool_errcheck

View file

@ -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'}
}

View file

@ -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',
}

View file

@ -40,7 +40,7 @@ def test_useless_exclude_for_hook(capsys, in_git_dir):
out = out.strip()
expected = (
"The exclude pattern 'foo' for check-useless-excludes "
"does not match any files"
'does not match any files'
)
assert expected == out
@ -69,7 +69,7 @@ def test_useless_exclude_with_types_filter(capsys, in_git_dir):
out = out.strip()
expected = (
"The exclude pattern '.pre-commit-config.yaml' for "
"check-useless-excludes does not match any files"
'check-useless-excludes does not match any files'
)
assert expected == out

View file

@ -528,7 +528,7 @@ def test_local_golang_additional_dependencies(store):
}
ret = _get_hook(config, store, 'hello').run(())
assert ret[0] == 0
assert _norm_out(ret[1]) == b"Hello, Go examples!\n"
assert _norm_out(ret[1]) == b'Hello, Go examples!\n'
def test_local_rust_additional_dependencies(store):
@ -544,7 +544,7 @@ def test_local_rust_additional_dependencies(store):
}
ret = _get_hook(config, store, 'hello').run(())
assert ret[0] == 0
assert _norm_out(ret[1]) == b"Hello World!\n"
assert _norm_out(ret[1]) == b'Hello World!\n'
def test_fail_hooks(store):

View file

@ -17,12 +17,12 @@ def test_CalledProcessError_str():
)
assert str(error) == (
"Command: ['git', 'status']\n"
"Return code: 1\n"
"Expected return code: 0\n"
"Output: \n"
" stdout\n"
"Errors: \n"
" stderr\n"
'Return code: 1\n'
'Expected return code: 0\n'
'Output: \n'
' stdout\n'
'Errors: \n'
' stderr\n'
)
@ -32,10 +32,10 @@ def test_CalledProcessError_str_nooutput():
)
assert str(error) == (
"Command: ['git', 'status']\n"
"Return code: 1\n"
"Expected return code: 0\n"
"Output: (none)\n"
"Errors: (none)\n"
'Return code: 1\n'
'Expected return code: 0\n'
'Output: (none)\n'
'Errors: (none)\n'
)