summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:17:38 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2020-11-03 06:17:38 +0000
commite3829cc5d067599ba8416821f67a936b8b3dd368 (patch)
tree5b1b408e23e36868aeb04e934a51feaec86cd145 /tests
parentReleasing debian version 2.7.1-1. (diff)
downloadpre-commit-e3829cc5d067599ba8416821f67a936b8b3dd368.tar.xz
pre-commit-e3829cc5d067599ba8416821f67a936b8b3dd368.zip
Merging upstream version 2.8.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/commands/install_uninstall_test.py57
-rw-r--r--tests/commands/run_test.py6
-rw-r--r--tests/commands/sample_config_test.py2
-rw-r--r--tests/commands/try_repo_test.py14
-rw-r--r--tests/error_handler_test.py47
-rw-r--r--tests/languages/dotnet_test.py0
-rw-r--r--tests/languages/helpers_test.py51
-rw-r--r--tests/languages/node_test.py65
-rw-r--r--tests/languages/pygrep_test.py72
-rw-r--r--tests/languages/python_test.py3
-rw-r--r--tests/languages/ruby_test.py40
-rw-r--r--tests/main_test.py2
-rw-r--r--tests/repository_test.py38
-rw-r--r--tests/xargs_test.py2
14 files changed, 318 insertions, 81 deletions
diff --git a/tests/commands/install_uninstall_test.py b/tests/commands/install_uninstall_test.py
index 5809a3f..7a4b906 100644
--- a/tests/commands/install_uninstall_test.py
+++ b/tests/commands/install_uninstall_test.py
@@ -3,6 +3,8 @@ import re
import sys
from unittest import mock
+import re_assert
+
import pre_commit.constants as C
from pre_commit import git
from pre_commit.commands import install_uninstall
@@ -54,8 +56,13 @@ def patch_sys_exe(exe):
def test_shebang_windows():
+ with patch_platform('win32'), patch_sys_exe('python'):
+ assert shebang() == '#!/usr/bin/env python'
+
+
+def test_shebang_windows_drop_ext():
with patch_platform('win32'), patch_sys_exe('python.exe'):
- assert shebang() == '#!/usr/bin/env python.exe'
+ assert shebang() == '#!/usr/bin/env python'
def test_shebang_posix_not_on_path():
@@ -143,7 +150,7 @@ FILES_CHANGED = (
)
-NORMAL_PRE_COMMIT_RUN = re.compile(
+NORMAL_PRE_COMMIT_RUN = re_assert.Matches(
fr'^\[INFO\] Initializing environment for .+\.\n'
fr'Bash hook\.+Passed\n'
fr'\[master [a-f0-9]{{7}}\] commit!\n'
@@ -159,7 +166,7 @@ def test_install_pre_commit_and_run(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
@@ -171,7 +178,7 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def test_install_in_submodule_and_run(tempdir_factory, store):
@@ -185,7 +192,7 @@ def test_install_in_submodule_and_run(tempdir_factory, store):
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def test_install_in_worktree_and_run(tempdir_factory, store):
@@ -198,7 +205,7 @@ def test_install_in_worktree_and_run(tempdir_factory, store):
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def test_commit_am(tempdir_factory, store):
@@ -243,7 +250,7 @@ def test_install_idempotent(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def _path_without_us():
@@ -297,7 +304,7 @@ def test_environment_not_sourced(tempdir_factory, store):
)
-FAILING_PRE_COMMIT_RUN = re.compile(
+FAILING_PRE_COMMIT_RUN = re_assert.Matches(
r'^\[INFO\] Initializing environment for .+\.\n'
r'Failing hook\.+Failed\n'
r'- hook id: failing_hook\n'
@@ -316,10 +323,10 @@ def test_failing_hooks_returns_nonzero(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 1
- assert FAILING_PRE_COMMIT_RUN.match(output)
+ FAILING_PRE_COMMIT_RUN.assert_matches(output)
-EXISTING_COMMIT_RUN = re.compile(
+EXISTING_COMMIT_RUN = re_assert.Matches(
fr'^legacy hook\n'
fr'\[master [a-f0-9]{{7}}\] commit!\n'
fr'{FILES_CHANGED}'
@@ -342,7 +349,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
# Make sure we installed the "old" hook correctly
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
assert ret == 0
- assert EXISTING_COMMIT_RUN.match(output)
+ EXISTING_COMMIT_RUN.assert_matches(output)
# Now install pre-commit (no-overwrite)
assert install(C.CONFIG_FILE, store, hook_types=['pre-commit']) == 0
@@ -351,7 +358,7 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
assert output.startswith('legacy hook\n')
- assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
def test_legacy_overwriting_legacy_hook(tempdir_factory, store):
@@ -377,10 +384,10 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
assert output.startswith('legacy hook\n')
- assert NORMAL_PRE_COMMIT_RUN.match(output[len('legacy hook\n'):])
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
-FAIL_OLD_HOOK = re.compile(
+FAIL_OLD_HOOK = re_assert.Matches(
r'fail!\n'
r'\[INFO\] Initializing environment for .+\.\n'
r'Bash hook\.+Passed\n',
@@ -401,7 +408,7 @@ def test_failing_existing_hook_returns_1(tempdir_factory, store):
# We should get a failure from the legacy hook
ret, output = _get_commit_output(tempdir_factory)
assert ret == 1
- assert FAIL_OLD_HOOK.match(output)
+ FAIL_OLD_HOOK.assert_matches(output)
def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
@@ -413,7 +420,7 @@ def test_install_overwrite_no_existing_hooks(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def test_install_overwrite(tempdir_factory, store):
@@ -426,7 +433,7 @@ def test_install_overwrite(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
@@ -441,7 +448,7 @@ def test_uninstall_restores_legacy_hooks(tempdir_factory, store):
# Make sure we installed the "old" hook correctly
ret, output = _get_commit_output(tempdir_factory, touch_file='baz')
assert ret == 0
- assert EXISTING_COMMIT_RUN.match(output)
+ EXISTING_COMMIT_RUN.assert_matches(output)
def test_replace_old_commit_script(tempdir_factory, store):
@@ -463,7 +470,7 @@ def test_replace_old_commit_script(tempdir_factory, store):
ret, output = _get_commit_output(tempdir_factory)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
@@ -476,7 +483,7 @@ def test_uninstall_doesnt_remove_not_our_hooks(in_git_dir):
assert pre_commit.exists()
-PRE_INSTALLED = re.compile(
+PRE_INSTALLED = re_assert.Matches(
fr'Bash hook\.+Passed\n'
fr'\[master [a-f0-9]{{7}}\] commit!\n'
fr'{FILES_CHANGED}'
@@ -493,7 +500,7 @@ def test_installs_hooks_with_hooks_True(tempdir_factory, store):
)
assert ret == 0
- assert PRE_INSTALLED.match(output)
+ PRE_INSTALLED.assert_matches(output)
def test_install_hooks_command(tempdir_factory, store):
@@ -506,7 +513,7 @@ def test_install_hooks_command(tempdir_factory, store):
)
assert ret == 0
- assert PRE_INSTALLED.match(output)
+ PRE_INSTALLED.assert_matches(output)
def test_installed_from_venv(tempdir_factory, store):
@@ -533,7 +540,7 @@ def test_installed_from_venv(tempdir_factory, store):
},
)
assert ret == 0
- assert NORMAL_PRE_COMMIT_RUN.match(output)
+ NORMAL_PRE_COMMIT_RUN.assert_matches(output)
def _get_push_output(tempdir_factory, remote='origin', opts=()):
@@ -880,7 +887,7 @@ def test_prepare_commit_msg_legacy(
def test_pre_merge_commit_integration(tempdir_factory, store):
- expected = re.compile(
+ output_pattern = re_assert.Matches(
r'^\[INFO\] Initializing environment for .+\n'
r'Bash hook\.+Passed\n'
r"Merge made by the 'recursive' strategy.\n"
@@ -902,7 +909,7 @@ def test_pre_merge_commit_integration(tempdir_factory, store):
tempdir_factory=tempdir_factory,
)
assert ret == 0
- assert expected.match(output)
+ output_pattern.assert_matches(output)
def test_install_disallow_missing_config(tempdir_factory, store):
diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py
index 2461ed5..00b4712 100644
--- a/tests/commands/run_test.py
+++ b/tests/commands/run_test.py
@@ -2,6 +2,7 @@ import os.path
import shlex
import sys
import time
+from typing import MutableMapping
from unittest import mock
import pytest
@@ -18,7 +19,6 @@ from pre_commit.commands.run import Classifier
from pre_commit.commands.run import filter_by_include_exclude
from pre_commit.commands.run import run
from pre_commit.util import cmd_output
-from pre_commit.util import EnvironT
from pre_commit.util import make_executable
from testing.auto_namedtuple import auto_namedtuple
from testing.fixtures import add_config_to_repo
@@ -482,7 +482,7 @@ def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
def test_checkout_type(cap_out, store, repo_with_passing_hook):
args = run_opts(from_ref='', to_ref='', checkout_type='1')
- environ: EnvironT = {}
+ environ: MutableMapping[str, str] = {}
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, args, environ,
)
@@ -1032,7 +1032,7 @@ def test_skipped_without_any_setup_for_post_checkout(in_git_dir, store):
def test_pre_commit_env_variable_set(cap_out, store, repo_with_passing_hook):
args = run_opts()
- environ: EnvironT = {}
+ environ: MutableMapping[str, str] = {}
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook, args, environ,
)
diff --git a/tests/commands/sample_config_test.py b/tests/commands/sample_config_test.py
index 11c0876..8e3a904 100644
--- a/tests/commands/sample_config_test.py
+++ b/tests/commands/sample_config_test.py
@@ -10,7 +10,7 @@ def test_sample_config(capsys):
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
- rev: v2.4.0
+ rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
diff --git a/tests/commands/try_repo_test.py b/tests/commands/try_repo_test.py
index d3ec3fd..a157d16 100644
--- a/tests/commands/try_repo_test.py
+++ b/tests/commands/try_repo_test.py
@@ -3,6 +3,8 @@ import re
import time
from unittest import mock
+import re_assert
+
from pre_commit import git
from pre_commit.commands.try_repo import try_repo
from pre_commit.util import cmd_output
@@ -43,7 +45,7 @@ def test_try_repo_repo_only(cap_out, tempdir_factory):
_run_try_repo(tempdir_factory, verbose=True)
start, config, rest = _get_out(cap_out)
assert start == ''
- assert re.match(
+ config_pattern = re_assert.Matches(
'^repos:\n'
'- repo: .+\n'
' rev: .+\n'
@@ -51,8 +53,8 @@ def test_try_repo_repo_only(cap_out, tempdir_factory):
' - id: bash_hook\n'
' - id: bash_hook2\n'
' - id: bash_hook3\n$',
- config,
)
+ config_pattern.assert_matches(config)
assert rest == '''\
Bash hook............................................(no files to check)Skipped
- hook id: bash_hook
@@ -71,14 +73,14 @@ def test_try_repo_with_specific_hook(cap_out, tempdir_factory):
_run_try_repo(tempdir_factory, hook='bash_hook', verbose=True)
start, config, rest = _get_out(cap_out)
assert start == ''
- assert re.match(
+ config_pattern = re_assert.Matches(
'^repos:\n'
'- repo: .+\n'
' rev: .+\n'
' hooks:\n'
' - id: bash_hook\n$',
- config,
)
+ config_pattern.assert_matches(config)
assert rest == '''\
Bash hook............................................(no files to check)Skipped
- hook id: bash_hook
@@ -128,14 +130,14 @@ def test_try_repo_uncommitted_changes(cap_out, tempdir_factory):
start, config, rest = _get_out(cap_out)
assert start == '[WARNING] Creating temporary repo with uncommitted changes...\n' # noqa: E501
- assert re.match(
+ config_pattern = re_assert.Matches(
'^repos:\n'
'- repo: .+shadow-repo\n'
' rev: .+\n'
' hooks:\n'
' - id: bash_hook\n$',
- config,
)
+ config_pattern.assert_matches(config)
assert rest == 'modified name!...........................................................Passed\n' # noqa: E501
diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py
index d066e57..6b0bb86 100644
--- a/tests/error_handler_test.py
+++ b/tests/error_handler_test.py
@@ -1,12 +1,13 @@
import os.path
-import re
import stat
import sys
from unittest import mock
import pytest
+import re_assert
from pre_commit import error_handler
+from pre_commit.errors import FatalError
from pre_commit.store import Store
from pre_commit.util import CalledProcessError
from testing.util import cmd_output_mocked_pre_commit_home
@@ -26,27 +27,28 @@ def test_error_handler_no_exception(mocked_log_and_exit):
def test_error_handler_fatal_error(mocked_log_and_exit):
- exc = error_handler.FatalError('just a test')
+ exc = FatalError('just a test')
with error_handler.error_handler():
raise exc
mocked_log_and_exit.assert_called_once_with(
'An error has occurred',
+ 1,
exc,
# Tested below
mock.ANY,
)
- assert re.match(
+ pattern = re_assert.Matches(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
r' File ".+tests.error_handler_test.py", line \d+, '
r'in test_error_handler_fatal_error\n'
r' raise exc\n'
- r'(pre_commit\.error_handler\.)?FatalError: just a test\n',
- mocked_log_and_exit.call_args[0][2],
+ r'(pre_commit\.errors\.)?FatalError: just a test\n',
)
+ pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
def test_error_handler_uncaught_error(mocked_log_and_exit):
@@ -56,11 +58,12 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
mocked_log_and_exit.assert_called_once_with(
'An unexpected error has occurred',
+ 3,
exc,
# Tested below
mock.ANY,
)
- assert re.match(
+ pattern = re_assert.Matches(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
@@ -68,8 +71,8 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
r'in test_error_handler_uncaught_error\n'
r' raise exc\n'
r'ValueError: another test\n',
- mocked_log_and_exit.call_args[0][2],
)
+ pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
@@ -79,11 +82,12 @@ def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
mocked_log_and_exit.assert_called_once_with(
'Interrupted (^C)',
+ 130,
exc,
# Tested below
mock.ANY,
)
- assert re.match(
+ pattern = re_assert.Matches(
r'Traceback \(most recent call last\):\n'
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
r' yield\n'
@@ -91,15 +95,20 @@ def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
r'in test_error_handler_keyboardinterrupt\n'
r' raise exc\n'
r'KeyboardInterrupt\n',
- mocked_log_and_exit.call_args[0][2],
)
+ pattern.assert_matches(mocked_log_and_exit.call_args[0][3])
def test_log_and_exit(cap_out, mock_store_dir):
- with pytest.raises(SystemExit):
- error_handler._log_and_exit(
- 'msg', error_handler.FatalError('hai'), "I'm a stacktrace",
- )
+ tb = (
+ 'Traceback (most recent call last):\n'
+ ' File "<stdin>", line 2, in <module>\n'
+ 'pre_commit.errors.FatalError: hai\n'
+ )
+
+ with pytest.raises(SystemExit) as excinfo:
+ error_handler._log_and_exit('msg', 1, FatalError('hai'), tb)
+ assert excinfo.value.code == 1
printed = cap_out.get()
log_file = os.path.join(mock_store_dir, 'pre-commit.log')
@@ -108,7 +117,7 @@ def test_log_and_exit(cap_out, mock_store_dir):
assert os.path.exists(log_file)
with open(log_file) as f:
logged = f.read()
- expected = (
+ pattern = re_assert.Matches(
r'^### version information\n'
r'\n'
r'```\n'
@@ -127,10 +136,12 @@ def test_log_and_exit(cap_out, mock_store_dir):
r'```\n'
r'\n'
r'```\n'
- r"I'm a stacktrace\n"
- r'```\n'
+ r'Traceback \(most recent call last\):\n'
+ r' File "<stdin>", line 2, in <module>\n'
+ r'pre_commit\.errors\.FatalError: hai\n'
+ r'```\n',
)
- assert re.match(expected, logged)
+ pattern.assert_matches(logged)
def test_error_handler_non_ascii_exception(mock_store_dir):
@@ -163,7 +174,7 @@ def test_error_handler_no_tty(tempdir_factory):
'from pre_commit.error_handler import error_handler\n'
'with error_handler():\n'
' raise ValueError("\\u2603")\n',
- retcode=1,
+ retcode=3,
tempdir_factory=tempdir_factory,
pre_commit_home=pre_commit_home,
)
diff --git a/tests/languages/dotnet_test.py b/tests/languages/dotnet_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/languages/dotnet_test.py
diff --git a/tests/languages/helpers_test.py b/tests/languages/helpers_test.py
index fa493cc..669cd33 100644
--- a/tests/languages/helpers_test.py
+++ b/tests/languages/helpers_test.py
@@ -1,17 +1,66 @@
import multiprocessing
-import os
+import os.path
import sys
from unittest import mock
import pytest
import pre_commit.constants as C
+from pre_commit import parse_shebang
from pre_commit.languages import helpers
from pre_commit.prefix import Prefix
from pre_commit.util import CalledProcessError
from testing.auto_namedtuple import auto_namedtuple
+@pytest.fixture
+def find_exe_mck():
+ with mock.patch.object(parse_shebang, 'find_executable') as mck:
+ yield mck
+
+
+@pytest.fixture
+def homedir_mck():
+ def fake_expanduser(pth):
+ assert pth == '~'
+ return os.path.normpath('/home/me')
+
+ with mock.patch.object(os.path, 'expanduser', fake_expanduser):
+ yield
+
+
+def test_exe_exists_does_not_exist(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = None
+ assert helpers.exe_exists('ruby') is False
+
+
+def test_exe_exists_exists(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby')
+ assert helpers.exe_exists('ruby') is True
+
+
+def test_exe_exists_false_if_shim(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/foo/shims/ruby')
+ assert helpers.exe_exists('ruby') is False
+
+
+def test_exe_exists_false_if_homedir(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/home/me/somedir/ruby')
+ assert helpers.exe_exists('ruby') is False
+
+
+def test_exe_exists_commonpath_raises_ValueError(find_exe_mck, homedir_mck):
+ find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby')
+ with mock.patch.object(os.path, 'commonpath', side_effect=ValueError):
+ assert helpers.exe_exists('ruby') is True
+
+
+def test_exe_exists_true_when_homedir_is_slash(find_exe_mck):
+ find_exe_mck.return_value = os.path.normpath('/usr/bin/ruby')
+ with mock.patch.object(os.path, 'expanduser', return_value=os.sep):
+ assert helpers.exe_exists('ruby') is True
+
+
def test_basic_get_default_version():
assert helpers.basic_get_default_version() == C.DEFAULT
diff --git a/tests/languages/node_test.py b/tests/languages/node_test.py
index fd30046..8e52268 100644
--- a/tests/languages/node_test.py
+++ b/tests/languages/node_test.py
@@ -1,14 +1,21 @@
+import json
+import os
+import shutil
import sys
from unittest import mock
import pytest
import pre_commit.constants as C
+from pre_commit import envcontext
from pre_commit import parse_shebang
-from pre_commit.languages.node import get_default_version
+from pre_commit.languages import node
+from pre_commit.prefix import Prefix
+from pre_commit.util import cmd_output
+from testing.util import xfailif_windows
-ACTUAL_GET_DEFAULT_VERSION = get_default_version.__wrapped__
+ACTUAL_GET_DEFAULT_VERSION = node.get_default_version.__wrapped__
@pytest.fixture
@@ -45,3 +52,57 @@ def test_uses_default_when_node_and_npm_are_not_available(find_exe_mck):
def test_sets_default_on_windows(find_exe_mck):
find_exe_mck.return_value = '/path/to/exe'
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_healthy_system_node(tmpdir):
+ tmpdir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
+
+ prefix = Prefix(str(tmpdir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_unhealthy_if_system_node_goes_missing(tmpdir):
+ bin_dir = tmpdir.join('bin').ensure_dir()
+ node_bin = bin_dir.join('node')
+ node_bin.mksymlinkto(shutil.which('node'))
+
+ prefix_dir = tmpdir.join('prefix').ensure_dir()
+ prefix_dir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
+
+ path = ('PATH', (str(bin_dir), os.pathsep, envcontext.Var('PATH')))
+ with envcontext.envcontext((path,)):
+ prefix = Prefix(str(prefix_dir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+ node_bin.remove()
+ assert not node.healthy(prefix, 'system')
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_installs_without_links_outside_env(tmpdir):
+ tmpdir.join('bin/main.js').ensure().write(
+ '#!/usr/bin/env node\n'
+ '_ = require("lodash"); console.log("success!")\n',
+ )
+ tmpdir.join('package.json').write(
+ json.dumps({
+ 'name': 'foo',
+ 'version': '0.0.1',
+ 'bin': {'foo': './bin/main.js'},
+ 'dependencies': {'lodash': '*'},
+ }),
+ )
+
+ prefix = Prefix(str(tmpdir))
+ node.install_environment(prefix, 'system', ())
+ assert node.healthy(prefix, 'system')
+
+ # this directory shouldn't exist, make sure we succeed without it existing
+ cmd_output('rm', '-rf', str(tmpdir.join('node_modules')))
+
+ with node.in_env(prefix, 'system'):
+ assert cmd_output('foo')[1] == 'success!\n'
diff --git a/tests/languages/pygrep_test.py b/tests/languages/pygrep_test.py
index cabea22..d8bacc4 100644
--- a/tests/languages/pygrep_test.py
+++ b/tests/languages/pygrep_test.py
@@ -8,6 +8,9 @@ def some_files(tmpdir):
tmpdir.join('f1').write_binary(b'foo\nbar\n')
tmpdir.join('f2').write_binary(b'[INFO] hi\n')
tmpdir.join('f3').write_binary(b"with'quotes\n")
+ tmpdir.join('f4').write_binary(b'foo\npattern\nbar\n')
+ tmpdir.join('f5').write_binary(b'[INFO] hi\npattern\nbar')
+ tmpdir.join('f6').write_binary(b"pattern\nbarwith'foo\n")
with tmpdir.as_cwd():
yield
@@ -23,42 +26,99 @@ def some_files(tmpdir):
("h'q", 1, "f3:1:with'quotes\n"),
),
)
-def test_main(some_files, cap_out, pattern, expected_retcode, expected_out):
+def test_main(cap_out, pattern, expected_retcode, expected_out):
ret = pygrep.main((pattern, 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == expected_retcode
assert out == expected_out
-def test_ignore_case(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_line_no_match(cap_out):
+ ret = pygrep.main(('pattern\nbar', 'f4', 'f5', 'f6', '--negate'))
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f4\nf5\nf6\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_line_two_match(cap_out):
+ ret = pygrep.main(('foo', 'f4', 'f5', 'f6', '--negate'))
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f5\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_line_all_match(cap_out):
+ ret = pygrep.main(('pattern', 'f4', 'f5', 'f6', '--negate'))
+ out = cap_out.get()
+ assert ret == 0
+ assert out == ''
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_file_no_match(cap_out):
+ ret = pygrep.main(('baz', 'f4', 'f5', 'f6', '--negate', '--multiline'))
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f4\nf5\nf6\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_file_one_match(cap_out):
+ ret = pygrep.main(
+ ('foo\npattern', 'f4', 'f5', 'f6', '--negate', '--multiline'),
+ )
+ out = cap_out.get()
+ assert ret == 1
+ assert out == 'f5\nf6\n'
+
+
+@pytest.mark.usefixtures('some_files')
+def test_negate_by_file_all_match(cap_out):
+ ret = pygrep.main(
+ ('pattern\nbar', 'f4', 'f5', 'f6', '--negate', '--multiline'),
+ )
+ out = cap_out.get()
+ assert ret == 0
+ assert out == ''
+
+
+@pytest.mark.usefixtures('some_files')
+def test_ignore_case(cap_out):
ret = pygrep.main(('--ignore-case', 'info', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f2:1:[INFO] hi\n'
-def test_multiline(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline(cap_out):
ret = pygrep.main(('--multiline', r'foo\nbar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f1:1:foo\nbar\n'
-def test_multiline_line_number(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline_line_number(cap_out):
ret = pygrep.main(('--multiline', r'ar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f1:2:bar\n'
-def test_multiline_dotall_flag_is_enabled(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline_dotall_flag_is_enabled(cap_out):
ret = pygrep.main(('--multiline', r'o.*bar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
assert out == 'f1:1:foo\nbar\n'
-def test_multiline_multiline_flag_is_enabled(some_files, cap_out):
+@pytest.mark.usefixtures('some_files')
+def test_multiline_multiline_flag_is_enabled(cap_out):
ret = pygrep.main(('--multiline', r'foo$.*bar', 'f1', 'f2', 'f3'))
out = cap_out.get()
assert ret == 1
diff --git a/tests/languages/python_test.py b/tests/languages/python_test.py
index 29c5a9b..cfe1483 100644
--- a/tests/languages/python_test.py
+++ b/tests/languages/python_test.py
@@ -36,13 +36,14 @@ def test_norm_version_expanduser():
def test_norm_version_of_default_is_sys_executable():
- assert python.norm_version('default') == os.path.realpath(sys.executable)
+ assert python.norm_version('default') is None
@pytest.mark.parametrize('v', ('python3.6', 'python3', 'python'))
def test_sys_executable_matches(v):
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
assert python._sys_executable_matches(v)
+ assert python.norm_version(v) is None
@pytest.mark.parametrize('v', ('notpython', 'python3.x'))
diff --git a/tests/languages/ruby_test.py b/tests/languages/ruby_test.py
index 853bb73..6c0c9e5 100644
--- a/tests/languages/ruby_test.py
+++ b/tests/languages/ruby_test.py
@@ -30,23 +30,45 @@ def test_uses_system_if_both_gem_and_ruby_are_available(find_exe_mck):
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
+@pytest.fixture
+def fake_gem_prefix(tmpdir):
+ gemspec = '''\
+Gem::Specification.new do |s|
+ s.name = 'pre_commit_dummy_package'
+ s.version = '0.0.0'
+ s.summary = 'dummy gem for pre-commit hooks'
+ s.authors = ['Anthony Sottile']
+end
+'''
+ tmpdir.join('dummy_gem.gemspec').write(gemspec)
+ yield Prefix(tmpdir)
+
+
+@xfailif_windows # pragma: win32 no cover
+def test_install_ruby_system(fake_gem_prefix):
+ ruby.install_environment(fake_gem_prefix, 'system', ())
+
+ # Should be able to activate and use rbenv install
+ with ruby.in_env(fake_gem_prefix, 'system'):
+ _, out, _ = cmd_output('gem', 'list')
+ assert 'pre_commit_dummy_package' in out
+
+
@xfailif_windows # pragma: win32 no cover
-def test_install_rbenv(tempdir_factory):
- prefix = Prefix(tempdir_factory.get())
- ruby._install_rbenv(prefix, C.DEFAULT)
+def test_install_ruby_default(fake_gem_prefix):
+ ruby.install_environment(fake_gem_prefix, C.DEFAULT, ())
# Should have created rbenv directory
- assert os.path.exists(prefix.path('rbenv-default'))
+ assert os.path.exists(fake_gem_prefix.path('rbenv-default'))
# Should be able to activate using our script and access rbenv
- with ruby.in_env(prefix, 'default'):
+ with ruby.in_env(fake_gem_prefix, 'default'):
cmd_output('rbenv', '--help')
@xfailif_windows # pragma: win32 no cover
-def test_install_rbenv_with_version(tempdir_factory):
- prefix = Prefix(tempdir_factory.get())
- ruby._install_rbenv(prefix, version='1.9.3p547')
+def test_install_ruby_with_version(fake_gem_prefix):
+ ruby.install_environment(fake_gem_prefix, '2.7.2', ())
# Should be able to activate and use rbenv install
- with ruby.in_env(prefix, '1.9.3p547'):
+ with ruby.in_env(fake_gem_prefix, '2.7.2'):
cmd_output('rbenv', 'install', '--help')
diff --git a/tests/main_test.py b/tests/main_test.py
index f7abeeb..6738df6 100644
--- a/tests/main_test.py
+++ b/tests/main_test.py
@@ -6,7 +6,7 @@ import pytest
import pre_commit.constants as C
from pre_commit import main
-from pre_commit.error_handler import FatalError
+from pre_commit.errors import FatalError
from testing.auto_namedtuple import auto_namedtuple
diff --git a/tests/repository_test.py b/tests/repository_test.py
index 84e4da9..3d5093d 100644
--- a/tests/repository_test.py
+++ b/tests/repository_test.py
@@ -1,5 +1,4 @@
import os.path
-import re
import shutil
import sys
from typing import Any
@@ -8,6 +7,7 @@ from unittest import mock
import cfgv
import pytest
+import re_assert
import pre_commit.constants as C
from pre_commit.clientlib import CONFIG_SCHEMA
@@ -31,6 +31,7 @@ from testing.fixtures import make_repo
from testing.fixtures import modify_manifest
from testing.util import cwd
from testing.util import get_resource_path
+from testing.util import skipif_cant_run_coursier
from testing.util import skipif_cant_run_docker
from testing.util import skipif_cant_run_swift
from testing.util import xfailif_windows
@@ -94,8 +95,8 @@ def test_conda_with_additional_dependencies_hook(tempdir_factory, store):
config_kwargs={
'hooks': [{
'id': 'additional-deps',
- 'args': ['-c', 'import mccabe; print("OK")'],
- 'additional_dependencies': ['mccabe'],
+ 'args': ['-c', 'import tzdata; print("OK")'],
+ 'additional_dependencies': ['python-tzdata'],
}],
},
)
@@ -109,8 +110,8 @@ def test_local_conda_additional_dependencies(store):
'name': 'local-conda',
'entry': 'python',
'language': 'conda',
- 'args': ['-c', 'import mccabe; print("OK")'],
- 'additional_dependencies': ['mccabe'],
+ 'args': ['-c', 'import tzdata; print("OK")'],
+ 'additional_dependencies': ['python-tzdata'],
}],
}
hook = _get_hook(config, store, 'local-conda')
@@ -195,6 +196,15 @@ def test_versioned_python_hook(tempdir_factory, store):
)
+@skipif_cant_run_coursier # pragma: win32 no cover
+def test_run_a_coursier_hook(tempdir_factory, store):
+ _test_hook_repo(
+ tempdir_factory, store, 'coursier_hooks_repo',
+ 'echo-java',
+ ['Hello World from coursier'], b'Hello World from coursier\n',
+ )
+
+
@skipif_cant_run_docker # pragma: win32 no cover
def test_run_a_docker_hook(tempdir_factory, store):
_test_hook_repo(
@@ -843,12 +853,12 @@ def test_too_new_version(tempdir_factory, store, fake_log_handler):
with pytest.raises(SystemExit):
_get_hook(config, store, 'bash_hook')
msg = fake_log_handler.handle.call_args[0][0].msg
- assert re.match(
+ pattern = re_assert.Matches(
r'^The hook `bash_hook` requires pre-commit version 999\.0\.0 but '
r'version \d+\.\d+\.\d+ is installed. '
r'Perhaps run `pip install --upgrade pre-commit`\.$',
- msg,
)
+ pattern.assert_matches(msg)
@pytest.mark.parametrize('version', ('0.1.0', C.VERSION))
@@ -917,3 +927,17 @@ def test_local_perl_additional_dependencies(store):
ret, out = _hook_run(hook, (), color=False)
assert ret == 0
assert _norm_out(out).startswith(b'This is perltidy, v20200110')
+
+
+@pytest.mark.parametrize(
+ 'repo',
+ (
+ 'dotnet_hooks_csproj_repo',
+ 'dotnet_hooks_sln_repo',
+ ),
+)
+def test_dotnet_hook(tempdir_factory, store, repo):
+ _test_hook_repo(
+ tempdir_factory, store, repo,
+ 'dotnet example hook', [], b'Hello from dotnet!\n',
+ )
diff --git a/tests/xargs_test.py b/tests/xargs_test.py
index 1fc9207..4f6136e 100644
--- a/tests/xargs_test.py
+++ b/tests/xargs_test.py
@@ -160,7 +160,7 @@ def test_xargs_concurrency():
assert ret == 0
pids = stdout.splitlines()
assert len(pids) == 5
- # It would take 0.5*5=2.5 seconds ot run all of these in serial, so if it
+ # It would take 0.5*5=2.5 seconds to run all of these in serial, so if it
# takes less, they must have run concurrently.
assert elapsed < 2.5