Repository: pytest-dev/pytest
File: src/_pytest/assertion/truncate.py (Python)

Code before:
from __future__ import annotations

from _pytest.assertion import util
from _pytest.config import Config
from _pytest.nodes import Item


DEFAULT_MAX_LINES = 8
DEFAULT_MAX_CHARS = DEFAULT_MAX_LINES * 80
USAGE_MSG = "use '-vv' to show"


def truncate_if_required(explanation: list[str], item: Item) -> list[str]:
    """Truncate this assertion explanation if the given test item is eligible."""
    should_truncate, max_lines, max_chars = _get_truncation_parameters(item)
    if should_truncate:
        return _truncate_explanation(
            explanation,
            max_lines=max_lines,
            max_chars=max_chars,
        )
    return explanation


def _get_truncation_parameters(item: Item) -> tuple[bool, int, int]:
    """Whether or not this test item is eligible for truncation."""
    # We do not need to truncate if one of conditions is met:
    # 1. Verbosity level is 2 or more;
    # 2. Test is being run in CI environment;
    # 3. Both truncation_limit_lines and truncation_limit_chars
    #    .ini parameters are set to 0 explicitly.
    max_lines = item.config.getini("truncation_limit_lines")
    max_lines = int(max_lines if max_lines is not None else DEFAULT_MAX_LINES)

    max_chars = item.config.getini("truncation_limit_chars")
    max_chars = int(max_chars if max_chars is not None else DEFAULT_MAX_CHARS)

    verbose = item.config.get_verbosity(Config.VERBOSITY_ASSERTIONS)

    should_truncate = verbose < 2 and not util.running_on_ci()
    should_truncate = should_truncate and (max_lines > 0 or max_chars > 0)

    return should_truncate, max_lines, max_chars


def _truncate_explanation(
    input_lines: list[str],
    max_lines: int,
    max_chars: int,
) -> list[str]:
    """Truncate given list of strings that makes up the assertion explanation.

Review comment (suggestion):
We should update the docstring:

```suggestion
    """Return the truncation parameters related to the given item, as (should truncate, max lines, max chars)."""
```

Code after:
from __future__ import annotations

from _pytest.assertion import util
from _pytest.config import Config
from _pytest.nodes import Item


DEFAULT_MAX_LINES = 8
DEFAULT_MAX_CHARS = DEFAULT_MAX_LINES * 80
USAGE_MSG = "use '-vv' to show"


def truncate_if_required(explanation: list[str], item: Item) -> list[str]:
    """Truncate this assertion explanation if the given test item is eligible."""
    should_truncate, max_lines, max_chars = _get_truncation_parameters(item)
    if should_truncate:
        return _truncate_explanation(
            explanation,
            max_lines=max_lines,
            max_chars=max_chars,
        )
    return explanation


def _get_truncation_parameters(item: Item) -> tuple[bool, int, int]:
    """Return the truncation parameters related to the given item, as (should truncate, max lines, max chars)."""
    # We do not need to truncate if one of conditions is met:
    # 1. Verbosity level is 2 or more;
    # 2. Test is being run in CI environment;
    # 3. Both truncation_limit_lines and truncation_limit_chars
    #    .ini parameters are set to 0 explicitly.
    max_lines = item.config.getini("truncation_limit_lines")
    max_lines = int(max_lines if max_lines is not None else DEFAULT_MAX_LINES)

    max_chars = item.config.getini("truncation_limit_chars")
    max_chars = int(max_chars if max_chars is not None else DEFAULT_MAX_CHARS)

    verbose = item.config.get_verbosity(Config.VERBOSITY_ASSERTIONS)

    should_truncate = verbose < 2 and not util.running_on_ci()
    should_truncate = should_truncate and (max_lines > 0 or max_chars > 0)

    return should_truncate, max_lines, max_chars


def _truncate_explanation(
    input_lines: list[str],
    max_lines: int,
    max_chars: int,
) -> list[str]:
    """Truncate given list of strings that makes up the assertion explanation.