diff --git a/CHANGELOG.md b/CHANGELOG.md index ea99c87..a3d0f38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - added official support for Python 3.15 ### Fixes +- `--order-scope-level` rejects negative values instead of accepting an invalid + scope level - class-level relative ordering resolves nested classes and includes their nested tests - relative ordering resolves whole-test labels when parameter IDs contain brackets or node separators (see [#272](https://github.com/pytest-dev/pytest-order/issues/272)) diff --git a/src/pytest_order/plugin.py b/src/pytest_order/plugin.py index f420ee9..c3bbf7d 100644 --- a/src/pytest_order/plugin.py +++ b/src/pytest_order/plugin.py @@ -1,3 +1,4 @@ +from argparse import ArgumentTypeError from collections.abc import Callable, Generator import pytest @@ -10,6 +11,13 @@ from .sorter import Sorter +def _non_negative_int(value: str) -> int: + number = int(value) + if number < 0: + raise ArgumentTypeError(f"{value} is not a non-negative integer") + return number + + def pytest_configure(config: Config) -> None: """ Register the "order" marker and configure the plugin, @@ -73,7 +81,7 @@ def pytest_addoption(parser: Parser) -> None: group.addoption( "--order-scope-level", action="store", - type=int, + type=_non_negative_int, dest="order_scope_level", help=( "Defines that the given directory level is used as order scope. " diff --git a/tests/test_order_scope_level.py b/tests/test_order_scope_level.py index 57bb166..38da327 100644 --- a/tests/test_order_scope_level.py +++ b/tests/test_order_scope_level.py @@ -124,6 +124,14 @@ def test_dir_level2(fixture_path, capsys): ) +def test_negative_scope_level(fixture_path): + result = fixture_path.runpytest("--order-scope-level=-1") + assert result.ret == pytest.ExitCode.USAGE_ERROR + result.stderr.fnmatch_lines( + ["*error: argument --order-scope-level: -1 is not a non-negative integer"] + ) + + @pytest.mark.skipif( pytest.__version__.startswith("3.7."), reason="Warning does not appear in output in pytest < 3.8",