| 1 |
import logging |
| 2 |
import os |
| 3 |
from pathlib import Path |
| 4 |
from typing import Generator |
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture(scope="session", autouse=True) |
| 9 |
def set_test_config() -> Generator: |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
orig_environ = os.environ.copy() |
| 14 |
os.environ["DUCT_SAMPLE_INTERVAL"] = "0.01" |
| 15 |
os.environ["DUCT_REPORT_INTERVAL"] = "0.1" |
| 16 |
yield |
| 17 |
|
| 18 |
|
| 19 |
for k, v in os.environ.items(): |
| 20 |
if k in orig_environ: |
| 21 |
os.environ[k] = v |
| 22 |
else: |
| 23 |
del os.environ[k] |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(autouse=True) |
| 27 |
def reset_logger_state() -> Generator: |
| 28 |
"""Automatically reset logger state after each test. |
| 29 |
|
| 30 |
The execute() function can disable the logger globally when quiet=True |
| 31 |
or log_level="NONE", which affects subsequent tests. This fixture ensures |
| 32 |
the logger is reset to default state after each test. |
| 33 |
""" |
| 34 |
import con_duct._duct_main as main_module |
| 35 |
|
| 36 |
yield |
| 37 |
|
| 38 |
main_module.lgr.disabled = False |
| 39 |
main_module.lgr.setLevel(logging.INFO) |
| 40 |
|
| 41 |
|
| 42 |
@pytest.fixture |
| 43 |
def temp_output_dir(tmp_path: Path) -> str: |
| 44 |
|
| 45 |
|
| 46 |
return str(tmp_path) + os.sep |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture |
| 50 |
def clean_env(monkeypatch: pytest.MonkeyPatch) -> pytest.MonkeyPatch: |
| 51 |
"""Provide a clean environment for testing .env file loading. |
| 52 |
|
| 53 |
Clears all DUCT_* and TEST_* environment variables to avoid test pollution. |
| 54 |
Returns the monkeypatch instance for setting new env vars in tests. |
| 55 |
""" |
| 56 |
for key in list(os.environ.keys()): |
| 57 |
if key.startswith("DUCT_") or key.startswith("TEST_"): |
| 58 |
monkeypatch.delenv(key, raising=False) |
| 59 |
return monkeypatch |