Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate pre-commit-validate-{config,manifest} #2362

Merged
merged 2 commits into from Apr 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
@@ -1,6 +1,6 @@
- id: validate_manifest
name: validate pre-commit manifest
description: This validator validates a pre-commit hooks manifest file
entry: pre-commit-validate-manifest
entry: pre-commit validate-manifest
language: python
files: ^(\.pre-commit-hooks\.yaml|hooks\.yaml)$
files: ^\.pre-commit-hooks\.yaml$
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Expand Up @@ -10,7 +10,7 @@ resources:
type: github
endpoint: github
name: asottile/azure-pipeline-templates
ref: refs/tags/v2.1.0
ref: refs/tags/v2.4.1

jobs:
- template: job--python-tox.yml@asottile
Expand Down
30 changes: 14 additions & 16 deletions pre_commit/clientlib.py
Expand Up @@ -14,6 +14,8 @@

import pre_commit.constants as C
from pre_commit.color import add_color_option
from pre_commit.commands.validate_config import validate_config
from pre_commit.commands.validate_manifest import validate_manifest
from pre_commit.errors import FatalError
from pre_commit.languages.all import all_languages
from pre_commit.logging_handler import logging_handler
Expand Down Expand Up @@ -100,14 +102,12 @@ def validate_manifest_main(argv: Sequence[str] | None = None) -> int:
args = parser.parse_args(argv)

with logging_handler(args.color):
ret = 0
for filename in args.filenames:
try:
load_manifest(filename)
except InvalidManifestError as e:
print(e)
ret = 1
return ret
logger.warning(
'pre-commit-validate-manifest is deprecated -- '
'use `pre-commit validate-manifest` instead.',
)

return validate_manifest(args.filenames)


LOCAL = 'local'
Expand Down Expand Up @@ -409,11 +409,9 @@ def validate_config_main(argv: Sequence[str] | None = None) -> int:
args = parser.parse_args(argv)

with logging_handler(args.color):
ret = 0
for filename in args.filenames:
try:
load_config(filename)
except InvalidConfigError as e:
print(e)
ret = 1
return ret
logger.warning(
'pre-commit-validate-config is deprecated -- '
'use `pre-commit validate-config` instead.',
)

return validate_config(args.filenames)
16 changes: 16 additions & 0 deletions pre_commit/commands/validate_config.py
@@ -0,0 +1,16 @@
from __future__ import annotations

from pre_commit import clientlib


def validate_config(filenames: list[str]) -> int:
ret = 0

for filename in filenames:
try:
clientlib.load_config(filename)
except clientlib.InvalidConfigError as e:
print(e)
ret = 1

return ret
16 changes: 16 additions & 0 deletions pre_commit/commands/validate_manifest.py
@@ -0,0 +1,16 @@
from __future__ import annotations

from pre_commit import clientlib


def validate_manifest(filenames: list[str]) -> int:
ret = 0

for filename in filenames:
try:
clientlib.load_manifest(filename)
except clientlib.InvalidManifestError as e:
print(e)
ret = 1

return ret
26 changes: 24 additions & 2 deletions pre_commit/main.py
Expand Up @@ -21,6 +21,8 @@
from pre_commit.commands.run import run
from pre_commit.commands.sample_config import sample_config
from pre_commit.commands.try_repo import try_repo
from pre_commit.commands.validate_config import validate_config
from pre_commit.commands.validate_manifest import validate_manifest
from pre_commit.error_handler import error_handler
from pre_commit.logging_handler import logging_handler
from pre_commit.store import Store
Expand All @@ -34,8 +36,10 @@
# pyvenv
os.environ.pop('__PYVENV_LAUNCHER__', None)


COMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config'}
COMMANDS_NO_GIT = {
'clean', 'gc', 'init-templatedir', 'sample-config',
'validate-config', 'validate-manifest',
}


def _add_config_option(parser: argparse.ArgumentParser) -> None:
Expand Down Expand Up @@ -304,6 +308,20 @@ def main(argv: Sequence[str] | None = None) -> int:
_add_config_option(uninstall_parser)
_add_hook_type_option(uninstall_parser)

validate_config_parser = subparsers.add_parser(
'validate-config', help='Validate .pre-commit-config.yaml files',
)
add_color_option(validate_config_parser)
_add_config_option(validate_config_parser)
validate_config_parser.add_argument('filenames', nargs='*')

validate_manifest_parser = subparsers.add_parser(
'validate-manifest', help='Validate .pre-commit-hooks.yaml files',
)
add_color_option(validate_manifest_parser)
_add_config_option(validate_manifest_parser)
validate_manifest_parser.add_argument('filenames', nargs='*')

help = subparsers.add_parser(
'help', help='Show help for a specific command.',
)
Expand Down Expand Up @@ -378,6 +396,10 @@ def main(argv: Sequence[str] | None = None) -> int:
config_file=args.config,
hook_types=args.hook_types,
)
elif args.command == 'validate-config':
return validate_config(args.filenames)
elif args.command == 'validate-manifest':
return validate_manifest(args.filenames)
else:
raise NotImplementedError(
f'Command {args.command} not implemented.',
Expand Down
6 changes: 3 additions & 3 deletions testing/get-swift.sh
Expand Up @@ -3,9 +3,9 @@
set -euo pipefail

. /etc/lsb-release
if [ "$DISTRIB_CODENAME" = "bionic" ]; then
SWIFT_URL='https://swift.org/builds/swift-5.1.3-release/ubuntu1804/swift-5.1.3-RELEASE/swift-5.1.3-RELEASE-ubuntu18.04.tar.gz'
SWIFT_HASH='ac82ccd773fe3d586fc340814e31e120da1ff695c6a712f6634e9cc720769610'
if [ "$DISTRIB_CODENAME" = "focal" ]; then
SWIFT_URL='https://download.swift.org/swift-5.6.1-release/ubuntu2004/swift-5.6.1-RELEASE/swift-5.6.1-RELEASE-ubuntu20.04.tar.gz'
SWIFT_HASH='2b4f22d4a8b59fe8e050f0b7f020f8d8f12553cbda56709b2340a4a3bb90cfea'
else
echo "unknown dist: ${DISTRIB_CODENAME}" 1>&2
exit 1
Expand Down
16 changes: 14 additions & 2 deletions tests/clientlib_test.py
Expand Up @@ -122,8 +122,8 @@ def test_validate_config_old_list_format_ok(tmpdir, cap_out):
f = tmpdir.join('cfg.yaml')
f.write('- {repo: meta, hooks: [{id: identity}]}')
assert not validate_config_main((f.strpath,))
start = '[WARNING] normalizing pre-commit configuration to a top-level map'
assert cap_out.get().startswith(start)
msg = '[WARNING] normalizing pre-commit configuration to a top-level map'
assert msg in cap_out.get()


def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog):
Expand All @@ -139,6 +139,12 @@ def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog):
ret_val = validate_config_main((f.strpath,))
assert not ret_val
assert caplog.record_tuples == [
(
'pre_commit',
logging.WARNING,
'pre-commit-validate-config is deprecated -- '
'use `pre-commit validate-config` instead.',
),
(
'pre_commit',
logging.WARNING,
Expand All @@ -162,6 +168,12 @@ def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
ret_val = validate_config_main((f.strpath,))
assert not ret_val
assert caplog.record_tuples == [
(
'pre_commit',
logging.WARNING,
'pre-commit-validate-config is deprecated -- '
'use `pre-commit validate-config` instead.',
),
(
'pre_commit',
logging.WARNING,
Expand Down
1 change: 1 addition & 0 deletions tests/main_test.py
Expand Up @@ -79,6 +79,7 @@ def test_adjust_args_try_repo_repo_relative(in_git_dir):
FNS = (
'autoupdate', 'clean', 'gc', 'hook_impl', 'install', 'install_hooks',
'migrate_config', 'run', 'sample_config', 'uninstall',
'validate_config', 'validate_manifest',
)
CMDS = tuple(fn.replace('_', '-') for fn in FNS)

Expand Down
2 changes: 2 additions & 0 deletions tests/repository_test.py
Expand Up @@ -173,6 +173,7 @@ def test_python_venv(tempdir_factory, store):
)


@xfailif_windows # pragma: win32 no cover # no python 2 in GHA
def test_switch_language_versions_doesnt_clobber(tempdir_factory, store):
# We're using the python3 repo because it prints the python version
path = make_repo(tempdir_factory, 'python3_hooks_repo')
Expand Down Expand Up @@ -892,6 +893,7 @@ def test_local_python_repo(store, local_python_config):
assert _norm_out(out) == b"3\n['filename']\nHello World\n"


@xfailif_windows # pragma: win32 no cover # no python2 in GHA
def test_local_python_repo_python2(store, local_python_config):
local_python_config['hooks'][0]['language_version'] = 'python2'
hook = _get_hook(local_python_config, store, 'python3-hook')
Expand Down