Skip to content

always show full diff in ci #9025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/9023.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

Full diffs are now always shown for equality assertions of iterables when
`CI` or ``BUILD_NUMBER`` is found in the environment, even when ``-v`` isn't
used.
10 changes: 2 additions & 8 deletions src/_pytest/assertion/truncate.py
Original file line number Diff line number Diff line change
@@ -3,10 +3,10 @@
Current default behaviour is to truncate assertion explanations at
~8 terminal lines, unless running in "-vv" mode or running on CI.
"""
import os
from typing import List
from typing import Optional

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


@@ -27,13 +27,7 @@ def truncate_if_required(
def _should_truncate_item(item: Item) -> bool:
"""Whether or not this test item is eligible for truncation."""
verbose = item.config.option.verbose
return verbose < 2 and not _running_on_ci()


def _running_on_ci() -> bool:
"""Check if we're currently running on a CI system."""
env_vars = ["CI", "BUILD_NUMBER"]
return any(var in os.environ for var in env_vars)
return verbose < 2 and not util.running_on_ci()


def _truncate_explanation(
10 changes: 8 additions & 2 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Utilities for assertion debugging."""
import collections.abc
import os
import pprint
from typing import AbstractSet
from typing import Any
@@ -17,7 +18,6 @@
from _pytest._io.saferepr import saferepr
from _pytest.config import Config


# The _reprcompare attribute on the util module is used by the new assertion
# interpretation code and assertion rewriter to detect this plugin was
# loaded and in turn call the hooks defined here as part of the
@@ -287,7 +287,7 @@ def _surrounding_parens_on_own_lines(lines: List[str]) -> None:
def _compare_eq_iterable(
left: Iterable[Any], right: Iterable[Any], verbose: int = 0
) -> List[str]:
if not verbose:
if not verbose and not running_on_ci():
return ["Use -v to get the full diff"]
# dynamic import to speedup pytest
import difflib
@@ -490,3 +490,9 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
else:
newdiff.append(line)
return newdiff


def running_on_ci() -> bool:
"""Check if we're currently running on a CI system."""
env_vars = ["CI", "BUILD_NUMBER"]
return any(var in os.environ for var in env_vars)
20 changes: 20 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
from _pytest import outcomes
from _pytest.assertion import truncate
from _pytest.assertion import util
from _pytest.monkeypatch import MonkeyPatch
from _pytest.pytester import Pytester


@@ -448,6 +449,25 @@ def test_iterable_full_diff(self, left, right, expected) -> None:
assert verbose_expl is not None
assert "\n".join(verbose_expl).endswith(textwrap.dedent(expected).strip())

def test_iterable_full_diff_ci(
self, monkeypatch: MonkeyPatch, pytester: Pytester
) -> None:
pytester.makepyfile(
r"""
def test_full_diff():
left = [0, 1]
right = [0, 2]
assert left == right
"""
)
monkeypatch.setenv("CI", "true")
result = pytester.runpytest()
result.stdout.fnmatch_lines(["E Full diff:"])

monkeypatch.delenv("CI", raising=False)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["E Use -v to get the full diff"])

def test_list_different_lengths(self) -> None:
expl = callequal([0, 1], [0, 1, 2])
assert expl is not None