mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-04 20:26:53 +00:00
add --color option
This commit is contained in:
parent
05cae7e046
commit
848003cc05
10 changed files with 181 additions and 2 deletions
|
|
@ -1,15 +1,18 @@
|
|||
"""Tests for the BaseFormatter object."""
|
||||
import argparse
|
||||
import sys
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from flake8 import style_guide
|
||||
from flake8.formatting import _windows_color
|
||||
from flake8.formatting import base
|
||||
|
||||
|
||||
def options(**kwargs):
|
||||
"""Create an argparse.Namespace instance."""
|
||||
kwargs.setdefault("color", "auto")
|
||||
kwargs.setdefault("output_file", None)
|
||||
kwargs.setdefault("tee", False)
|
||||
return argparse.Namespace(**kwargs)
|
||||
|
|
@ -136,6 +139,49 @@ def test_write_produces_stdout(capsys):
|
|||
assert capsys.readouterr().out == f"{line}\n{source}\n"
|
||||
|
||||
|
||||
def test_color_always_is_true():
|
||||
"""Verify that color='always' sets it to True."""
|
||||
formatter = base.BaseFormatter(options(color="always"))
|
||||
assert formatter.color is True
|
||||
|
||||
|
||||
def _mock_isatty(val):
|
||||
attrs = {"isatty.return_value": val}
|
||||
return mock.patch.object(sys, "stdout", **attrs)
|
||||
|
||||
|
||||
def _mock_windows_color(val):
|
||||
return mock.patch.object(_windows_color, "terminal_supports_color", val)
|
||||
|
||||
|
||||
def test_color_auto_is_true_for_tty():
|
||||
"""Verify that color='auto' sets it to True for a tty."""
|
||||
with _mock_isatty(True), _mock_windows_color(True):
|
||||
formatter = base.BaseFormatter(options(color="auto"))
|
||||
assert formatter.color is True
|
||||
|
||||
|
||||
def test_color_auto_is_false_without_tty():
|
||||
"""Verify that color='auto' sets it to False without a tty."""
|
||||
with _mock_isatty(False), _mock_windows_color(True):
|
||||
formatter = base.BaseFormatter(options(color="auto"))
|
||||
assert formatter.color is False
|
||||
|
||||
|
||||
def test_color_auto_is_false_if_not_supported_on_windows():
|
||||
"""Verify that color='auto' is False if not supported on windows."""
|
||||
with _mock_isatty(True), _mock_windows_color(False):
|
||||
formatter = base.BaseFormatter(options(color="auto"))
|
||||
assert formatter.color is False
|
||||
|
||||
|
||||
def test_color_never_is_false():
|
||||
"""Verify that color='never' sets it to False despite a tty."""
|
||||
with _mock_isatty(True), _mock_windows_color(True):
|
||||
formatter = base.BaseFormatter(options(color="never"))
|
||||
assert formatter.color is False
|
||||
|
||||
|
||||
class AfterInitFormatter(base.BaseFormatter):
|
||||
"""Subclass for testing after_init."""
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ from flake8.formatting import default
|
|||
|
||||
def options(**kwargs):
|
||||
"""Create an argparse.Namespace instance."""
|
||||
kwargs.setdefault("color", "auto")
|
||||
kwargs.setdefault("output_file", None)
|
||||
kwargs.setdefault("tee", False)
|
||||
return argparse.Namespace(**kwargs)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ from flake8.formatting import default
|
|||
|
||||
def options(**kwargs):
|
||||
"""Create an argparse.Namespace instance."""
|
||||
kwargs.setdefault("color", "auto")
|
||||
kwargs.setdefault("output_file", None)
|
||||
kwargs.setdefault("tee", False)
|
||||
return argparse.Namespace(**kwargs)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue