Skip to content

Commit 727247e

Browse files
author
Jacob Scott
committed
Add tests for alternate config
1 parent f205e6d commit 727247e

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

testing/fixtures.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,24 @@ def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
9494
return config
9595

9696

97-
def write_config(directory, config):
97+
def read_config(directory, config_file=C.CONFIG_FILE):
98+
config_path = os.path.join(directory, config_file)
99+
config = ordered_load(io.open(config_path).read())
100+
return config
101+
102+
103+
def write_config(directory, config, config_file=C.CONFIG_FILE):
98104
if type(config) is not list:
99105
assert type(config) is OrderedDict
100106
config = [config]
101-
with io.open(os.path.join(directory, C.CONFIG_FILE), 'w') as config_file:
102-
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
107+
with io.open(os.path.join(directory, config_file), 'w') as outfile:
108+
outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
103109

104110

105-
def add_config_to_repo(git_path, config):
106-
write_config(git_path, config)
111+
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):
112+
write_config(git_path, config, config_file=config_file)
107113
with cwd(git_path):
108-
cmd_output('git', 'add', C.CONFIG_FILE)
114+
cmd_output('git', 'add', config_file)
109115
cmd_output('git', 'commit', '-m', 'Add hooks config')
110116
return git_path
111117

tests/commands/run_test.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from testing.fixtures import add_config_to_repo
2525
from testing.fixtures import make_consuming_repo
2626
from testing.fixtures import modify_config
27+
from testing.fixtures import read_config
2728
from testing.util import cmd_output_mocked_pre_commit_home
2829

2930

@@ -74,19 +75,20 @@ def _get_opts(
7475
)
7576

7677

77-
def _do_run(cap_out, repo, args, environ={}):
78-
runner = Runner(repo, C.CONFIG_FILE)
78+
def _do_run(cap_out, repo, args, environ={}, config_file=C.CONFIG_FILE):
79+
runner = Runner(repo, config_file)
7980
with cwd(runner.git_root): # replicates Runner.create behaviour
8081
ret = run(runner, args, environ=environ)
8182
printed = cap_out.get_bytes()
8283
return ret, printed
8384

8485

85-
def _test_run(cap_out, repo, opts, expected_outputs, expected_ret, stage):
86+
def _test_run(cap_out, repo, opts, expected_outputs, expected_ret, stage,
87+
config_file=C.CONFIG_FILE):
8688
if stage:
8789
stage_a_file()
8890
args = _get_opts(**opts)
89-
ret, printed = _do_run(cap_out, repo, args)
91+
ret, printed = _do_run(cap_out, repo, args, config_file=config_file)
9092

9193
assert ret == expected_ret, (ret, expected_ret, printed)
9294
for expected_output_part in expected_outputs:
@@ -205,6 +207,26 @@ def test_always_run(
205207
)
206208

207209

210+
def test_always_run_alt_config(
211+
cap_out, repo_with_passing_hook, mock_out_store_directory,
212+
):
213+
repo_root = '.'
214+
config = read_config(repo_root)
215+
config[0]['hooks'][0]['always_run'] = True
216+
alt_config_file = 'alternate_config.yaml'
217+
add_config_to_repo(repo_root, config, config_file=alt_config_file)
218+
219+
_test_run(
220+
cap_out,
221+
repo_with_passing_hook,
222+
{},
223+
(b'Bash hook', b'Passed'),
224+
0,
225+
stage=False,
226+
config_file=alt_config_file
227+
)
228+
229+
208230
@pytest.mark.parametrize(
209231
('origin', 'source', 'expect_failure'),
210232
(

tests/runner_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,38 @@ def test_local_hooks(tempdir_factory, mock_out_store_directory):
7979
assert len(runner.repositories[0].hooks) == 2
8080

8181

82+
def test_local_hooks_alt_config(tempdir_factory, mock_out_store_directory):
83+
config = OrderedDict((
84+
('repo', 'local'),
85+
('hooks', (OrderedDict((
86+
('id', 'arg-per-line'),
87+
('name', 'Args per line hook'),
88+
('entry', 'bin/hook.sh'),
89+
('language', 'script'),
90+
('files', ''),
91+
('args', ['hello', 'world']),
92+
)), OrderedDict((
93+
('id', 'ugly-format-json'),
94+
('name', 'Ugly format json'),
95+
('entry', 'ugly-format-json'),
96+
('language', 'python'),
97+
('files', ''),
98+
)), OrderedDict((
99+
('id', 'do_not_commit'),
100+
('name', 'Block if "DO NOT COMMIT" is found'),
101+
('entry', 'DO NOT COMMIT'),
102+
('language', 'pcre'),
103+
('files', '^(.*)$'),
104+
))))
105+
))
106+
git_path = git_dir(tempdir_factory)
107+
alt_config_file = 'alternate_config.yaml'
108+
add_config_to_repo(git_path, config, config_file=alt_config_file)
109+
runner = Runner(git_path, alt_config_file)
110+
assert len(runner.repositories) == 1
111+
assert len(runner.repositories[0].hooks) == 3
112+
113+
82114
def test_pre_commit_path(in_tmpdir):
83115
path = os.path.join('foo', 'bar')
84116
cmd_output('git', 'init', path)

0 commit comments

Comments
 (0)