Use path helper method for principal file in google_pubsub (#54744)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Franck Nijhof 2021-08-17 19:49:38 +02:00 committed by GitHub
parent 15feb430fc
commit 5b75c8254b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 20 deletions

View File

@ -41,16 +41,10 @@ CONFIG_SCHEMA = vol.Schema(
def setup(hass: HomeAssistant, yaml_config: dict[str, Any]): def setup(hass: HomeAssistant, yaml_config: dict[str, Any]):
"""Activate Google Pub/Sub component.""" """Activate Google Pub/Sub component."""
config = yaml_config[DOMAIN] config = yaml_config[DOMAIN]
project_id = config[CONF_PROJECT_ID] project_id = config[CONF_PROJECT_ID]
topic_name = config[CONF_TOPIC_NAME] topic_name = config[CONF_TOPIC_NAME]
if hass.config.config_dir: service_principal_path = hass.config.path(config[CONF_SERVICE_PRINCIPAL])
service_principal_path = os.path.join(
hass.config.config_dir, config[CONF_SERVICE_PRINCIPAL]
)
else:
service_principal_path = config[CONF_SERVICE_PRINCIPAL]
if not os.path.isfile(service_principal_path): if not os.path.isfile(service_principal_path):
_LOGGER.error("Path to credentials file cannot be found") _LOGGER.error("Path to credentials file cannot be found")

View File

@ -1,6 +1,7 @@
"""The tests for the Google Pub/Sub component.""" """The tests for the Google Pub/Sub component."""
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
import os
import unittest.mock as mock import unittest.mock as mock
import pytest import pytest
@ -51,13 +52,12 @@ def mock_client_fixture():
yield client yield client
@pytest.fixture(autouse=True, name="mock_os") @pytest.fixture(autouse=True, name="mock_is_file")
def mock_os_fixture(): def mock_is_file_fixture():
"""Mock the OS cli.""" """Mock os.path.isfile."""
with mock.patch(f"{GOOGLE_PUBSUB_PATH}.os") as os_cli: with mock.patch(f"{GOOGLE_PUBSUB_PATH}.os.path.isfile") as is_file:
os_cli.path = mock.MagicMock() is_file.return_value = True
setattr(os_cli.path, "join", mock.MagicMock(return_value="path")) yield is_file
yield os_cli
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@ -84,9 +84,9 @@ async def test_minimal_config(hass, mock_client):
assert hass.bus.listen.called assert hass.bus.listen.called
assert hass.bus.listen.call_args_list[0][0][0] == EVENT_STATE_CHANGED assert hass.bus.listen.call_args_list[0][0][0] == EVENT_STATE_CHANGED
assert mock_client.PublisherClient.from_service_account_json.call_count == 1 assert mock_client.PublisherClient.from_service_account_json.call_count == 1
assert ( assert mock_client.PublisherClient.from_service_account_json.call_args[0][
mock_client.PublisherClient.from_service_account_json.call_args[0][0] == "path" 0
) ] == os.path.join(hass.config.config_dir, "creds")
async def test_full_config(hass, mock_client): async def test_full_config(hass, mock_client):
@ -111,9 +111,9 @@ async def test_full_config(hass, mock_client):
assert hass.bus.listen.called assert hass.bus.listen.called
assert hass.bus.listen.call_args_list[0][0][0] == EVENT_STATE_CHANGED assert hass.bus.listen.call_args_list[0][0][0] == EVENT_STATE_CHANGED
assert mock_client.PublisherClient.from_service_account_json.call_count == 1 assert mock_client.PublisherClient.from_service_account_json.call_count == 1
assert ( assert mock_client.PublisherClient.from_service_account_json.call_args[0][
mock_client.PublisherClient.from_service_account_json.call_args[0][0] == "path" 0
) ] == os.path.join(hass.config.config_dir, "creds")
def make_event(entity_id): def make_event(entity_id):