1
0
Fork 0

Merging upstream version 2.21.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-09 21:35:37 +01:00
parent 08d9b01ff9
commit 54d13e9018
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
48 changed files with 534 additions and 210 deletions

View file

@ -15,6 +15,8 @@ from pre_commit.clientlib import DEFAULT_LANGUAGE_VERSION
from pre_commit.clientlib import MANIFEST_SCHEMA
from pre_commit.clientlib import META_HOOK_DICT
from pre_commit.clientlib import MigrateShaToRev
from pre_commit.clientlib import OptionalSensibleRegexAtHook
from pre_commit.clientlib import OptionalSensibleRegexAtTop
from pre_commit.clientlib import validate_config_main
from pre_commit.clientlib import validate_manifest_main
from testing.fixtures import sample_local_config
@ -261,6 +263,27 @@ def test_warn_mutable_rev_conditional():
cfgv.validate(config_obj, CONFIG_REPO_DICT)
@pytest.mark.parametrize(
'validator_cls',
(
OptionalSensibleRegexAtHook,
OptionalSensibleRegexAtTop,
),
)
def test_sensible_regex_validators_dont_pass_none(validator_cls):
key = 'files'
with pytest.raises(cfgv.ValidationError) as excinfo:
validator = validator_cls(key, cfgv.check_string)
validator.check({key: None})
assert str(excinfo.value) == (
'\n'
f'==> At key: {key}'
'\n'
'=====> Expected string got NoneType'
)
@pytest.mark.parametrize(
('regex', 'warning'),
(
@ -296,6 +319,22 @@ def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning):
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]
def test_validate_optional_sensible_regex_at_local_hook(caplog):
config_obj = sample_local_config()
config_obj['hooks'][0]['files'] = 'dir/*.py'
cfgv.validate(config_obj, CONFIG_REPO_DICT)
assert caplog.record_tuples == [
(
'pre_commit',
logging.WARNING,
"The 'files' field in hook 'do_not_commit' is a regex, not a glob "
"-- matching '/*' probably isn't what you want here",
),
]
@pytest.mark.parametrize(
('regex', 'warning'),
(

View file

@ -135,7 +135,7 @@ def test_init_templatedir_skip_on_missing_config(
retcode, output = git_commit(
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
retcode=None,
check=False,
)
assert retcode == commit_retcode

View file

@ -126,7 +126,7 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
cmd_output('git', 'add', touch_file)
return git_commit(
fn=cmd_output_mocked_pre_commit_home,
retcode=None,
check=False,
tempdir_factory=tempdir_factory,
**kwargs,
)
@ -286,7 +286,7 @@ def test_environment_not_sourced(tempdir_factory, store):
'GIT_AUTHOR_EMAIL': os.environ['GIT_AUTHOR_EMAIL'],
'GIT_COMMITTER_EMAIL': os.environ['GIT_COMMITTER_EMAIL'],
},
retcode=None,
check=False,
)
assert ret == 1
assert out == (
@ -551,7 +551,7 @@ def _get_push_output(tempdir_factory, remote='origin', opts=()):
return cmd_output_mocked_pre_commit_home(
'git', 'push', remote, 'HEAD:new_branch', *opts,
tempdir_factory=tempdir_factory,
retcode=None,
check=False,
)[:2]

View file

@ -536,6 +536,13 @@ def test_merge_conflict(cap_out, store, in_merge_conflict):
assert b'Unmerged files. Resolve before committing.' in printed
def test_files_during_merge_conflict(cap_out, store, in_merge_conflict):
opts = run_opts(files=['placeholder'])
ret, printed = _do_run(cap_out, store, in_merge_conflict, opts)
assert ret == 0
assert b'Bash hook' in printed
def test_merge_conflict_modified(cap_out, store, in_merge_conflict):
# Touch another file so we have unstaged non-conflicting things
assert os.path.exists('placeholder')
@ -635,6 +642,32 @@ def test_skip_bypasses_installation(cap_out, store, repo_with_passing_hook):
assert ret == 0
def test_skip_alias_bypasses_installation(
cap_out, store, repo_with_passing_hook,
):
config = {
'repo': 'local',
'hooks': [
{
'id': 'skipme',
'name': 'skipme-1',
'alias': 'skipme-1',
'entry': 'skipme',
'language': 'python',
'additional_dependencies': ['/pre-commit-does-not-exist'],
},
],
}
add_config_to_repo(repo_with_passing_hook, config)
ret, printed = _do_run(
cap_out, store, repo_with_passing_hook,
run_opts(all_files=True),
{'SKIP': 'skipme-1'},
)
assert ret == 0
def test_hook_id_not_in_non_verbose_output(
cap_out, store, repo_with_passing_hook,
):
@ -685,7 +718,7 @@ def test_non_ascii_hook_id(repo_with_passing_hook, tempdir_factory):
with cwd(repo_with_passing_hook):
_, stdout, _ = cmd_output_mocked_pre_commit_home(
sys.executable, '-m', 'pre_commit.main', 'run', '',
retcode=None, tempdir_factory=tempdir_factory,
check=False, tempdir_factory=tempdir_factory,
)
assert 'UnicodeDecodeError' not in stdout
# Doesn't actually happen, but a reasonable assertion
@ -704,7 +737,7 @@ def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory):
_, out = git_commit(
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
retcode=None,
check=False,
)
assert 'UnicodeEncodeError' not in out
# Doesn't actually happen, but a reasonable assertion

View file

@ -68,7 +68,7 @@ def _make_conflict():
bar_only_file.write('bar')
cmd_output('git', 'add', 'bar_only_file')
git_commit(msg=_make_conflict.__name__)
cmd_output('git', 'merge', 'foo', retcode=None)
cmd_output('git', 'merge', 'foo', check=False)
@pytest.fixture

View file

@ -162,7 +162,7 @@ def test_error_handler_non_ascii_exception(mock_store_dir):
def test_error_handler_non_utf8_exception(mock_store_dir):
with pytest.raises(SystemExit):
with error_handler.error_handler():
raise CalledProcessError(1, ('exe',), 0, b'error: \xa0\xe1', b'')
raise CalledProcessError(1, ('exe',), b'error: \xa0\xe1', b'')
def test_error_handler_non_stringable_exception(mock_store_dir):
@ -183,10 +183,11 @@ 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=3,
check=False,
tempdir_factory=tempdir_factory,
pre_commit_home=pre_commit_home,
)
assert ret == 3
log_file = os.path.join(pre_commit_home, 'pre-commit.log')
out_lines = out.splitlines()
assert out_lines[-2] == 'An unexpected error has occurred: ValueError: ☃'

