Skip to content

Commit

Permalink
Bug fix for issue #100 (disable colored text on output redirection)
Browse files Browse the repository at this point in the history
Details: #100
  • Loading branch information
xolox committed Dec 9, 2020
1 parent 3935774 commit 4f8c9aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions coloredlogs/__init__.py
@@ -1,7 +1,7 @@
# Colored terminal output for Python's logging module.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: February 16, 2020
# Last Change: December 8, 2020
# URL: https://coloredlogs.readthedocs.io

"""
Expand Down Expand Up @@ -384,7 +384,7 @@ def install(level=None, **kw):
"""
logger = kw.get('logger') or logging.getLogger()
reconfigure = kw.get('reconfigure', True)
stream = kw.get('stream', None)
stream = kw.get('stream', sys.stderr)
style = check_style(kw.get('style') or DEFAULT_FORMAT_STYLE)
# Get the log level from an argument, environment variable or default and
# convert the names of log levels to numbers to enable numeric comparison.
Expand Down
33 changes: 31 additions & 2 deletions coloredlogs/tests.py
@@ -1,7 +1,7 @@
# Automated tests for the `coloredlogs' package.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: February 16, 2020
# Last Change: December 8, 2020
# URL: https://coloredlogs.readthedocs.io

"""Automated tests for the `coloredlogs` package."""
Expand All @@ -18,7 +18,7 @@

# External dependencies.
from humanfriendly.compat import StringIO
from humanfriendly.terminal import ANSI_COLOR_CODES, ansi_style, ansi_wrap
from humanfriendly.terminal import ANSI_COLOR_CODES, ANSI_CSI, ansi_style, ansi_wrap
from humanfriendly.testing import PatchedAttribute, PatchedItem, TestCase, retry
from humanfriendly.text import format, random_string

Expand Down Expand Up @@ -389,6 +389,35 @@ def test_plain_text_output_format(self):
assert severity in last_line
assert PLAIN_TEXT_PATTERN.match(last_line)

def test_auto_disable(self):
"""
Make sure ANSI escape sequences are not emitted when logging output is being redirected.
This is a regression test for https://github.com/xolox/python-coloredlogs/issues/100.
It works as follows:
1. We mock an interactive terminal using 'capturer' to ensure that this
test works inside test drivers that capture output (like pytest).
2. We launch a subprocess (to ensure a clean process state) where
stderr is captured but stdout is not, emulating issue #100.
3. The output captured on stderr contained ANSI escape sequences after
this test was written and before the issue was fixed, so now this
serves as a regression test for issue #100.
"""
with CaptureOutput():
interpreter = subprocess.Popen([
sys.executable, "-c", ";".join([
"import coloredlogs, logging",
"coloredlogs.install()",
"logging.info('Hello world')",
]),
], stderr=subprocess.PIPE)
stdout, stderr = interpreter.communicate()
assert ANSI_CSI not in stderr.decode('UTF-8')

def test_html_conversion(self):
"""Check the conversion from ANSI escape sequences to HTML."""
# Check conversion of colored text.
Expand Down

0 comments on commit 4f8c9aa

Please sign in to comment.