Skip to content

Commit 0922bc1

Browse files
authored
Make tests more robust to the running environment (#464)
1 parent b2c979c commit 0922bc1

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ def docs_test_env():
8282
yield setenv
8383

8484
setenv.clear()
85+
86+
87+
@pytest.fixture
88+
def cli_test_env():
89+
setenv = SetEnv()
90+
91+
# envs for reproducible cli tests
92+
setenv.set('COLUMNS', '80')
93+
94+
yield setenv
95+
96+
setenv.clear()

tests/test_settings.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from enum import IntEnum
88
from pathlib import Path
99
from typing import Any, Callable, Dict, Generic, Hashable, List, Optional, Set, Tuple, Type, TypeVar, Union
10+
from unittest import mock
1011

1112
import pytest
1213
from annotated_types import MinLen
@@ -68,6 +69,12 @@ class SettingWithPopulateByName(BaseSettings):
6869
model_config = SettingsConfigDict(populate_by_name=True)
6970

7071

72+
@pytest.fixture(autouse=True)
73+
def clean_env():
74+
with mock.patch.dict(os.environ, clear=True):
75+
yield
76+
77+
7178
def test_sub_env(env):
7279
env.set('apple', 'hello')
7380
s = SimpleSettings()
@@ -1109,9 +1116,13 @@ class Settings(BaseSettings):
11091116

11101117

11111118
@pytest.fixture
1112-
def home_tmp():
1119+
def home_tmp(tmp_path, env):
1120+
env.set('HOME', str(tmp_path))
1121+
env.set('USERPROFILE', str(tmp_path))
1122+
env.set('HOMEPATH', str(tmp_path))
1123+
11131124
tmp_filename = f'{uuid.uuid4()}.env'
1114-
home_tmp_path = Path.home() / tmp_filename
1125+
home_tmp_path = tmp_path / tmp_filename
11151126
yield home_tmp_path, tmp_filename
11161127
home_tmp_path.unlink()
11171128

tests/test_source_cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
ARGPARSE_OPTIONS_TEXT = 'options' if sys.version_info >= (3, 10) else 'optional arguments'
4545

4646

47+
@pytest.fixture(autouse=True)
48+
def cli_test_env_autouse(cli_test_env):
49+
pass
50+
51+
4752
def foobar(a, b, c=4):
4853
pass
4954

0 commit comments

Comments
 (0)