1
0
Fork 0

Merging upstream version 3.5.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-09 21:45:39 +01:00
parent 0a2ab56a91
commit 7c2b6aba2b
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
14 changed files with 130 additions and 106 deletions

View file

@ -293,7 +293,7 @@ def test_verbose_duration(cap_out, store, in_git_dir, t1, t2, expected):
write_config('.', {'repo': 'meta', 'hooks': [{'id': 'identity'}]})
cmd_output('git', 'add', '.')
opts = run_opts(verbose=True)
with mock.patch.object(time, 'time', side_effect=(t1, t2)):
with mock.patch.object(time, 'monotonic', side_effect=(t1, t2)):
ret, printed = _do_run(cap_out, store, str(in_git_dir), opts)
assert ret == 0
assert expected in printed
@ -1127,8 +1127,8 @@ def test_classifier_empty_types_or(tmpdir):
types_or=[],
exclude_types=[],
)
assert for_symlink == ['foo']
assert for_file == ['bar']
assert tuple(for_symlink) == ('foo',)
assert tuple(for_file) == ('bar',)
@pytest.fixture
@ -1142,33 +1142,33 @@ def some_filenames():
def test_include_exclude_base_case(some_filenames):
ret = filter_by_include_exclude(some_filenames, '', '^$')
assert ret == [
assert tuple(ret) == (
'.pre-commit-hooks.yaml',
'pre_commit/git.py',
'pre_commit/main.py',
]
)
def test_matches_broken_symlink(tmpdir):
with tmpdir.as_cwd():
os.symlink('does-not-exist', 'link')
ret = filter_by_include_exclude({'link'}, '', '^$')
assert ret == ['link']
assert tuple(ret) == ('link',)
def test_include_exclude_total_match(some_filenames):
ret = filter_by_include_exclude(some_filenames, r'^.*\.py$', '^$')
assert ret == ['pre_commit/git.py', 'pre_commit/main.py']
assert tuple(ret) == ('pre_commit/git.py', 'pre_commit/main.py')
def test_include_exclude_does_search_instead_of_match(some_filenames):
ret = filter_by_include_exclude(some_filenames, r'\.yaml$', '^$')
assert ret == ['.pre-commit-hooks.yaml']
assert tuple(ret) == ('.pre-commit-hooks.yaml',)
def test_include_exclude_exclude_removes_files(some_filenames):
ret = filter_by_include_exclude(some_filenames, '', r'\.py$')
assert ret == ['.pre-commit-hooks.yaml']
assert tuple(ret) == ('.pre-commit-hooks.yaml',)
def test_args_hook_only(cap_out, store, repo_with_passing_hook):

View file

@ -43,7 +43,7 @@ def _run_try_repo(tempdir_factory, **kwargs):
def test_try_repo_repo_only(cap_out, tempdir_factory):
with mock.patch.object(time, 'time', return_value=0.0):
with mock.patch.object(time, 'monotonic', return_value=0.0):
_run_try_repo(tempdir_factory, verbose=True)
start, config, rest = _get_out(cap_out)
assert start == ''

View file

