Skip to content

Commit

Permalink
Make tests more robust to the running environment (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberTailor authored Nov 4, 2024
1 parent b2c979c commit 0922bc1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
15 changes: 13 additions & 2 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 5 additions & 0 deletions tests/test_source_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0922bc1

Please sign in to comment.