|
1 | 1 | from __future__ import unicode_literals |
2 | 2 |
|
| 3 | +import logging |
| 4 | + |
3 | 5 | import cfgv |
4 | 6 | import pytest |
5 | 7 |
|
@@ -116,6 +118,48 @@ def test_validate_config_old_list_format_ok(tmpdir): |
116 | 118 | assert not validate_config_main((f.strpath,)) |
117 | 119 |
|
118 | 120 |
|
| 121 | +def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog): |
| 122 | + f = tmpdir.join('cfg.yaml') |
| 123 | + f.write( |
| 124 | + '- repo: https://gitlab.com/pycqa/flake8\n' |
| 125 | + ' rev: 3.7.7\n' |
| 126 | + ' hooks:\n' |
| 127 | + ' - id: flake8\n' |
| 128 | + ' args: [--some-args]\n', |
| 129 | + ) |
| 130 | + ret_val = validate_config_main((f.strpath,)) |
| 131 | + assert not ret_val |
| 132 | + assert caplog.record_tuples == [ |
| 133 | + ( |
| 134 | + 'pre_commit', |
| 135 | + logging.WARNING, |
| 136 | + 'Unexpected config key(s): args', |
| 137 | + ), |
| 138 | + ] |
| 139 | + |
| 140 | + |
| 141 | +def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog): |
| 142 | + f = tmpdir.join('cfg.yaml') |
| 143 | + f.write( |
| 144 | + 'repos:\n' |
| 145 | + '- repo: https://gitlab.com/pycqa/flake8\n' |
| 146 | + ' rev: 3.7.7\n' |
| 147 | + ' hooks:\n' |
| 148 | + ' - id: flake8\n' |
| 149 | + 'foo:\n' |
| 150 | + ' id: 1.0.0\n', |
| 151 | + ) |
| 152 | + ret_val = validate_config_main((f.strpath,)) |
| 153 | + assert not ret_val |
| 154 | + assert caplog.record_tuples == [ |
| 155 | + ( |
| 156 | + 'pre_commit', |
| 157 | + logging.WARNING, |
| 158 | + 'Unexpected config key(s): foo', |
| 159 | + ), |
| 160 | + ] |
| 161 | + |
| 162 | + |
119 | 163 | @pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main)) |
120 | 164 | def test_mains_not_ok(tmpdir, fn): |
121 | 165 | not_yaml = tmpdir.join('f.notyaml') |
@@ -261,3 +305,12 @@ def test_minimum_pre_commit_version_failing(): |
261 | 305 | def test_minimum_pre_commit_version_passing(): |
262 | 306 | cfg = {'repos': [], 'minimum_pre_commit_version': '0'} |
263 | 307 | cfgv.validate(cfg, CONFIG_SCHEMA) |
| 308 | + |
| 309 | + |
| 310 | +@pytest.mark.parametrize('schema', (CONFIG_SCHEMA, CONFIG_REPO_DICT)) |
| 311 | +def test_warn_additional(schema): |
| 312 | + allowed_keys = {item.key for item in schema.items if hasattr(item, 'key')} |
| 313 | + warn_additional, = [ |
| 314 | + x for x in schema.items if isinstance(x, cfgv.WarnAdditionalKeys) |
| 315 | + ] |
| 316 | + assert allowed_keys == set(warn_additional.keys) |
0 commit comments