# Path: pre_commit/envcontext.py
# @contextlib.contextmanager
# def envcontext(
#         patch: PatchesT,
#         _env: MutableMapping[str, str] | None = None,
# ) -> Generator[None, None, None]:
#     """In this context, `os.environ` is modified according to `patch`.
# 
#     `patch` is an iterable of 2-tuples (key, value):
#         `key`: string
#         `value`:
#             - string: `environ[key] == value` inside the context.
#             - UNSET: `key not in environ` inside the context.
#             - template: A template is a tuple of strings and Var which will be
#               replaced with the previous environment
#     """
#     env = os.environ if _env is None else _env
#     before = dict(env)
# 
#     for k, v in patch:
#         if v is UNSET:
#             env.pop(k, None)
#         elif isinstance(v, tuple):
#             env[k] = format_env(v, before)
#         else:
#             env[k] = v
# 
#     try:
#         yield
#     finally:
#         env.clear()
#         env.update(before)
# 
# Path: pre_commit/color.py
# def format_color(text: str, color: str, use_color_setting: bool) -> str:
#     """Format text with color.
# 
#     Args:
#         text - Text to be formatted with color if `use_color`
#         color - The color start string
#         use_color_setting - Whether or not to color
#     """
#     if use_color_setting:
#         return f'{color}{text}{NORMAL}'
#     else:
#         return text
# 
# Path: pre_commit/color.py
# GREEN = '\033[42m'
# 
# Path: pre_commit/color.py
# def use_color(setting: str) -> bool:
#     """Choose whether to use color based on the command argument.
# 
#     Args:
#         setting - Either `auto`, `always`, or `never`
#     """
#     if setting not in COLOR_CHOICES:
#         raise ValueError(setting)
# 
#     return (
#         setting == 'always' or (
#             setting == 'auto' and
#             sys.stderr.isatty() and
#             terminal_supports_color and
#             os.getenv('TERM') != 'dumb'
#         )
#     )


import sys
import pytest
from unittest import mock
from pre_commit import envcontext
from pre_commit.color import format_color
from pre_commit.color import GREEN
from pre_commit.color import use_color
from __future__ import annotations





@pytest.mark.parametrize(
    ('in_text', 'in_color', 'in_use_color', 'expected'), (
        ('foo', GREEN, True, f'{GREEN}foo\033[m'),
        ('foo', GREEN, False, 'foo'),
    ),
)
def test_format_color(in_text, in_color, in_use_color, expected):
    ret = format_color(in_text, in_color, in_use_color)
    assert ret == expected


def test_use_color_never():

# Path: pre_commit/envcontext.py
# @contextlib.contextmanager
# def envcontext(
#         patch: PatchesT,
#         _env: MutableMapping[str, str] | None = None,
# ) -> Generator[None, None, None]:
#     """In this context, `os.environ` is modified according to `patch`.
# 
#     `patch` is an iterable of 2-tuples (key, value):
#         `key`: string
#         `value`:
#             - string: `environ[key] == value` inside the context.
#             - UNSET: `key not in environ` inside the context.
#             - template: A template is a tuple of strings and Var which will be
#               replaced with the previous environment
#     """
#     env = os.environ if _env is None else _env
#     before = dict(env)
# 
#     for k, v in patch:
#         if v is UNSET:
#             env.pop(k, None)
#         elif isinstance(v, tuple):
#             env[k] = format_env(v, before)
#         else:
#             env[k] = v
# 
#     try:
#         yield
#     finally:
#         env.clear()
#         env.update(before)
# 
# Path: pre_commit/color.py
# def format_color(text: str, color: str, use_color_setting: bool) -> str:
#     """Format text with color.
# 
#     Args:
#         text - Text to be formatted with color if `use_color`
#         color - The color start string
#         use_color_setting - Whether or not to color
#     """
#     if use_color_setting:
#         return f'{color}{text}{NORMAL}'
#     else:
#         return text
# 
# Path: pre_commit/color.py
# GREEN = '\033[42m'
# 
# Path: pre_commit/color.py
# def use_color(setting: str) -> bool:
#     """Choose whether to use color based on the command argument.
# 
#     Args:
#         setting - Either `auto`, `always`, or `never`
#     """
#     if setting not in COLOR_CHOICES:
#         raise ValueError(setting)
# 
#     return (
#         setting == 'always' or (
#             setting == 'auto' and
#             sys.stderr.isatty() and
#             terminal_supports_color and
#             os.getenv('TERM') != 'dumb'
#         )
#     )


# Path: tests/color_test.py
import sys
import pytest
from unittest import mock
from pre_commit import envcontext
from pre_commit.color import format_color
from pre_commit.color import GREEN
from pre_commit.color import use_color

from __future__ import annotations





@pytest.mark.parametrize(
    ('in_text', 'in_color', 'in_use_color', 'expected'), (
        ('foo', GREEN, True, f'{GREEN}foo\033[m'),
        ('foo', GREEN, False, 'foo'),
    ),
)
def test_format_color(in_text, in_color, in_use_color, expected):
    ret = format_color(in_text, in_color, in_use_color)
    assert ret == expected


def test_use_color_never():

    assert use_color('never') is False