Merging upstream version 2.3.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
c869774b2b
commit
ac2b33dbed
17 changed files with 261 additions and 37 deletions
|
@ -29,26 +29,26 @@ def test_use_color_always():
|
|||
|
||||
|
||||
def test_use_color_no_tty():
|
||||
with mock.patch.object(sys.stdout, 'isatty', return_value=False):
|
||||
with mock.patch.object(sys.stderr, 'isatty', return_value=False):
|
||||
assert use_color('auto') is False
|
||||
|
||||
|
||||
def test_use_color_tty_with_color_support():
|
||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
||||
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
|
||||
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
||||
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
|
||||
assert use_color('auto') is True
|
||||
|
||||
|
||||
def test_use_color_tty_without_color_support():
|
||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
||||
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
|
||||
with mock.patch('pre_commit.color.terminal_supports_color', False):
|
||||
with envcontext.envcontext((('TERM', envcontext.UNSET),)):
|
||||
assert use_color('auto') is False
|
||||
|
||||
|
||||
def test_use_color_dumb_term():
|
||||
with mock.patch.object(sys.stdout, 'isatty', return_value=True):
|
||||
with mock.patch.object(sys.stderr, 'isatty', return_value=True):
|
||||
with mock.patch('pre_commit.color.terminal_supports_color', True):
|
||||
with envcontext.envcontext((('TERM', 'dumb'),)):
|
||||
assert use_color('auto') is False
|
||||
|
|
|
@ -263,6 +263,45 @@ def test_does_not_reformat(tmpdir, out_of_date, store):
|
|||
assert cfg.read() == expected
|
||||
|
||||
|
||||
def test_does_not_change_mixed_endlines_read(up_to_date, tmpdir, store):
|
||||
fmt = (
|
||||
'repos:\n'
|
||||
'- repo: {}\n'
|
||||
' rev: {} # definitely the version I want!\r\n'
|
||||
' hooks:\r\n'
|
||||
' - id: foo\n'
|
||||
' # These args are because reasons!\r\n'
|
||||
' args: [foo, bar, baz]\r\n'
|
||||
)
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
|
||||
expected = fmt.format(up_to_date, git.head_rev(up_to_date)).encode()
|
||||
cfg.write_binary(expected)
|
||||
|
||||
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
|
||||
assert cfg.read_binary() == expected
|
||||
|
||||
|
||||
def test_does_not_change_mixed_endlines_write(tmpdir, out_of_date, store):
|
||||
fmt = (
|
||||
'repos:\n'
|
||||
'- repo: {}\n'
|
||||
' rev: {} # definitely the version I want!\r\n'
|
||||
' hooks:\r\n'
|
||||
' - id: foo\n'
|
||||
' # These args are because reasons!\r\n'
|
||||
' args: [foo, bar, baz]\r\n'
|
||||
)
|
||||
cfg = tmpdir.join(C.CONFIG_FILE)
|
||||
cfg.write_binary(
|
||||
fmt.format(out_of_date.path, out_of_date.original_rev).encode(),
|
||||
)
|
||||
|
||||
assert autoupdate(str(cfg), store, freeze=False, tags_only=False) == 0
|
||||
expected = fmt.format(out_of_date.path, out_of_date.head_rev).encode()
|
||||
assert cfg.read_binary() == expected
|
||||
|
||||
|
||||
def test_loses_formatting_when_not_detectable(out_of_date, store, tmpdir):
|
||||
"""A best-effort attempt is made at updating rev without rewriting
|
||||
formatting. When the original formatting cannot be detected, this
|
||||
|
|
|
@ -89,6 +89,51 @@ def test_run_legacy_recursive(tmpdir):
|
|||
call()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
('hook_type', 'args'),
|
||||
(
|
||||
('pre-commit', []),
|
||||
('pre-merge-commit', []),
|
||||
('pre-push', ['branch_name', 'remote_name']),
|
||||
('commit-msg', ['.git/COMMIT_EDITMSG']),
|
||||
('post-checkout', ['old_head', 'new_head', '1']),
|
||||
# multiple choices for commit-editmsg
|
||||
('prepare-commit-msg', ['.git/COMMIT_EDITMSG']),
|
||||
('prepare-commit-msg', ['.git/COMMIT_EDITMSG', 'message']),
|
||||
('prepare-commit-msg', ['.git/COMMIT_EDITMSG', 'commit', 'deadbeef']),
|
||||
),
|
||||
)
|
||||
def test_check_args_length_ok(hook_type, args):
|
||||
hook_impl._check_args_length(hook_type, args)
|
||||
|
||||
|
||||
def test_check_args_length_error_too_many_plural():
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
hook_impl._check_args_length('pre-commit', ['run', '--all-files'])
|
||||
msg, = excinfo.value.args
|
||||
assert msg == (
|
||||
'hook-impl for pre-commit expected 0 arguments but got 2: '
|
||||
"['run', '--all-files']"
|
||||
)
|
||||
|
||||
|
||||
def test_check_args_length_error_too_many_singluar():
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
hook_impl._check_args_length('commit-msg', [])
|
||||
msg, = excinfo.value.args
|
||||
assert msg == 'hook-impl for commit-msg expected 1 argument but got 0: []'
|
||||
|
||||
|
||||
def test_check_args_length_prepare_commit_msg_error():
|
||||
with pytest.raises(SystemExit) as excinfo:
|
||||
hook_impl._check_args_length('prepare-commit-msg', [])
|
||||
msg, = excinfo.value.args
|
||||
assert msg == (
|
||||
'hook-impl for prepare-commit-msg expected 1, 2, or 3 arguments '
|
||||
'but got 0: []'
|
||||
)
|
||||
|
||||
|
||||
def test_run_ns_pre_commit():
|
||||
ns = hook_impl._run_ns('pre-commit', True, (), b'')
|
||||
assert ns is not None
|
||||
|
|
|
@ -52,6 +52,18 @@ def test_full_msg():
|
|||
assert ret == 'start......end\n'
|
||||
|
||||
|
||||
def test_full_msg_with_cjk():
|
||||
ret = _full_msg(
|
||||
start='啊あ아',
|
||||
end_msg='end',
|
||||
end_color='',
|
||||
use_color=False,
|
||||
cols=15,
|
||||
)
|
||||
# 5 dots: 15 - 6 - 3 - 1
|
||||
assert ret == '啊あ아.....end\n'
|
||||
|
||||
|
||||
def test_full_msg_with_color():
|
||||
ret = _full_msg(
|
||||
start='start',
|
||||
|
|
|
@ -20,4 +20,4 @@ def test_docker_fallback_user():
|
|||
getuid=invalid_attribute,
|
||||
getgid=invalid_attribute,
|
||||
):
|
||||
assert docker.get_docker_user() == '1000:1000'
|
||||
assert docker.get_docker_user() == ()
|
||||
|
|
47
tests/languages/node_test.py
Normal file
47
tests/languages/node_test.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
import sys
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import parse_shebang
|
||||
from pre_commit.languages.node import get_default_version
|
||||
|
||||
|
||||
ACTUAL_GET_DEFAULT_VERSION = get_default_version.__wrapped__
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def is_linux():
|
||||
with mock.patch.object(sys, 'platform', 'linux'):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def is_win32():
|
||||
with mock.patch.object(sys, 'platform', 'win32'):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def find_exe_mck():
|
||||
with mock.patch.object(parse_shebang, 'find_executable') as mck:
|
||||
yield mck
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('is_linux')
|
||||
def test_sets_system_when_node_and_npm_are_available(find_exe_mck):
|
||||
find_exe_mck.return_value = '/path/to/exe'
|
||||
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('is_linux')
|
||||
def test_uses_default_when_node_and_npm_are_not_available(find_exe_mck):
|
||||
find_exe_mck.return_value = None
|
||||
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('is_win32')
|
||||
def test_sets_default_on_windows(find_exe_mck):
|
||||
find_exe_mck.return_value = '/path/to/exe'
|
||||
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
|
|
@ -131,9 +131,9 @@ def test_python_hook(tempdir_factory, store):
|
|||
def test_python_hook_default_version(tempdir_factory, store):
|
||||
# make sure that this continues to work for platforms where default
|
||||
# language detection does not work
|
||||
with mock.patch.object(
|
||||
python, 'get_default_version', return_value=C.DEFAULT,
|
||||
):
|
||||
returns_default = mock.Mock(return_value=C.DEFAULT)
|
||||
lang = languages['python']._replace(get_default_version=returns_default)
|
||||
with mock.patch.dict(languages, python=lang):
|
||||
test_python_hook(tempdir_factory, store)
|
||||
|
||||
|
||||
|
@ -243,6 +243,15 @@ def test_run_a_node_hook(tempdir_factory, store):
|
|||
)
|
||||
|
||||
|
||||
def test_run_a_node_hook_default_version(tempdir_factory, store):
|
||||
# make sure that this continues to work for platforms where node is not
|
||||
# installed at the system
|
||||
returns_default = mock.Mock(return_value=C.DEFAULT)
|
||||
lang = languages['node']._replace(get_default_version=returns_default)
|
||||
with mock.patch.dict(languages, node=lang):
|
||||
test_run_a_node_hook(tempdir_factory, store)
|
||||
|
||||
|
||||
def test_run_versioned_node_hook(tempdir_factory, store):
|
||||
_test_hook_repo(
|
||||
tempdir_factory, store, 'node_versioned_hooks_repo',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue