From c8f8c5cc059f0024a8312440d12a123533b09894 Mon Sep 17 00:00:00 2001 From: Cristian Le Date: Wed, 19 Apr 2023 16:09:24 +0200 Subject: [PATCH] Add pre-commit wrapper Signed-off-by: Cristian Le --- .pre-commit-hooks.yaml | 8 ++++---- setup.py | 3 +++ tmt.spec | 2 ++ tmt/cli.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 70e8e547bc..ecb1a53ee4 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -9,7 +9,7 @@ - id: tmt-lint name: tmt lint - entry: bash -c "git ls-files --error-unmatch $(python3 -c 'import tmt; print(tmt.Tree(logger=tmt.Logger.create(), path=\".\").root)')/.fmf/version && tmt lint --source $@" PAD + entry: tmt-pre-commit files: '.*\.fmf$' verbose: true pass_filenames: true @@ -18,7 +18,7 @@ - id: tmt-tests-lint name: tmt tests lint - entry: bash -c "git ls-files --error-unmatch $(python3 -c 'import tmt; print(tmt.Tree(logger=tmt.Logger.create(), path=\".\").root)')/.fmf/version && tmt tests lint --source $@" PAD + entry: tmt-pre-commit --lint-type tests files: '.*\.fmf$' verbose: true pass_filenames: true @@ -27,7 +27,7 @@ - id: tmt-plans-lint name: tmt plans lint - entry: bash -c "git ls-files --error-unmatch $(python3 -c 'import tmt; print(tmt.Tree(logger=tmt.Logger.create(), path=\".\").root)')/.fmf/version && tmt plans lint --source $@" PAD + entry: tmt-pre-commit --lint-type plans files: '.*\.fmf$' verbose: true pass_filenames: true @@ -36,7 +36,7 @@ - id: tmt-stories-lint name: tmt stories lint - entry: bash -c "git ls-files --error-unmatch $(python3 -c 'import tmt; print(tmt.Tree(logger=tmt.Logger.create(), path=\".\").root)')/.fmf/version && tmt stories lint --source $@" PAD + entry: tmt-pre-commit --lint-type stories files: '.*\.fmf$' verbose: true pass_filenames: true diff --git a/setup.py b/setup.py index a68aae29c5..2675dfdd0b 100755 --- a/setup.py +++ b/setup.py @@ -139,5 +139,8 @@ packages=__pkgs__, provides=__provides__, scripts=__scripts__, + entry_points={ + 'console_scripts': ['tmt-pre-commit=tmt.cli:pre_commit'] + }, version=__version__ ) diff --git a/tmt.spec b/tmt.spec index 8358e14f39..54c79c4653 100644 --- a/tmt.spec +++ b/tmt.spec @@ -178,6 +178,8 @@ package to have all available plugins ready for testing. %install %py3_install +# Executable only used for pre-commit. Should not be installed +rm %{buildroot}/%{_bindir}/tmt-pre-commit mkdir -p %{buildroot}%{_mandir}/man1 mkdir -p %{buildroot}/etc/bash_completion.d/ diff --git a/tmt/cli.py b/tmt/cli.py index 87f3e7b6c6..c6ae96fd26 100644 --- a/tmt/cli.py +++ b/tmt/cli.py @@ -1618,3 +1618,47 @@ def completion_fish(context: Context, install: bool, **kwargs: Any) -> None: Setup shell completions for fish. """ setup_completion('fish', install) + + +@click.command(context_settings=dict( + ignore_unknown_options=True, + allow_extra_args=True, + )) +@click.pass_context +@click.option( + '-r', '--root', metavar='PATH', show_default=True, default='.', + help='Variable passed to tmt main. See `tmt --help`') +@click.option( + '-v', '--verbose', count=True, default=0, + help='Variable passed to tmt main. See `tmt --help`') +@click.option( + '--version', is_flag=True, + help="Variable passed to tmt main. See `tmt --help`") +@click.option( + '--lint-type', default=None, + type=click.Choice(['tests', 'plans', 'stories']), + help='tmt types to lint') +def pre_commit( + context: Context, + root: str, + verbose: int, + version: bool, + lint_type: Optional[str]) -> None: + """ + Cli wrapper of tmt lint for + """ + # Special handling for --version + if version: + main(['--version']) + # Construct the arguments for main cli + args = ['--root', root] + if verbose: + args += ['-' + 'v' * verbose] + if lint_type: + args += [lint_type] + args += ['lint', '--source'] + # Get everything else + if '--source' in context.args: + context.args.remove('--source') + args += context.args + main(args)