Skip to content

Commit

Permalink
[create-pull-request] automated change (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: MatthewFlamm <MatthewFlamm@users.noreply.github.com>
  • Loading branch information
MatthewFlamm and MatthewFlamm authored Dec 1, 2020
1 parent 7b2c2ea commit eae4a4a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pytest-homeassistant-custom-component

![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=0.118.0.dev0&labelColor=blue)
![HA core version](https://img.shields.io/static/v1?label=HA+core+version&message=0.119.0.dev0&labelColor=blue)

Package to automatically extract testing plugins from Home Assistant for custom component testing.
Automatic extraction of testing plugins and helpers.
Expand Down
6 changes: 6 additions & 0 deletions pytest_homeassistant_custom_component/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import json
import logging
import os
import pathlib
import threading
import time
import uuid
Expand Down Expand Up @@ -198,6 +199,8 @@ def async_create_task(coroutine):
hass.async_add_executor_job = async_add_executor_job
hass.async_create_task = async_create_task

hass.data[loader.DATA_CUSTOM_COMPONENTS] = {}

hass.config.location_name = "test home"
hass.config.config_dir = get_test_config_dir()
hass.config.latitude = 32.87336
Expand Down Expand Up @@ -708,6 +711,9 @@ def patch_yaml_files(files_dict, endswith=True):
def mock_open_f(fname, **_):
"""Mock open() in the yaml module, used by load_yaml."""
# Return the mocked file on full match
if isinstance(fname, pathlib.Path):
fname = str(fname)

if fname in files_dict:
_LOGGER.debug("patch_yaml_files match %s", fname)
res = StringIO(files_dict[fname])
Expand Down
2 changes: 1 addition & 1 deletion pytest_homeassistant_custom_component/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This file is originally from homeassistant/core and modified by pytest-homeassistant-custom-component.
"""
MAJOR_VERSION = 0
MINOR_VERSION = 118
MINOR_VERSION = 119
PATCH_VERSION = "0.dev0"
__short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}"
__version__ = f"{__short_version__}.{PATCH_VERSION}"
27 changes: 22 additions & 5 deletions pytest_homeassistant_custom_component/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import threading

from aiohttp.test_utils import make_mocked_request
import multidict
import pytest
import requests_mock as _requests_mock

Expand All @@ -26,11 +27,11 @@
from homeassistant.components.websocket_api.http import URL
from homeassistant.const import ATTR_NOW, EVENT_TIME_CHANGED
from homeassistant.exceptions import ServiceNotFound
from homeassistant.helpers import event
from homeassistant.helpers import config_entry_oauth2_flow, event
from homeassistant.setup import async_setup_component
from homeassistant.util import location

from .async_mock import MagicMock, Mock, patch
from .async_mock import MagicMock, patch
from .ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS

pytest.register_assert_rewrite(".common")
Expand Down Expand Up @@ -281,19 +282,29 @@ async def auth_client():


@pytest.fixture
def current_request(hass):
def current_request():
"""Mock current request."""
with patch("homeassistant.helpers.network.current_request") as mock_request_context:
with patch("homeassistant.components.http.current_request") as mock_request_context:
mocked_request = make_mocked_request(
"GET",
"/some/request",
headers={"Host": "example.com"},
sslcontext=ssl.SSLContext(ssl.PROTOCOL_TLS),
)
mock_request_context.get = Mock(return_value=mocked_request)
mock_request_context.get.return_value = mocked_request
yield mock_request_context


@pytest.fixture
def current_request_with_host(current_request):
"""Mock current request with a host header."""
new_headers = multidict.CIMultiDict(current_request.get.return_value.headers)
new_headers[config_entry_oauth2_flow.HEADER_FRONTEND_BASE] = "https://example.com"
current_request.get.return_value = current_request.get.return_value.clone(
headers=new_headers
)


@pytest.fixture
def hass_ws_client(aiohttp_client, hass_access_token, hass):
"""Websocket client fixture connected to websocket server."""
Expand Down Expand Up @@ -530,3 +541,9 @@ def pattern_time_change_listener(ev) -> None:
async_track_utc_time_change,
):
yield


@pytest.fixture
def enable_custom_integrations(hass):
"""Enable custom integrations defined in the test dir."""
hass.data.pop(loader.DATA_CUSTOM_COMPONENTS)
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ coverage==5.3
jsonpickle==1.4.1
mock-open==1.4.0
mypy==0.790
pre-commit==2.8.2
pre-commit==2.9.2
pylint==2.6.0
astroid==2.4.2
pipdeptree==1.0.0
Expand Down

0 comments on commit eae4a4a

Please sign in to comment.