View file

@ -104,7 +104,7 @@ def test_is_in_merge_conflict_submodule(in_conflicting_submodule):
def test_cherry_pick_conflict(in_merge_conflict):
cmd_output('git', 'merge', '--abort')
foo_ref = cmd_output('git', 'rev-parse', 'foo')[1].strip()
cmd_output('git', 'cherry-pick', foo_ref, retcode=None)
cmd_output('git', 'cherry-pick', foo_ref, check=False)
assert git.is_in_merge_conflict() is False

View file

@ -178,6 +178,6 @@ def test_get_docker_path_in_docker_windows(in_docker):
def test_get_docker_path_in_docker_docker_in_docker(in_docker):
# won't be able to discover "self" container in true docker-in-docker
err = CalledProcessError(1, (), 0, b'', b'')
err = CalledProcessError(1, (), b'', b'')
with mock.patch.object(docker, 'cmd_output_b', side_effect=err):
assert docker._get_docker_path('/project') == '/project'

View file

@ -6,6 +6,7 @@ import pytest
from pre_commit import envcontext
from pre_commit.languages import r
from pre_commit.util import win_exe
from testing.fixtures import make_config_from_repo
from testing.fixtures import make_repo
from tests.repository_test import _get_hook_no_install
@ -133,7 +134,7 @@ def test_r_parsing_file_local(tempdir_factory, store):
def test_rscript_exec_relative_to_r_home():
expected = os.path.join('r_home_dir', 'bin', 'Rscript')
expected = os.path.join('r_home_dir', 'bin', win_exe('Rscript'))
with envcontext.envcontext((('R_HOME', 'r_home_dir'),)):
assert r._rscript_exec() == expected

View file

@ -71,10 +71,10 @@ def test_install_ruby_default(fake_gem_prefix):
@xfailif_windows # pragma: win32 no cover
def test_install_ruby_with_version(fake_gem_prefix):
ruby.install_environment(fake_gem_prefix, '2.7.2', ())
ruby.install_environment(fake_gem_prefix, '3.1.0', ())
# Should be able to activate and use rbenv install
with ruby.in_env(fake_gem_prefix, '2.7.2'):
with ruby.in_env(fake_gem_prefix, '3.1.0'):
cmd_output('rbenv', 'install', '--help')

View file

