mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Add a helper to modify config files under test
This commit is contained in:
parent
336939ef99
commit
be4d0a2742
2 changed files with 54 additions and 60 deletions
|
|
@ -48,6 +48,20 @@ def modify_manifest(path):
|
||||||
cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
|
cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def modify_config(path='.', commit=True):
|
||||||
|
"""Modify the config yielded by this context to write to
|
||||||
|
.pre-commit-config.yaml
|
||||||
|
"""
|
||||||
|
config_path = os.path.join(path, C.CONFIG_FILE)
|
||||||
|
config = ordered_load(io.open(config_path).read())
|
||||||
|
yield config
|
||||||
|
with io.open(config_path, 'w', encoding='UTF-8') as config_file:
|
||||||
|
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
|
||||||
|
if commit:
|
||||||
|
cmd_output('git', 'commit', '-am', 'update config', cwd=path)
|
||||||
|
|
||||||
|
|
||||||
def config_with_local_hooks():
|
def config_with_local_hooks():
|
||||||
return OrderedDict((
|
return OrderedDict((
|
||||||
('repo', 'local'),
|
('repo', 'local'),
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import sys
|
||||||
import mock
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
import pre_commit.constants as C
|
||||||
from pre_commit.commands.install_uninstall import install
|
from pre_commit.commands.install_uninstall import install
|
||||||
from pre_commit.commands.run import _get_skips
|
from pre_commit.commands.run import _get_skips
|
||||||
from pre_commit.commands.run import _has_unmerged_paths
|
from pre_commit.commands.run import _has_unmerged_paths
|
||||||
|
|
@ -24,6 +25,7 @@ from pre_commit.util import cwd
|
||||||
from testing.auto_namedtuple import auto_namedtuple
|
from testing.auto_namedtuple import auto_namedtuple
|
||||||
from testing.fixtures import add_config_to_repo
|
from testing.fixtures import add_config_to_repo
|
||||||
from testing.fixtures import make_consuming_repo
|
from testing.fixtures import make_consuming_repo
|
||||||
|
from testing.fixtures import modify_config
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture
|
@pytest.yield_fixture
|
||||||
|
|
@ -313,9 +315,8 @@ def test_multiple_hooks_same_id(
|
||||||
):
|
):
|
||||||
with cwd(repo_with_passing_hook):
|
with cwd(repo_with_passing_hook):
|
||||||
# Add bash hook on there again
|
# Add bash hook on there again
|
||||||
with io.open('.pre-commit-config.yaml', 'a+') as config_file:
|
with modify_config() as config:
|
||||||
config_file.write(' - id: bash_hook\n')
|
config[0]['hooks'].append({'id': 'bash_hook'})
|
||||||
cmd_output('git', 'add', '.pre-commit-config.yaml')
|
|
||||||
stage_a_file()
|
stage_a_file()
|
||||||
|
|
||||||
ret, output = _do_run(repo_with_passing_hook, _get_opts())
|
ret, output = _do_run(repo_with_passing_hook, _get_opts())
|
||||||
|
|
@ -343,12 +344,8 @@ def test_stdout_write_bug_py26(
|
||||||
repo_with_failing_hook, mock_out_store_directory, tempdir_factory,
|
repo_with_failing_hook, mock_out_store_directory, tempdir_factory,
|
||||||
):
|
):
|
||||||
with cwd(repo_with_failing_hook):
|
with cwd(repo_with_failing_hook):
|
||||||
# Add bash hook on there again
|
with modify_config() as config:
|
||||||
with io.open(
|
config[0]['hooks'][0]['args'] = ['☃']
|
||||||
'.pre-commit-config.yaml', 'a+', encoding='UTF-8',
|
|
||||||
) as config_file:
|
|
||||||
config_file.write(' args: ["☃"]\n')
|
|
||||||
cmd_output('git', 'add', '.pre-commit-config.yaml')
|
|
||||||
stage_a_file()
|
stage_a_file()
|
||||||
|
|
||||||
install(Runner(repo_with_failing_hook))
|
install(Runner(repo_with_failing_hook))
|
||||||
|
|
@ -382,8 +379,8 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory):
|
||||||
git_path = make_consuming_repo(tempdir_factory, 'python_hooks_repo')
|
git_path = make_consuming_repo(tempdir_factory, 'python_hooks_repo')
|
||||||
with cwd(git_path):
|
with cwd(git_path):
|
||||||
# Override files so we run against them
|
# Override files so we run against them
|
||||||
with io.open('.pre-commit-config.yaml', 'a+') as config_file:
|
with modify_config() as config:
|
||||||
config_file.write(' files: ""\n')
|
config[0]['hooks'][0]['files'] = ''
|
||||||
|
|
||||||
# Write a crap ton of files
|
# Write a crap ton of files
|
||||||
for i in range(400):
|
for i in range(400):
|
||||||
|
|
@ -463,9 +460,7 @@ def test_local_hook_for_stages(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_local_hook_passes(
|
def test_local_hook_passes(repo_with_passing_hook, mock_out_store_directory):
|
||||||
repo_with_passing_hook, mock_out_store_directory,
|
|
||||||
):
|
|
||||||
config = OrderedDict((
|
config = OrderedDict((
|
||||||
('repo', 'local'),
|
('repo', 'local'),
|
||||||
('hooks', (OrderedDict((
|
('hooks', (OrderedDict((
|
||||||
|
|
@ -497,9 +492,7 @@ def test_local_hook_passes(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_local_hook_fails(
|
def test_local_hook_fails(repo_with_passing_hook, mock_out_store_directory):
|
||||||
repo_with_passing_hook, mock_out_store_directory,
|
|
||||||
):
|
|
||||||
config = OrderedDict((
|
config = OrderedDict((
|
||||||
('repo', 'local'),
|
('repo', 'local'),
|
||||||
('hooks', [OrderedDict((
|
('hooks', [OrderedDict((
|
||||||
|
|
@ -525,60 +518,47 @@ def test_local_hook_fails(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_allow_unstaged_config_option(
|
@pytest.yield_fixture
|
||||||
repo_with_passing_hook, mock_out_store_directory,
|
def modified_config_repo(repo_with_passing_hook):
|
||||||
):
|
with modify_config(repo_with_passing_hook, commit=False) as config:
|
||||||
with cwd(repo_with_passing_hook):
|
# Some minor modification
|
||||||
with io.open('.pre-commit-config.yaml', 'a+') as config_file:
|
config[0]['hooks'][0]['files'] = ''
|
||||||
# writing a newline should be relatively harmless to get a change
|
yield repo_with_passing_hook
|
||||||
config_file.write('\n')
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_allow_unstaged_config_option(
|
||||||
|
modified_config_repo, mock_out_store_directory,
|
||||||
|
):
|
||||||
args = _get_opts(allow_unstaged_config=True)
|
args = _get_opts(allow_unstaged_config=True)
|
||||||
ret, printed = _do_run(repo_with_passing_hook, args)
|
ret, printed = _do_run(modified_config_repo, args)
|
||||||
assert b'You have an unstaged config file' in printed
|
expected = (
|
||||||
assert b'have specified the --allow-unstaged-config option.' in printed
|
b'You have an unstaged config file and have specified the '
|
||||||
|
b'--allow-unstaged-config option.'
|
||||||
|
)
|
||||||
|
assert expected in printed
|
||||||
assert ret == 0
|
assert ret == 0
|
||||||
|
|
||||||
|
|
||||||
def modify_config(path):
|
|
||||||
with cwd(path):
|
|
||||||
with io.open('.pre-commit-config.yaml', 'a+') as config_file:
|
|
||||||
# writing a newline should be relatively harmless to get a change
|
|
||||||
config_file.write('\n')
|
|
||||||
|
|
||||||
|
|
||||||
def test_no_allow_unstaged_config_option(
|
def test_no_allow_unstaged_config_option(
|
||||||
repo_with_passing_hook, mock_out_store_directory,
|
modified_config_repo, mock_out_store_directory,
|
||||||
):
|
):
|
||||||
modify_config(repo_with_passing_hook)
|
|
||||||
args = _get_opts(allow_unstaged_config=False)
|
args = _get_opts(allow_unstaged_config=False)
|
||||||
ret, printed = _do_run(repo_with_passing_hook, args)
|
ret, printed = _do_run(modified_config_repo, args)
|
||||||
assert b'Your .pre-commit-config.yaml is unstaged.' in printed
|
assert b'Your .pre-commit-config.yaml is unstaged.' in printed
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
|
|
||||||
|
|
||||||
def test_no_stash_suppresses_allow_unstaged_config_option(
|
@pytest.mark.parametrize(
|
||||||
repo_with_passing_hook, mock_out_store_directory,
|
'opts',
|
||||||
|
(
|
||||||
|
{'allow_unstaged_config': False, 'no_stash': True},
|
||||||
|
{'all_files': True},
|
||||||
|
{'files': [C.CONFIG_FILE]},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_unstaged_message_suppressed(
|
||||||
|
modified_config_repo, mock_out_store_directory, opts,
|
||||||
):
|
):
|
||||||
modify_config(repo_with_passing_hook)
|
args = _get_opts(**opts)
|
||||||
args = _get_opts(allow_unstaged_config=False, no_stash=True)
|
ret, printed = _do_run(modified_config_repo, args)
|
||||||
ret, printed = _do_run(repo_with_passing_hook, args)
|
|
||||||
assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
|
|
||||||
|
|
||||||
|
|
||||||
def test_all_files_suppresses_allow_unstaged_config_option(
|
|
||||||
repo_with_passing_hook, mock_out_store_directory,
|
|
||||||
):
|
|
||||||
modify_config(repo_with_passing_hook)
|
|
||||||
args = _get_opts(all_files=True)
|
|
||||||
ret, printed = _do_run(repo_with_passing_hook, args)
|
|
||||||
assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
|
|
||||||
|
|
||||||
|
|
||||||
def test_files_suppresses_allow_unstaged_config_option(
|
|
||||||
repo_with_passing_hook, mock_out_store_directory,
|
|
||||||
):
|
|
||||||
modify_config(repo_with_passing_hook)
|
|
||||||
args = _get_opts(files=['.pre-commit-config.yaml'])
|
|
||||||
ret, printed = _do_run(repo_with_passing_hook, args)
|
|
||||||
assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
|
assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue