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]):
"""Activate Google Pub/Sub component."""
config = yaml_config[DOMAIN]
project_id = config[CONF_PROJECT_ID]
topic_name = config[CONF_TOPIC_NAME]
if hass.config.config_dir:
service_principal_path = os.path.join(
hass.config.config_dir, config[CONF_SERVICE_PRINCIPAL]
)
else:
service_principal_path = config[CONF_SERVICE_PRINCIPAL]
service_principal_path = hass.config.path(config[CONF_SERVICE_PRINCIPAL])
if not os.path.isfile(service_principal_path):
_LOGGER.error("Path to credentials file cannot be found")

View File

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