File: conftest.py

package info (click to toggle)
pydantic-settings 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,180 kB
  • sloc: python: 11,040; makefile: 33
file content (124 lines) | stat: -rw-r--r-- 2,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from __future__ import annotations

import os
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
    from collections.abc import Iterator


class SetEnv:
    def __init__(self):
        self.envars = set()

    def set(self, name, value):
        self.envars.add(name)
        os.environ[name] = value

    def pop(self, name):
        self.envars.remove(name)
        os.environ.pop(name)

    def clear(self):
        for n in self.envars:
            os.environ.pop(n)


class Dir:
    def __init__(self, basedir: Path) -> None:
        self.basedir = basedir

    def write(self, files: dict[str, str]) -> None:
        for path, content in files.items():
            file_path = self.basedir / path
            file_path.parent.mkdir(parents=True, exist_ok=True)
            file_path.write_text(content)


@pytest.fixture
def tmp_files(tmp_path):
    yield Dir(tmp_path)


@pytest.fixture
def cd_tmp_path(tmp_path: Path) -> Iterator[Path]:
    """Change directory into the value of the ``tmp_path`` fixture.

    .. rubric:: Example
    .. code-block:: python

        from typing import TYPE_CHECKING

        if TYPE_CHECKING:
            from pathlib import Path


        def test_something(cd_tmp_path: Path) -> None:
            ...

    Returns:
        Value of the :fixture:`tmp_path` fixture (a :class:`~pathlib.Path` object).

    """
    prev_dir = Path.cwd()
    os.chdir(tmp_path)
    try:
        yield tmp_path
    finally:
        os.chdir(prev_dir)


@pytest.fixture
def env():
    setenv = SetEnv()

    yield setenv

    setenv.clear()


@pytest.fixture
def docs_test_env():
    setenv = SetEnv()

    # envs for basic usage example
    setenv.set('my_auth_key', 'xxx')
    setenv.set('my_api_key', 'xxx')

    # envs for parsing environment variable values example
    setenv.set('V0', '0')
    setenv.set('SUB_MODEL', '{"v1": "json-1", "v2": "json-2"}')
    setenv.set('SUB_MODEL__V2', 'nested-2')
    setenv.set('SUB_MODEL__V3', '3')
    setenv.set('SUB_MODEL__DEEP__V4', 'v4')

    # envs for parsing environment variable values example with env_nested_max_split=1
    setenv.set('GENERATION_LLM_PROVIDER', 'anthropic')
    setenv.set('GENERATION_LLM_API_KEY', 'your-api-key')
    setenv.set('GENERATION_LLM_API_VERSION', '2024-03-15')

    # Variables from index.md that need to be cleaned up
    setenv.set('FooAlias', '')
    setenv.set('BAR', '')
    setenv.set('TARGET_BAR', '')
    setenv.set('TARGET_ALL_FooAlias', '')
    setenv.set('TARGET_ALIAS_FooAliase', '')

    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()