Skip to content

Commit

Permalink
WIP: PYTEST_FORCE_COLOR, NO_COLOR
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Sep 9, 2020
1 parent 0749dfd commit 1ccb77a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/_pytest/_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@


def use_markup(file: "TextIO") -> bool:
# Backward compatibility with pylib: handle PY_COLORS={0,1} only.
val = os.getenv("PY_COLORS")
if val in ("0", "1"):
return val == "1"

# PYTEST_FORCE_COLOR: handled as boolean.
val = os.getenv("PYTEST_FORCE_COLOR")
if val is not None:
from _pytest.config import _strtobool

return _strtobool(val)

from _pytest.assertion.util import _running_on_ci
# NO_COLOR: disable markup with any value (https://no-color.org/).
if "NO_COLOR" in os.environ:
return False

if _running_on_ci():
return True
Expand Down Expand Up @@ -103,3 +111,7 @@ def _highlight(self, source):
return source
else:
return highlight(source, PythonLexer(), TerminalFormatter(bg="dark"))


def _running_on_ci():
return os.environ.get("CI", "").lower() == "true" or "BUILD_NUMBER" in os.environ
8 changes: 6 additions & 2 deletions src/_pytest/assertion/truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
~8 terminal lines, unless running in "-vv" mode or running on CI.
"""
from ..compat import TYPE_CHECKING
from _pytest.assertion.util import _running_on_ci

if TYPE_CHECKING:
from typing import List
Expand All @@ -28,7 +27,12 @@ def _should_truncate(config: "Config") -> bool:
level = config.getini("assert_truncate_level") # type: str
verbose = config.option.verbose # type: int
if level == "auto":
return verbose < 2 and not _running_on_ci()
if verbose >= 2:
return False

from _pytest._io import _running_on_ci

return not _running_on_ci()
return int(level) > verbose


Expand Down
5 changes: 0 additions & 5 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utilities for assertion debugging"""
import collections.abc
import itertools
import os
import pprint
import re
from typing import AbstractSet
Expand Down Expand Up @@ -534,7 +533,3 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
"? " + indent + marker,
]
return newdiff


def _running_on_ci():
return os.environ.get("CI", "").lower() == "true" or "BUILD_NUMBER" in os.environ
10 changes: 7 additions & 3 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from _pytest import nodes
from _pytest._code.code import ExceptionInfo
from _pytest._code.code import ReprFileLocation
from _pytest.assertion.util import _running_on_ci
from _pytest.compat import order_preserving_dict
from _pytest.compat import shell_quote
from _pytest.compat import TYPE_CHECKING
Expand Down Expand Up @@ -1214,10 +1213,15 @@ def short_test_summary(self) -> None:
if not self.reportchars:
return

if not self.isatty or _running_on_ci():
if not self.isatty:
termwidth = None
else:
termwidth = self._tw.fullwidth
from _pytest._io import _running_on_ci

if _running_on_ci():
termwidth = None
else:
termwidth = self._tw.fullwidth

def show_simple(stat, lines: List[str]) -> None:
failed = self.stats.get(stat, [])
Expand Down

0 comments on commit 1ccb77a

Please sign in to comment.