summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/color_test.py8
-rw-r--r--tests/commands/autoupdate_test.py39
-rw-r--r--tests/commands/hook_impl_test.py45
-rw-r--r--tests/commands/run_test.py12
-rw-r--r--tests/languages/docker_test.py2
-rw-r--r--tests/languages/node_test.py47
-rw-r--r--tests/repository_test.py15
7 files changed, 160 insertions, 8 deletions
diff --git a/tests/color_test.py b/tests/color_test.py
index 98b39c1..5cd226a 100644
--- a/tests/color_test.py
+++ b/tests/color_test.py
@@ -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
diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py
index 2c7b2f1..25161d1 100644
--- a/tests/commands/autoupdate_test.py
+++ b/tests/commands/autoupdate_test.py
@@ -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
diff --git a/tests/commands/hook_impl_test.py b/tests/commands/hook_impl_test.py
index 032fa8f..ddf65b7 100644
--- a/tests/commands/hook_impl_test.py
+++ b/tests/commands/hook_impl_test.py
@@ -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
diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py
index f8e8823..c51bcff 100644
--- a/tests/commands/run_test.py
+++ b/tests/commands/run_test.py
@@ -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',
diff --git a/tests/languages/docker_test.py b/tests/languages/docker_test.py
index 171a3f7..b65b223 100644
--- a/tests/languages/docker_test.py
+++ b/tests/languages/docker_test.py
@@ -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() == ()
diff --git a/tests/languages/node_test.py b/tests/languages/node_test.py
new file mode 100644
index 0000000..fd30046
--- /dev/null
+++ b/tests/languages/node_test.py
@@ -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
diff --git a/tests/repository_test.py b/tests/repository_test.py
index df7e7d1..3c7a637 100644
--- a/tests/repository_test.py
+++ b/tests/repository_test.py
@@ -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',