@ -0,0 +1,90 @@
from __future__ import annotations
from unittest import mock
import pytest
import pre_commit.constants as C
from pre_commit import parse_shebang
from pre_commit.languages import rust
from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output
ACTUAL_GET_DEFAULT_VERSION = rust.get_default_version.__wrapped__
@pytest.fixture
def cmd_output_b_mck():
with mock.patch.object(rust, 'cmd_output_b') as mck:
yield mck
def test_sets_system_when_rust_is_available(cmd_output_b_mck):
cmd_output_b_mck.return_value = (0, b'', b'')
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
def test_uses_default_when_rust_is_not_available(cmd_output_b_mck):
cmd_output_b_mck.return_value = (127, b'', b'error: not found')
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
@pytest.mark.parametrize('language_version', (C.DEFAULT, '1.56.0'))
def test_installs_with_bootstrapped_rustup(tmpdir, language_version):
tmpdir.join('src', 'main.rs').ensure().write(
'fn main() {\n'
' println!("Hello, world!");\n'
'}\n',
)
tmpdir.join('Cargo.toml').ensure().write(
'[package]\n'
'name = "hello_world"\n'
'version = "0.1.0"\n'
'edition = "2021"\n',
)
prefix = Prefix(str(tmpdir))
find_executable_exes = []
original_find_executable = parse_shebang.find_executable
def mocked_find_executable(exe: str) -> str | None:
"""
Return `None` the first time `find_executable` is called to ensure
that the bootstrapping code is executed, then just let the function
work as normal.
Also log the arguments to ensure that everything works as expected.
"""
find_executable_exes.append(exe)
if len(find_executable_exes) == 1:
return None
return original_find_executable(exe)
with mock.patch.object(parse_shebang, 'find_executable') as find_exe_mck:
find_exe_mck.side_effect = mocked_find_executable
rust.install_environment(prefix, language_version, ())
assert find_executable_exes == ['rustup', 'rustup', 'cargo']
with rust.in_env(prefix, language_version):
assert cmd_output('hello_world')[1] == 'Hello, world!\n'
def test_installs_with_existing_rustup(tmpdir):
tmpdir.join('src', 'main.rs').ensure().write(
'fn main() {\n'
' println!("Hello, world!");\n'
'}\n',
)
tmpdir.join('Cargo.toml').ensure().write(
'[package]\n'
'name = "hello_world"\n'
'version = "0.1.0"\n'
'edition = "2021"\n',
)
prefix = Prefix(str(tmpdir))
assert parse_shebang.find_executable('rustup') is not None
rust.install_environment(prefix, '1.56.0', ())
with rust.in_env(prefix, '1.56.0'):
assert cmd_output('hello_world')[1] == 'Hello, world!\n'

View file

