From 91ed1c280e2008e8fc45f0bd2b29520e043acc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Sun, 3 Nov 2024 20:44:41 +0500 Subject: [PATCH 1/3] Fix running `test_source_cli` with unusual COLUMNS values Tests expect help text to be wrapped at 80 characters. Issue: https://github.com/pydantic/pydantic-settings/issues/463 --- tests/conftest.py | 12 ++++++++++++ tests/test_source_cli.py | 5 +++++ 2 files changed, 17 insertions(+) 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_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 From 00d1aaca4c2f30e4d4c3f948303a5c6333a969cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Sun, 3 Nov 2024 21:17:33 +0500 Subject: [PATCH 2/3] Strip environment for `test_settings` Certain variables, when set in the running environment, can make tests fail. As environment variable names are case-insensitive by default, it's better to start with a clean environment. Issue: https://github.com/pydantic/pydantic-settings/issues/463 --- tests/test_settings.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_settings.py b/tests/test_settings.py index 95a4b964..7321fde6 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() From cf4aa52cdc7347b776bb9ad564b1be0ef211aee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anna=20=E2=80=9CCyberTailor=E2=80=9D?= Date: Sun, 3 Nov 2024 21:46:13 +0500 Subject: [PATCH 3/3] Use temporary home directory for `test_env_file_home_directory` Issue: https://github.com/pydantic/pydantic-settings/issues/463 --- tests/test_settings.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_settings.py b/tests/test_settings.py index 7321fde6..8e6297e8 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1116,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()