X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions pre_commit/clientlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ def apply_default(self, dct: dict[str, Any]) -> None:
super().apply_default(dct)


class DeprecatedStagesWarning(NamedTuple):
key: str

def check(self, dct: dict[str, Any]) -> None:
if self.key not in dct:
return

val = dct[self.key]
cfgv.check_array(cfgv.check_any)(val)

legacy_stages = [stage for stage in val if stage in _STAGES]
if legacy_stages:
logger.warning(
f'hook id `{dct["id"]}` uses deprecated stage names '
f'({", ".join(legacy_stages)}) which will be removed in a '
f'future version. '
f'run: `pre-commit migrate-config` to automatically fix this.',
)

def apply_default(self, dct: dict[str, Any]) -> None:
pass

def remove_default(self, dct: dict[str, Any]) -> None:
raise NotImplementedError


MANIFEST_HOOK_DICT = cfgv.Map(
'Hook', 'id',

Expand Down Expand Up @@ -267,6 +293,12 @@ def check(self, dct: dict[str, Any]) -> None:
raise cfgv.ValidationError(f'{self.key!r} cannot be overridden')


_COMMON_HOOK_WARNINGS = (
OptionalSensibleRegexAtHook('files', cfgv.check_string),
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
DeprecatedStagesWarning('stages'),
)

META_HOOK_DICT = cfgv.Map(
'Hook', 'id',
cfgv.Required('id', cfgv.check_string),
Expand All @@ -289,8 +321,7 @@ def check(self, dct: dict[str, Any]) -> None:
item
for item in MANIFEST_HOOK_DICT.items
),
OptionalSensibleRegexAtHook('files', cfgv.check_string),
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
*_COMMON_HOOK_WARNINGS,
)
CONFIG_HOOK_DICT = cfgv.Map(
'Hook', 'id',
Expand All @@ -308,16 +339,13 @@ def check(self, dct: dict[str, Any]) -> None:
if item.key != 'stages'
),
StagesMigrationNoDefault('stages', []),
OptionalSensibleRegexAtHook('files', cfgv.check_string),
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
*_COMMON_HOOK_WARNINGS,
)
LOCAL_HOOK_DICT = cfgv.Map(
'Hook', 'id',

*MANIFEST_HOOK_DICT.items,

OptionalSensibleRegexAtHook('files', cfgv.check_string),
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
*_COMMON_HOOK_WARNINGS,
)
CONFIG_REPO_DICT = cfgv.Map(
'Repository', 'repo',
Expand Down
26 changes: 26 additions & 0 deletions tests/clientlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]


def test_warning_for_deprecated_stages(caplog):
config_obj = sample_local_config()
config_obj['hooks'][0]['stages'] = ['commit', 'push']

cfgv.validate(config_obj, CONFIG_REPO_DICT)

assert caplog.record_tuples == [
(
'pre_commit',
logging.WARNING,
'hook id `do_not_commit` uses deprecated stage names '
'(commit, push) which will be removed in a future version. '
'run: `pre-commit migrate-config` to automatically fix this.',
),
]


def test_no_warning_for_non_deprecated_stages(caplog):
config_obj = sample_local_config()
config_obj['hooks'][0]['stages'] = ['pre-commit', 'pre-push']

cfgv.validate(config_obj, CONFIG_REPO_DICT)

assert caplog.record_tuples == []


@pytest.mark.parametrize(
'manifest_obj',
(
Expand Down
X Tutup