Files
esphome/tests/component_tests/conftest.py
T
Clyde Stubbs 4648804db6
CI / Create common environment (push) Has been cancelled
CI / Check ruff (push) Has been cancelled
CI / Check flake8 (push) Has been cancelled
CI / Check pylint (push) Has been cancelled
CI / Check pyupgrade (push) Has been cancelled
CI / Run script/ci-custom (push) Has been cancelled
CI / Run pytest (macOS-latest, 3.11) (push) Has been cancelled
CI / Run pytest (ubuntu-latest, 3.10) (push) Has been cancelled
CI / Run pytest (ubuntu-latest, 3.11) (push) Has been cancelled
CI / Run pytest (ubuntu-latest, 3.12) (push) Has been cancelled
CI / Run pytest (ubuntu-latest, 3.13) (push) Has been cancelled
CI / Run pytest (windows-latest, 3.11) (push) Has been cancelled
CI / Run integration tests (push) Has been cancelled
CI / Check clang-format (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 IDF (push) Has been cancelled
CI / Run script/clang-tidy for ESP8266 (push) Has been cancelled
CI / Run script/clang-tidy for ZEPHYR (push) Has been cancelled
CI / list-components (push) Has been cancelled
CI / Component test ${{ matrix.file }} (push) Has been cancelled
CI / Split components for testing into 20 groups maximum (push) Has been cancelled
CI / Test split components (push) Has been cancelled
CI / CI Status (push) Has been cancelled
YAML lint / yamllint (push) Has been cancelled
Synchronise Device Classes from Home Assistant / Sync Device Classes (push) Has been cancelled
[image] Add byte order option and unit tests (#9326)
2025-07-08 02:28:00 +00:00

74 lines
2.4 KiB
Python

"""Fixtures for component tests."""
from __future__ import annotations
from collections.abc import Callable, Generator
from pathlib import Path
import sys
import pytest
# Add package root to python path
here = Path(__file__).parent
package_root = here.parent.parent
sys.path.insert(0, package_root.as_posix())
from esphome.__main__ import generate_cpp_contents # noqa: E402
from esphome.config import read_config # noqa: E402
from esphome.core import CORE # noqa: E402
@pytest.fixture(autouse=True)
def config_path(request: pytest.FixtureRequest) -> Generator[None]:
"""Set CORE.config_path to the component's config directory and reset it after the test."""
original_path = CORE.config_path
config_dir = Path(request.fspath).parent / "config"
# Check if config directory exists, if not use parent directory
if config_dir.exists():
# Set config_path to a dummy yaml file in the config directory
# This ensures CORE.config_dir points to the config directory
CORE.config_path = str(config_dir / "dummy.yaml")
else:
CORE.config_path = str(Path(request.fspath).parent / "dummy.yaml")
yield
CORE.config_path = original_path
@pytest.fixture
def component_fixture_path(request: pytest.FixtureRequest) -> Callable[[str], Path]:
"""Return a function to get absolute paths relative to the component's fixtures directory."""
def _get_path(file_name: str) -> Path:
"""Get the absolute path of a file relative to the component's fixtures directory."""
return (Path(request.fspath).parent / "fixtures" / file_name).absolute()
return _get_path
@pytest.fixture
def component_config_path(request: pytest.FixtureRequest) -> Callable[[str], Path]:
"""Return a function to get absolute paths relative to the component's config directory."""
def _get_path(file_name: str) -> Path:
"""Get the absolute path of a file relative to the component's config directory."""
return (Path(request.fspath).parent / "config" / file_name).absolute()
return _get_path
@pytest.fixture
def generate_main() -> Generator[Callable[[str | Path], str]]:
"""Generates the C++ main.cpp file and returns it in string form."""
def generator(path: str | Path) -> str:
CORE.config_path = str(path)
CORE.config = read_config({})
generate_cpp_contents(CORE.config)
return CORE.cpp_main_section
yield generator
CORE.reset()