mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-14 17:41:45 +04:00
Add tests for alternate config
This commit is contained in:
parent
f205e6d170
commit
727247e6ed
3 changed files with 70 additions and 10 deletions
|
|
@ -94,18 +94,24 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def write_config(directory, config):
|
def read_config(directory, config_file=C.CONFIG_FILE):
|
||||||
|
config_path = os.path.join(directory, config_file)
|
||||||
|
config = ordered_load(io.open(config_path).read())
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def write_config(directory, config, config_file=C.CONFIG_FILE):
|
||||||
if type(config) is not list:
|
if type(config) is not list:
|
||||||
assert type(config) is OrderedDict
|
assert type(config) is OrderedDict
|
||||||
config = [config]
|
config = [config]
|
||||||
with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file:
|
with io.open(os.path.join(directory, config_file), 'w') as outfile:
|
||||||
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
|
outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
|
||||||
|
|
||||||
|
|
||||||
def add_config_to_repo(git_path, config):
|
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):
|
||||||
write_config(git_path, config)
|
write_config(git_path, config, config_file=config_file)
|
||||||
with cwd(git_path):
|
with cwd(git_path):
|
||||||
cmd_output('git', 'add', C.CONFIG_FILE)
|
cmd_output('git', 'add', config_file)
|
||||||
cmd_output('git', 'commit', '-m', 'Add hooks config')
|
cmd_output('git', 'commit', '-m', 'Add hooks config')
|
||||||
return git_path
|
return git_path
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ 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
|
from testing.fixtures import modify_config
|
||||||
|
from testing.fixtures import read_config
|
||||||
from testing.util import cmd_output_mocked_pre_commit_home
|
from testing.util import cmd_output_mocked_pre_commit_home
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -74,19 +75,20 @@ def _get_opts(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _do_run(cap_out, repo, args, environ={}):
|
def _do_run(cap_out, repo, args, environ={}, config_file=C.CONFIG_FILE):
|
||||||
runner = Runner(repo, C.CONFIG_FILE)
|
runner = Runner(repo, config_file)
|
||||||
with cwd(runner.git_root): # replicates Runner.create behaviour
|
with cwd(runner.git_root): # replicates Runner.create behaviour
|
||||||
ret = run(runner, args, environ=environ)
|
ret = run(runner, args, environ=environ)
|
||||||
printed = cap_out.get_bytes()
|
printed = cap_out.get_bytes()
|
||||||
return ret, printed
|
return ret, printed
|
||||||
|
|
||||||
|
|
||||||
def _test_run(cap_out, repo, opts, expected_outputs, expected_ret, stage):
|
def _test_run(cap_out, repo, opts, expected_outputs, expected_ret, stage,
|
||||||
|
config_file=C.CONFIG_FILE):
|
||||||
if stage:
|
if stage:
|
||||||
stage_a_file()
|
stage_a_file()
|
||||||
args = _get_opts(**opts)
|
args = _get_opts(**opts)
|
||||||
ret, printed = _do_run(cap_out, repo, args)
|
ret, printed = _do_run(cap_out, repo, args, config_file=config_file)
|
||||||
|
|
||||||
assert ret == expected_ret, (ret, expected_ret, printed)
|
assert ret == expected_ret, (ret, expected_ret, printed)
|
||||||
for expected_output_part in expected_outputs:
|
for expected_output_part in expected_outputs:
|
||||||
|
|
@ -205,6 +207,26 @@ def test_always_run(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_always_run_alt_config(
|
||||||
|
cap_out, repo_with_passing_hook, mock_out_store_directory,
|
||||||
|
):
|
||||||
|
repo_root = '.'
|
||||||
|
config = read_config(repo_root)
|
||||||
|
config[0]['hooks'][0]['always_run'] = True
|
||||||
|
alt_config_file = 'alternate_config.yaml'
|
||||||
|
add_config_to_repo(repo_root, config, config_file=alt_config_file)
|
||||||
|
|
||||||
|
_test_run(
|
||||||
|
cap_out,
|
||||||
|
repo_with_passing_hook,
|
||||||
|
{},
|
||||||
|
(b'Bash hook', b'Passed'),
|
||||||
|
0,
|
||||||
|
stage=False,
|
||||||
|
config_file=alt_config_file
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('origin', 'source', 'expect_failure'),
|
('origin', 'source', 'expect_failure'),
|
||||||
(
|
(
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,38 @@ def test_local_hooks(tempdir_factory, mock_out_store_directory):
|
||||||
assert len(runner.repositories[0].hooks) == 2
|
assert len(runner.repositories[0].hooks) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_local_hooks_alt_config(tempdir_factory, mock_out_store_directory):
|
||||||
|
config = OrderedDict((
|
||||||
|
('repo', 'local'),
|
||||||
|
('hooks', (OrderedDict((
|
||||||
|
('id', 'arg-per-line'),
|
||||||
|
('name', 'Args per line hook'),
|
||||||
|
('entry', 'bin/hook.sh'),
|
||||||
|
('language', 'script'),
|
||||||
|
('files', ''),
|
||||||
|
('args', ['hello', 'world']),
|
||||||
|
)), OrderedDict((
|
||||||
|
('id', 'ugly-format-json'),
|
||||||
|
('name', 'Ugly format json'),
|
||||||
|
('entry', 'ugly-format-json'),
|
||||||
|
('language', 'python'),
|
||||||
|
('files', ''),
|
||||||
|
)), OrderedDict((
|
||||||
|
('id', 'do_not_commit'),
|
||||||
|
('name', 'Block if "DO NOT COMMIT" is found'),
|
||||||
|
('entry', 'DO NOT COMMIT'),
|
||||||
|
('language', 'pcre'),
|
||||||
|
('files', '^(.*)$'),
|
||||||
|
))))
|
||||||
|
))
|
||||||
|
git_path = git_dir(tempdir_factory)
|
||||||
|
alt_config_file = 'alternate_config.yaml'
|
||||||
|
add_config_to_repo(git_path, config, config_file=alt_config_file)
|
||||||
|
runner = Runner(git_path, alt_config_file)
|
||||||
|
assert len(runner.repositories) == 1
|
||||||
|
assert len(runner.repositories[0].hooks) == 3
|
||||||
|
|
||||||
|
|
||||||
def test_pre_commit_path(in_tmpdir):
|
def test_pre_commit_path(in_tmpdir):
|
||||||
path = os.path.join('foo', 'bar')
|
path = os.path.join('foo', 'bar')
|
||||||
cmd_output('git', 'init', path)
|
cmd_output('git', 'init', path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue