diff --git a/testing/fixtures.py b/testing/fixtures.py index 36c7400c..fdf651e8 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -94,18 +94,24 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True): 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: assert type(config) is OrderedDict config = [config] - with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file: - config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) + with io.open(os.path.join(directory, config_file), 'w') as outfile: + outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) -def add_config_to_repo(git_path, config): - write_config(git_path, config) +def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE): + write_config(git_path, config, config_file=config_file) 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') return git_path diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index a7a651ed..86d0ecd4 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -24,6 +24,7 @@ from testing.auto_namedtuple import auto_namedtuple from testing.fixtures import add_config_to_repo from testing.fixtures import make_consuming_repo from testing.fixtures import modify_config +from testing.fixtures import read_config 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={}): - runner = Runner(repo, C.CONFIG_FILE) +def _do_run(cap_out, repo, args, environ={}, config_file=C.CONFIG_FILE): + runner = Runner(repo, config_file) with cwd(runner.git_root): # replicates Runner.create behaviour ret = run(runner, args, environ=environ) printed = cap_out.get_bytes() 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: stage_a_file() 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) 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( ('origin', 'source', 'expect_failure'), ( diff --git a/tests/runner_test.py b/tests/runner_test.py index f93cb360..9039e573 100644 --- a/tests/runner_test.py +++ b/tests/runner_test.py @@ -79,6 +79,38 @@ def test_local_hooks(tempdir_factory, mock_out_store_directory): 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): path = os.path.join('foo', 'bar') cmd_output('git', 'init', path)