|
15 | 15 | from pre_commit.clientlib import MANIFEST_SCHEMA |
16 | 16 | from pre_commit.clientlib import META_HOOK_DICT |
17 | 17 | from pre_commit.clientlib import MigrateShaToRev |
| 18 | +from pre_commit.clientlib import OptionalSensibleRegexAtHook |
| 19 | +from pre_commit.clientlib import OptionalSensibleRegexAtTop |
18 | 20 | from pre_commit.clientlib import validate_config_main |
19 | 21 | from pre_commit.clientlib import validate_manifest_main |
20 | 22 | from testing.fixtures import sample_local_config |
@@ -261,6 +263,27 @@ def test_warn_mutable_rev_conditional(): |
261 | 263 | cfgv.validate(config_obj, CONFIG_REPO_DICT) |
262 | 264 |
|
263 | 265 |
|
| 266 | +@pytest.mark.parametrize( |
| 267 | + 'validator_cls', |
| 268 | + ( |
| 269 | + OptionalSensibleRegexAtHook, |
| 270 | + OptionalSensibleRegexAtTop, |
| 271 | + ), |
| 272 | +) |
| 273 | +def test_sensible_regex_validators_dont_pass_none(validator_cls): |
| 274 | + key = 'files' |
| 275 | + with pytest.raises(cfgv.ValidationError) as excinfo: |
| 276 | + validator = validator_cls(key, cfgv.check_string) |
| 277 | + validator.check({key: None}) |
| 278 | + |
| 279 | + assert str(excinfo.value) == ( |
| 280 | + '\n' |
| 281 | + f'==> At key: {key}' |
| 282 | + '\n' |
| 283 | + '=====> Expected string got NoneType' |
| 284 | + ) |
| 285 | + |
| 286 | + |
264 | 287 | @pytest.mark.parametrize( |
265 | 288 | ('regex', 'warning'), |
266 | 289 | ( |
@@ -296,6 +319,22 @@ def test_validate_optional_sensible_regex_at_hook(caplog, regex, warning): |
296 | 319 | assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)] |
297 | 320 |
|
298 | 321 |
|
| 322 | +def test_validate_optional_sensible_regex_at_local_hook(caplog): |
| 323 | + config_obj = sample_local_config() |
| 324 | + config_obj['hooks'][0]['files'] = r'dir/*.py' |
| 325 | + |
| 326 | + cfgv.validate(config_obj, CONFIG_REPO_DICT) |
| 327 | + |
| 328 | + assert caplog.record_tuples == [ |
| 329 | + ( |
| 330 | + 'pre_commit', |
| 331 | + logging.WARNING, |
| 332 | + "The 'files' field in hook 'do_not_commit' is a regex, not a glob " |
| 333 | + "-- matching '/*' probably isn't what you want here", |
| 334 | + ), |
| 335 | + ] |
| 336 | + |
| 337 | + |
299 | 338 | @pytest.mark.parametrize( |
300 | 339 | ('regex', 'warning'), |
301 | 340 | ( |
|
0 commit comments