From 6ce29ffc68b3404d05b31d2814d84f9ca41cb949 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 10 Oct 2024 16:00:47 +0300 Subject: [PATCH] New option: --allow-non-packages I can finally use this on my Vim plugins like https://github.com/mgedmin/taghelper.vim --- CHANGES.rst | 5 +++-- src/check_python_versions/cli.py | 14 +++++++++++--- tests/test_cli.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index cb609bb..4b2de77 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,10 +1,11 @@ Changelog ========= -0.22.2 (unreleased) +0.23.0 (unreleased) ------------------- -- Nothing changed yet. +- New option ``--allow-non-packages`` so you can automate tox.ini or GitHub + workflow updates in projects that are not Python packages. 0.22.1 (2024-10-09) diff --git a/src/check_python_versions/cli.py b/src/check_python_versions/cli.py index b1cba9d..8d61a01 100644 --- a/src/check_python_versions/cli.py +++ b/src/check_python_versions/cli.py @@ -370,6 +370,10 @@ def _main() -> None: parser.add_argument('--skip-non-packages', action='store_true', help='skip arguments that are not Python packages' ' without warning about them') + parser.add_argument('--allow-non-packages', action='store_true', + help='try to work on directories that are not Python' + ' packages but have a tox.ini' + ' or .github/workflows') parser.add_argument('--only', metavar='FILES', help='check only the specified files' ' (comma-separated list, e.g.' @@ -399,6 +403,9 @@ def _main() -> None: parser.error("argument --add: not allowed with argument --update") if args.update and args.drop: parser.error("argument --drop: not allowed with argument --update") + if args.skip_non_packages and args.allow_non_packages: + parser.error("use either --skip-non-packages or --allow-non-packages," + " not both") if args.diff and not (args.update or args.add or args.drop): parser.error( "argument --diff: not allowed without --update/--add/--drop") @@ -431,9 +438,10 @@ def _main() -> None: if n: print("\n") print(f"{path}:\n") - if not check_package(path): - mismatches.append(path) - continue + if not args.allow_non_packages: + if not check_package(path): + mismatches.append(path) + continue replacements = {} if args.add or args.drop or args.update: replacements = update_versions( diff --git a/tests/test_cli.py b/tests/test_cli.py index 4328c02..2bef24f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -739,6 +739,34 @@ def test_main_skip_non_packages(monkeypatch, capsys, tmp_path): assert capsys.readouterr().out == '' +def test_main_allow_non_packages(monkeypatch, capsys, tmp_path): + monkeypatch.setattr(sys, 'argv', [ + 'check-python-versions', '--allow-non-packages', str(tmp_path), + ]) + with pytest.raises(SystemExit) as exc_info: + cpv.main() + assert ( + capsys.readouterr().out + str(exc_info.value) + '\n' + ).replace(str(tmp_path), 'tmp') == textwrap.dedent("""\ + no file with version information found + + mismatch! + """) + + +def test_main_conflicting_non_packages_options(monkeypatch, capsys, tmp_path): + monkeypatch.setattr(sys, 'argv', [ + 'check-python-versions', '--skip-non-packages', '--allow-non-packages', + str(tmp_path), + ]) + with pytest.raises(SystemExit): + cpv.main() + assert ( + 'use either --skip-non-packages or --allow-non-packages, not both' + in capsys.readouterr().err + ) + + def test_main_single(monkeypatch, capsys, tmp_path): monkeypatch.setattr(sys, 'argv', [ 'check-python-versions',