diff --git a/changelog/14586.bugfix.rst b/changelog/14586.bugfix.rst new file mode 100644 index 00000000000..54b72253df9 --- /dev/null +++ b/changelog/14586.bugfix.rst @@ -0,0 +1 @@ +Negative values passed to ``log_auto_indent`` now correctly fall back to no indentation, matching the documented behavior. diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 3204d43ee01..982e12cd8c2 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -186,10 +186,10 @@ def _get_auto_indent(auto_indent_option: int | str | bool | None) -> int: case True: return -1 case int(): - return auto_indent_option + return max(auto_indent_option, 0) case str(): try: - return int(auto_indent_option) + return max(int(auto_indent_option), 0) except ValueError: pass try: diff --git a/testing/logging/test_formatter.py b/testing/logging/test_formatter.py index cfe3bee68c4..28a31e7d105 100644 --- a/testing/logging/test_formatter.py +++ b/testing/logging/test_formatter.py @@ -149,6 +149,18 @@ def test_multiline_message() -> None: "dummypath 10 INFO Test Message line1\n line2" ) + record.auto_indent = "-5" + output = ai_off_style.format(record) + assert output == ( + "dummypath 10 INFO Test Message line1\nline2" + ) + + record.auto_indent = -5 + output = ai_off_style.format(record) + assert output == ( + "dummypath 10 INFO Test Message line1\nline2" + ) + def test_colored_short_level() -> None: logfmt = "%(levelname).1s %(message)s"