@ -17,6 +17,8 @@ from testing.util import cwd
def _args(**kwargs):
kwargs.setdefault('command', 'help')
kwargs.setdefault('config', C.CONFIG_FILE)
if kwargs['command'] in {'run', 'try-repo'}:
kwargs.setdefault('commit_msg_filename', None)
return argparse.Namespace(**kwargs)
@ -35,13 +37,24 @@ def test_adjust_args_and_chdir_noop(in_git_dir):
def test_adjust_args_and_chdir_relative_things(in_git_dir):
in_git_dir.join('foo/cfg.yaml').ensure()
in_git_dir.join('foo').chdir()
with in_git_dir.join('foo').as_cwd():
args = _args(command='run', files=['f1', 'f2'], config='cfg.yaml')
main._adjust_args_and_chdir(args)
assert os.getcwd() == in_git_dir
assert args.config == os.path.join('foo', 'cfg.yaml')
assert args.files == [
os.path.join('foo', 'f1'),
os.path.join('foo', 'f2'),
]
args = _args(command='run', files=['f1', 'f2'], config='cfg.yaml')
main._adjust_args_and_chdir(args)
assert os.getcwd() == in_git_dir
assert args.config == os.path.join('foo', 'cfg.yaml')
assert args.files == [os.path.join('foo', 'f1'), os.path.join('foo', 'f2')]
def test_adjust_args_and_chdir_relative_commit_msg(in_git_dir):
in_git_dir.join('foo/cfg.yaml').ensure()
with in_git_dir.join('foo').as_cwd():
args = _args(command='run', files=[], commit_msg_filename='t.txt')
main._adjust_args_and_chdir(args)
assert os.getcwd() == in_git_dir
assert args.commit_msg_filename == os.path.join('foo', 't.txt')
@pytest.mark.skipif(os.name != 'nt', reason='windows feature')
@ -56,24 +69,22 @@ def test_install_on_subst(in_git_dir, store): # pragma: posix no cover
def test_adjust_args_and_chdir_non_relative_config(in_git_dir):
in_git_dir.join('foo').ensure_dir().chdir()
args = _args()
main._adjust_args_and_chdir(args)
assert os.getcwd() == in_git_dir
assert args.config == C.CONFIG_FILE
with in_git_dir.join('foo').ensure_dir().as_cwd():
args = _args()
main._adjust_args_and_chdir(args)
assert os.getcwd() == in_git_dir
assert args.config == C.CONFIG_FILE
def test_adjust_args_try_repo_repo_relative(in_git_dir):
in_git_dir.join('foo').ensure_dir().chdir()
args = _args(command='try-repo', repo='../foo', files=[])
assert args.repo is not None
assert os.path.exists(args.repo)
main._adjust_args_and_chdir(args)
assert os.getcwd() == in_git_dir
assert os.path.exists(args.repo)
assert args.repo == 'foo'
with in_git_dir.join('foo').ensure_dir().as_cwd():
args = _args(command='try-repo', repo='../foo', files=[])
assert args.repo is not None
assert os.path.exists(args.repo)
main._adjust_args_and_chdir(args)
assert os.getcwd() == in_git_dir
assert os.path.exists(args.repo)
assert args.repo == 'foo'
FNS = (

View file

@ -173,30 +173,20 @@ def test_python_venv(tempdir_factory, store):
)
@xfailif_windows # pragma: win32 no cover # no python 2 in GHA
def test_switch_language_versions_doesnt_clobber(tempdir_factory, store):
# We're using the python3 repo because it prints the python version
path = make_repo(tempdir_factory, 'python3_hooks_repo')
def run_on_version(version, expected_output):
config = make_config_from_repo(path)
config['hooks'][0]['language_version'] = version
hook = _get_hook(config, store, 'python3-hook')
ret, out = _hook_run(hook, [], color=False)
assert ret == 0
assert _norm_out(out) == expected_output
run_on_version('python2', b'2\n[]\nHello World\n')
run_on_version('python3', b'3\n[]\nHello World\n')
def test_versioned_python_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'python3_hooks_repo',
'python3-hook',
[os.devnull],
f'3\n[{os.devnull!r}]\nHello World\n'.encode(),
)
def test_language_versioned_python_hook(tempdir_factory, store):
# we patch this force virtualenv executing with `-p` since we can't
# reliably have multiple pythons available in CI
with mock.patch.object(
python,
'_sys_executable_matches',
return_value=False,
):
_test_hook_repo(
tempdir_factory, store, 'python3_hooks_repo',
'python3-hook',
[os.devnull],
f'3\n[{os.devnull!r}]\nHello World\n'.encode(),
)
@skipif_cant_run_coursier # pragma: win32 no cover
@ -345,7 +335,7 @@ def test_run_versioned_ruby_hook(tempdir_factory, store):
tempdir_factory, store, 'ruby_versioned_hooks_repo',
'ruby_hook',
[os.devnull],
b'2.5.1\nHello world from a ruby hook\n',
b'3.1.0\nHello world from a ruby hook\n',
)
@ -367,7 +357,7 @@ def test_run_ruby_hook_with_disable_shared_gems(
tempdir_factory, store, 'ruby_versioned_hooks_repo',
'ruby_hook',
[os.devnull],
b'2.5.1\nHello world from a ruby hook\n',
b'3.1.0\nHello world from a ruby hook\n',
)
@ -471,7 +461,7 @@ def test_additional_rust_cli_dependencies_installed(
hook = _get_hook(config, store, 'rust-hook')
binaries = os.listdir(
hook.prefix.path(
helpers.environment_dir(rust.ENVIRONMENT_DIR, C.DEFAULT), 'bin',
helpers.environment_dir(rust.ENVIRONMENT_DIR, 'system'), 'bin',
),
)
# normalize for windows
@ -485,12 +475,12 @@ def test_additional_rust_lib_dependencies_installed(
path = make_repo(tempdir_factory, 'rust_hooks_repo')
config = make_config_from_repo(path)
# A small rust package with no dependencies.
deps = ['shellharden:3.1.0']
deps = ['shellharden:3.1.0', 'git-version']
config['hooks'][0]['additional_dependencies'] = deps
hook = _get_hook(config, store, 'rust-hook')
binaries = os.listdir(
hook.prefix.path(
helpers.environment_dir(rust.ENVIRONMENT_DIR, C.DEFAULT), 'bin',
helpers.environment_dir(rust.ENVIRONMENT_DIR, 'system'), 'bin',
),
)
# normalize for windows
@ -883,7 +873,7 @@ def test_tags_on_repositories(in_tmpdir, tempdir_factory, store):
@pytest.fixture
def local_python_config():
# Make a "local" hooks repo that just installs our other hooks repo
repo_path = get_resource_path('python3_hooks_repo')
repo_path = get_resource_path('python_hooks_repo')
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
hooks = [
dict(hook, additional_dependencies=[repo_path]) for hook in manifest
@ -892,23 +882,12 @@ def local_python_config():
def test_local_python_repo(store, local_python_config):
hook = _get_hook(local_python_config, store, 'python3-hook')
hook = _get_hook(local_python_config, store, 'foo')
# language_version should have been adjusted to the interpreter version
assert hook.language_version != C.DEFAULT
ret, out = _hook_run(hook, ('filename',), color=False)
assert ret == 0
assert _norm_out(out) == b"3\n['filename']\nHello World\n"
@xfailif_windows # pragma: win32 no cover # no python2 in GHA
def test_local_python_repo_python2(store, local_python_config):
local_python_config['hooks'][0]['language_version'] = 'python2'
hook = _get_hook(local_python_config, store, 'python3-hook')
# language_version should have been adjusted to the interpreter version
assert hook.language_version != C.DEFAULT
ret, out = _hook_run(hook, ('filename',), color=False)
assert ret == 0
assert _norm_out(out) == b"2\n['filename']\nHello World\n"
assert _norm_out(out) == b"['filename']\nHello World\n"
def test_default_language_version(store, local_python_config):
@ -1052,6 +1031,7 @@ def test_local_perl_additional_dependencies(store):
'dotnet_hooks_csproj_repo',
'dotnet_hooks_sln_repo',
'dotnet_hooks_combo_repo',
'dotnet_hooks_csproj_prefix_repo',
),
)
def test_dotnet_hook(tempdir_factory, store, repo):

View file

@ -127,7 +127,7 @@ def test_clone_shallow_failure_fallback_to_complete(
# Force shallow clone failure
def fake_shallow_clone(self, *args, **kwargs):
raise CalledProcessError(1, (), 0, b'', None)
raise CalledProcessError(1, (), b'', None)
store._shallow_clone = fake_shallow_clone
ret = store.clone(path, rev)

View file

@ -18,11 +18,10 @@ from pre_commit.util import tmpdir
def test_CalledProcessError_str():
error = CalledProcessError(1, ('exe',), 0, b'output', b'errors')
error = CalledProcessError(1, ('exe',), b'output', b'errors')
assert str(error) == (
"command: ('exe',)\n"
'return code: 1\n'
'expected return code: 0\n'
'stdout:\n'
' output\n'
'stderr:\n'
@ -31,11 +30,10 @@ def test_CalledProcessError_str():
def test_CalledProcessError_str_nooutput():
error = CalledProcessError(1, ('exe',), 0, b'', b'')
error = CalledProcessError(1, ('exe',), b'', b'')
assert str(error) == (
"command: ('exe',)\n"
'return code: 1\n'
'expected return code: 0\n'
'stdout: (none)\n'
'stderr: (none)'
)
@ -83,14 +81,14 @@ def test_tmpdir():
def test_cmd_output_exe_not_found():
ret, out, _ = cmd_output('dne', retcode=None)
ret, out, _ = cmd_output('dne', check=False)
assert ret == 1
assert out == 'Executable `dne` not found'
@pytest.mark.parametrize('fn', (cmd_output_b, cmd_output_p))
def test_cmd_output_exe_not_found_bytes(fn):
ret, out, _ = fn('dne', retcode=None, stderr=subprocess.STDOUT)
ret, out, _ = fn('dne', check=False, stderr=subprocess.STDOUT)
assert ret == 1
assert out == b'Executable `dne` not found'
@ -101,7 +99,7 @@ def test_cmd_output_no_shebang(tmpdir, fn):
make_executable(f)
# previously this raised `OSError` -- the output is platform specific
ret, out, _ = fn(str(f), retcode=None, stderr=subprocess.STDOUT)
ret, out, _ = fn(str(f), check=False, stderr=subprocess.STDOUT)
assert ret == 1
assert isinstance(out, bytes)
assert out.endswith(b'\n')