@ -1,6 +1,5 @@
from __future__ import annotations
import multiprocessing
import os.path
import sys
from unittest import mock
@ -10,6 +9,7 @@ import pytest
import pre_commit.constants as C
from pre_commit import lang_base
from pre_commit import parse_shebang
from pre_commit import xargs
from pre_commit.prefix import Prefix
from pre_commit.util import CalledProcessError
@ -30,19 +30,6 @@ def homedir_mck():
yield
@pytest.fixture
def no_sched_getaffinity():
# Simulates an OS without os.sched_getaffinity available (mac/windows)
# https://docs.python.org/3/library/os.html#interface-to-the-scheduler
with mock.patch.object(
os,
'sched_getaffinity',
create=True,
side_effect=AttributeError,
):
yield
def test_exe_exists_does_not_exist(find_exe_mck, homedir_mck):
find_exe_mck.return_value = None
assert lang_base.exe_exists('ruby') is False
@ -129,40 +116,23 @@ def test_no_env_noop(tmp_path):
assert before == inside == after
def test_target_concurrency_sched_getaffinity(no_sched_getaffinity):
with mock.patch.object(
os,
'sched_getaffinity',
return_value=set(range(345)),
):
with mock.patch.dict(os.environ, clear=True):
assert lang_base.target_concurrency() == 345
@pytest.fixture
def cpu_count_mck():
with mock.patch.object(xargs, 'cpu_count', return_value=4):
yield
def test_target_concurrency_without_sched_getaffinity(no_sched_getaffinity):
with mock.patch.object(multiprocessing, 'cpu_count', return_value=123):
with mock.patch.dict(os.environ, {}, clear=True):
assert lang_base.target_concurrency() == 123
def test_target_concurrency_testing_env_var():
with mock.patch.dict(
os.environ, {'PRE_COMMIT_NO_CONCURRENCY': '1'}, clear=True,
):
assert lang_base.target_concurrency() == 1
def test_target_concurrency_on_travis():
with mock.patch.dict(os.environ, {'TRAVIS': '1'}, clear=True):
assert lang_base.target_concurrency() == 2
def test_target_concurrency_cpu_count_not_implemented(no_sched_getaffinity):
with mock.patch.object(
multiprocessing, 'cpu_count', side_effect=NotImplementedError,
):
with mock.patch.dict(os.environ, {}, clear=True):
assert lang_base.target_concurrency() == 1
@pytest.mark.parametrize(
('var', 'expected'),
(
('PRE_COMMIT_NO_CONCURRENCY', 1),
('TRAVIS', 2),
(None, 4),
),
)
def test_target_concurrency(cpu_count_mck, var, expected):
with mock.patch.dict(os.environ, {var: '1'} if var else {}, clear=True):
assert lang_base.target_concurrency() == expected
def test_shuffled_is_deterministic():

View file

@ -111,11 +111,11 @@ def test_golang_versioned(tmp_path):
tmp_path,
golang,
'go version',
version='1.18.4',
version='1.21.1',
)
assert ret == 0
assert out.startswith(b'go version go1.18.4')
assert out.startswith(b'go version go1.21.1')
def test_local_golang_additional_deps(tmp_path):

View file

@ -139,7 +139,7 @@ def test_node_with_user_config_set(tmp_path):
test_node_hook_system(tmp_path)
@pytest.mark.parametrize('version', (C.DEFAULT, '18.13.0'))
@pytest.mark.parametrize('version', (C.DEFAULT, '18.14.0'))
def test_node_hook_versions(tmp_path, version):
_make_hello_world(tmp_path)
ret = run_language(tmp_path, node, 'node-hello', version=version)

View file

@ -133,17 +133,17 @@ def test_normalize_cmd_PATH():
def test_normalize_cmd_shebang(in_tmpdir):
echo = _echo_exe().replace(os.sep, '/')
path = write_executable(echo)
assert parse_shebang.normalize_cmd((path,)) == (echo, path)
us = sys.executable.replace(os.sep, '/')
path = write_executable(us)
assert parse_shebang.normalize_cmd((path,)) == (us, path)
def test_normalize_cmd_PATH_shebang_full_path(in_tmpdir):
echo = _echo_exe().replace(os.sep, '/')
path = write_executable(echo)
us = sys.executable.replace(os.sep, '/')
path = write_executable(us)
with bin_on_path():
ret = parse_shebang.normalize_cmd(('run',))
assert ret == (echo, os.path.abspath(path))
assert ret == (us, os.path.abspath(path))
def test_normalize_cmd_PATH_shebang_PATH(in_tmpdir):

View file

@ -1,6 +1,7 @@
from __future__ import annotations
import concurrent.futures
import multiprocessing
import os
import sys
import time
@ -12,6 +13,40 @@ from pre_commit import parse_shebang
from pre_commit import xargs
def test_cpu_count_sched_getaffinity_exists():
with mock.patch.object(
os, 'sched_getaffinity', create=True, return_value=set(range(345)),
):
assert xargs.cpu_count() == 345
@pytest.fixture
def no_sched_getaffinity():
# Simulates an OS without os.sched_getaffinity available (mac/windows)
# https://docs.python.org/3/library/os.html#interface-to-the-scheduler
with mock.patch.object(
os,
'sched_getaffinity',
create=True,
side_effect=AttributeError,
):
yield
def test_cpu_count_multiprocessing_cpu_count_implemented(no_sched_getaffinity):
with mock.patch.object(multiprocessing, 'cpu_count', return_value=123):
assert xargs.cpu_count() == 123
def test_cpu_count_multiprocessing_cpu_count_not_implemented(
no_sched_getaffinity,
):
with mock.patch.object(
multiprocessing, 'cpu_count', side_effect=NotImplementedError,
):
assert xargs.cpu_count() == 1
@pytest.mark.parametrize(
('env', 'expected'),
(