From 78398b48e2e1123230ced40a7ba6a280369ae68e Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 8 Jul 2020 22:43:11 +0300 Subject: [PATCH 1/7] Add support for NO_COLOR and FORCE_COLOR --- changelog/7464.improvement.rst | 1 + src/_pytest/_io/terminalwriter.py | 4 ++++ testing/io/test_terminalwriter.py | 34 +++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 changelog/7464.improvement.rst diff --git a/changelog/7464.improvement.rst b/changelog/7464.improvement.rst new file mode 100644 index 00000000000..97d65a0e834 --- /dev/null +++ b/changelog/7464.improvement.rst @@ -0,0 +1 @@ +Add support for ``NO_COLOR`` and ``FORCE_COLOR``. diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index 70bb2e2dcd6..21fd6e56b64 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -27,6 +27,10 @@ def should_do_markup(file: TextIO) -> bool: return True if os.environ.get("PY_COLORS") == "0": return False + if "NO_COLOR" in os.environ: + return False + if "FORCE_COLOR" in os.environ: + return True return ( hasattr(file, "isatty") and file.isatty() diff --git a/testing/io/test_terminalwriter.py b/testing/io/test_terminalwriter.py index 94cff307fcd..b36a7bb6a11 100644 --- a/testing/io/test_terminalwriter.py +++ b/testing/io/test_terminalwriter.py @@ -154,8 +154,7 @@ def test_attr_hasmarkup() -> None: assert "\x1b[0m" in s -def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None: - monkeypatch.setitem(os.environ, "PY_COLORS", "1") +def assert_color_set(): file = io.StringIO() tw = terminalwriter.TerminalWriter(file) assert tw.hasmarkup @@ -166,8 +165,7 @@ def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None: assert "\x1b[0m" in s -def test_should_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: - monkeypatch.setitem(os.environ, "PY_COLORS", "0") +def assert_color_not_set(): f = io.StringIO() f.isatty = lambda: True # type: ignore tw = terminalwriter.TerminalWriter(file=f) @@ -177,6 +175,34 @@ def test_should_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: assert s == "hello\n" +def test_should_do_markup_PY_COLORS_eq_1(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "PY_COLORS", "1") + assert_color_set() + + +def test_should_not_do_markup_PY_COLORS_eq_0(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "PY_COLORS", "0") + assert_color_not_set() + + +def test_should_not_do_markup_NO_COLOR(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "NO_COLOR", "1") + assert_color_not_set() + + +def test_should_do_markup_FORCE_COLOR(monkeypatch: MonkeyPatch) -> None: + monkeypatch.setitem(os.environ, "FORCE_COLOR", "1") + assert_color_set() + + +def test_should_not_do_markup_NO_COLOR_and_FORCE_COLOR( + monkeypatch: MonkeyPatch, +) -> None: + monkeypatch.setitem(os.environ, "NO_COLOR", "1") + monkeypatch.setitem(os.environ, "FORCE_COLOR", "1") + assert_color_not_set() + + class TestTerminalWriterLineWidth: def test_init(self) -> None: tw = terminalwriter.TerminalWriter() From 1add88430ee3e54786e2e5588fc33f503a3365ad Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Wed, 8 Jul 2020 22:44:25 +0300 Subject: [PATCH 2/7] Remove redundant check; Jython is no longer supported --- src/_pytest/_io/terminalwriter.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/_pytest/_io/terminalwriter.py b/src/_pytest/_io/terminalwriter.py index 21fd6e56b64..0168dc13d4d 100644 --- a/src/_pytest/_io/terminalwriter.py +++ b/src/_pytest/_io/terminalwriter.py @@ -32,10 +32,7 @@ def should_do_markup(file: TextIO) -> bool: if "FORCE_COLOR" in os.environ: return True return ( - hasattr(file, "isatty") - and file.isatty() - and os.environ.get("TERM") != "dumb" - and not (sys.platform.startswith("java") and os._name == "nt") + hasattr(file, "isatty") and file.isatty() and os.environ.get("TERM") != "dumb" ) From 9d9c1a35c1e7e1677beffcf984f4f30d0160495f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 9 Jul 2020 11:25:04 +0300 Subject: [PATCH 3/7] Document PY_COLORS, NO_COLOR and FORCE_COLOR --- doc/en/reference.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 94a2470953a..896ccc34cad 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -1010,6 +1010,23 @@ loaded. This is not meant to be set by users, but is set by pytest internally with the name of the current test so other processes can inspect it, see :ref:`pytest current test env` for more information. +.. envvar:: PY_COLORS + +When set to "1", pytest will use color in terminal output. +When set to "0", pytest will not use color. +``PY_COLORS`` takes precedence over ``NO_COLOR`` and ``FORCE_COLOR``. + +.. envvar:: NO_COLOR + +When set (regardless of value), pytest will not use color in terminal output. +``PY_COLORS`` takes precedence over ``NO_COLOR``, which takes precedence over ``FORCE_COLOR``. +See `no-color.org `__ for other libraries supporting this community standard. + +.. envvar:: FORCE_COLOR + +When set (regardless of value), pytest will use color in terminal output. +``PY_COLORS`` and ``NO_COLOR`` take precedence over ``FORCE_COLOR``. + Exceptions ---------- From 2c00ecb04ca11ab049e292702d336107000ce074 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 9 Jul 2020 14:23:35 +0300 Subject: [PATCH 4/7] Use code format for values --- doc/en/reference.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 896ccc34cad..2b1d055261c 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -1012,8 +1012,8 @@ processes can inspect it, see :ref:`pytest current test env` for more informatio .. envvar:: PY_COLORS -When set to "1", pytest will use color in terminal output. -When set to "0", pytest will not use color. +When set to ``1``, pytest will use color in terminal output. +When set to ``0``, pytest will not use color. ``PY_COLORS`` takes precedence over ``NO_COLOR`` and ``FORCE_COLOR``. .. envvar:: NO_COLOR From af27a67d4c87a96cd1012a4c4b28a1b050568146 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Thu, 9 Jul 2020 14:27:33 +0300 Subject: [PATCH 5/7] Alphabetise PYTEST_ env vars, with non-pytest ones by precendence --- doc/en/reference.rst | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 2b1d055261c..86ed89d897a 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -988,10 +988,20 @@ Environment variables that can be used to change pytest's behavior. This contains a command-line (parsed by the py:mod:`shlex` module) that will be **prepended** to the command line given by the user, see :ref:`adding default options` for more information. +.. envvar:: PYTEST_CURRENT_TEST + +This is not meant to be set by users, but is set by pytest internally with the name of the current test so other +processes can inspect it, see :ref:`pytest current test env` for more information. + .. envvar:: PYTEST_DEBUG When set, pytest will print tracing and debug information. +.. envvar:: PYTEST_DISABLE_PLUGIN_AUTOLOAD + +When set, disables plugin auto-loading through setuptools entrypoints. Only explicitly specified plugins will be +loaded. + .. envvar:: PYTEST_PLUGINS Contains comma-separated list of modules that should be loaded as plugins: @@ -1000,16 +1010,6 @@ Contains comma-separated list of modules that should be loaded as plugins: export PYTEST_PLUGINS=mymodule.plugin,xdist -.. envvar:: PYTEST_DISABLE_PLUGIN_AUTOLOAD - -When set, disables plugin auto-loading through setuptools entrypoints. Only explicitly specified plugins will be -loaded. - -.. envvar:: PYTEST_CURRENT_TEST - -This is not meant to be set by users, but is set by pytest internally with the name of the current test so other -processes can inspect it, see :ref:`pytest current test env` for more information. - .. envvar:: PY_COLORS When set to ``1``, pytest will use color in terminal output. From 5678bc4fb00b723098111d12f4db24ce55d7a7af Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 10 Jul 2020 08:21:37 -0300 Subject: [PATCH 6/7] Update changelog/7464.improvement.rst --- changelog/7464.improvement.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/changelog/7464.improvement.rst b/changelog/7464.improvement.rst index 97d65a0e834..db9d3c60415 100644 --- a/changelog/7464.improvement.rst +++ b/changelog/7464.improvement.rst @@ -1 +1,3 @@ -Add support for ``NO_COLOR`` and ``FORCE_COLOR``. +Added support for ``NO_COLOR`` and ``FORCE_COLOR`` environment variables to control colored output. + +For more information, see `the docs `__. From 993deb7e202d768cdfb9ad58e1a9b278fcee72fa Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 10 Jul 2020 08:22:17 -0300 Subject: [PATCH 7/7] Rename 7464.improvement.rst to 7464.feature.rst --- changelog/{7464.improvement.rst => 7464.feature.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{7464.improvement.rst => 7464.feature.rst} (100%) diff --git a/changelog/7464.improvement.rst b/changelog/7464.feature.rst similarity index 100% rename from changelog/7464.improvement.rst rename to changelog/7464.feature.rst