# 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/envcontext.py
# UNSET = _Unset.UNSET
# 
# Path: pre_commit/envcontext.py
# class Var(NamedTuple):
#     name: str
#     default: str = ''


import os
import pytest
from unittest import mock
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var


def _test(*, before, patch, expected):
    env = before.copy()
    with envcontext(patch, _env=env):
        assert env == expected
    assert env == before


def test_trivial():
    _test(before={}, patch={}, expected={})


def test_noop():
    _test(before={'foo': 'bar'}, patch=(), expected={'foo': 'bar'})


def test_adds():
    _test(before={}, patch=[('foo', 'bar')], expected={'foo': 'bar'})


def test_overrides():
    _test(
        before={'foo': 'baz'},
        patch=[('foo', 'bar')],
        expected={'foo': 'bar'},
    )


def test_unset_but_nothing_to_unset():

# 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/envcontext.py
# UNSET = _Unset.UNSET
# 
# Path: pre_commit/envcontext.py
# class Var(NamedTuple):
#     name: str
#     default: str = ''


# Path: tests/envcontext_test.py
import os
import pytest
from unittest import mock
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var



def _test(*, before, patch, expected):
    env = before.copy()
    with envcontext(patch, _env=env):
        assert env == expected
    assert env == before


def test_trivial():
    _test(before={}, patch={}, expected={})


def test_noop():
    _test(before={'foo': 'bar'}, patch=(), expected={'foo': 'bar'})


def test_adds():
    _test(before={}, patch=[('foo', 'bar')], expected={'foo': 'bar'})


def test_overrides():
    _test(
        before={'foo': 'baz'},
        patch=[('foo', 'bar')],
        expected={'foo': 'bar'},
    )


def test_unset_but_nothing_to_unset():

    _test(before={}, patch=[('foo', UNSET)], expected={})