diff --git a/tests/conftest.py b/tests/conftest.py index 2118f9dc..7a968c57 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -82,3 +82,15 @@ def docs_test_env(): yield setenv setenv.clear() + + +@pytest.fixture +def cli_test_env(): + setenv = SetEnv() + + # envs for reproducible cli tests + setenv.set('COLUMNS', '80') + + yield setenv + + setenv.clear() diff --git a/tests/test_settings.py b/tests/test_settings.py index 95a4b964..8e6297e8 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -7,6 +7,7 @@ from enum import IntEnum from pathlib import Path from typing import Any, Callable, Dict, Generic, Hashable, List, Optional, Set, Tuple, Type, TypeVar, Union +from unittest import mock import pytest from annotated_types import MinLen @@ -68,6 +69,12 @@ class SettingWithPopulateByName(BaseSettings): model_config = SettingsConfigDict(populate_by_name=True) +@pytest.fixture(autouse=True) +def clean_env(): + with mock.patch.dict(os.environ, clear=True): + yield + + def test_sub_env(env): env.set('apple', 'hello') s = SimpleSettings() @@ -1109,9 +1116,13 @@ class Settings(BaseSettings): @pytest.fixture -def home_tmp(): +def home_tmp(tmp_path, env): + env.set('HOME', str(tmp_path)) + env.set('USERPROFILE', str(tmp_path)) + env.set('HOMEPATH', str(tmp_path)) + tmp_filename = f'{uuid.uuid4()}.env' - home_tmp_path = Path.home() / tmp_filename + home_tmp_path = tmp_path / tmp_filename yield home_tmp_path, tmp_filename home_tmp_path.unlink() diff --git a/tests/test_source_cli.py b/tests/test_source_cli.py index 7101d687..79aee160 100644 --- a/tests/test_source_cli.py +++ b/tests/test_source_cli.py @@ -44,6 +44,11 @@ ARGPARSE_OPTIONS_TEXT = 'options' if sys.version_info >= (3, 10) else 'optional arguments' +@pytest.fixture(autouse=True) +def cli_test_env_autouse(cli_test_env): + pass + + def foobar(a, b, c=4): pass