|
| 1 | +"""This module contains tests for configuring loggers for the Elgato Stream Deck plugins. |
| 2 | +
|
| 3 | +The tests validate that the logger configurations correctly create log files for both local plugin-specific logging and |
| 4 | +centralized Stream Deck logging. |
| 5 | +""" |
| 6 | +import logging |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import platformdirs |
| 10 | +import pytest |
| 11 | +from streamdeck.utils.logging import configure_local_logger, configure_streamdeck_logger |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def fake_plugin_local_log_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: |
| 16 | + """Fixture to set up a fake local log directory for plugin-specific logging. |
| 17 | +
|
| 18 | + This fixture uses `monkeypatch` to replace the `platformdirs.user_data_path` function, redirecting it to use a |
| 19 | + temporary path for testing purposes. |
| 20 | +
|
| 21 | + Args: |
| 22 | + monkeypatch (pytest.MonkeyPatch): The pytest monkeypatch object for modifying module attributes. |
| 23 | + tmp_path (Path): A temporary path provided by pytest for file operations. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + Path: The temporary path used for local plugin logging. |
| 27 | + """ |
| 28 | + monkeypatch.setattr( |
| 29 | + platformdirs, "user_data_path", value=(lambda plugin_uuid: tmp_path / plugin_uuid) |
| 30 | + ) |
| 31 | + return tmp_path |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def fake_streamdeck_log_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: |
| 36 | + """Fixture to set up a fake Stream Deck log directory for centralized logging. |
| 37 | +
|
| 38 | + This fixture uses `monkeypatch` to replace the `platformdirs.user_log_path` function, redirecting it to use a |
| 39 | + temporary path for testing purposes. |
| 40 | +
|
| 41 | + Args: |
| 42 | + monkeypatch (pytest.MonkeyPatch): The pytest monkeypatch object for modifying module attributes. |
| 43 | + tmp_path (Path): A temporary path provided by pytest for file operations. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + Path: The temporary path used for Stream Deck logging. |
| 47 | + """ |
| 48 | + monkeypatch.setattr(platformdirs, "user_log_path", value=(lambda appname: tmp_path / appname)) |
| 49 | + return tmp_path |
| 50 | + |
| 51 | + |
| 52 | +def test_local_logger(fake_plugin_local_log_dir: Path): |
| 53 | + """Test the configuration of a local plugin-specific logger. |
| 54 | +
|
| 55 | + This test verifies that a logger configured for a specific plugin writes logs to the correct local directory. |
| 56 | +
|
| 57 | + Args: |
| 58 | + fake_plugin_local_log_dir (Path): The fake local log directory path provided by the fixture. |
| 59 | + """ |
| 60 | + FAKE_LOGGER_NAME = "TEST-PLUGIN-FILE" |
| 61 | + FAKE_PLUGIN_UUID = "com.test.spotifier" |
| 62 | + LOG_STATEMENT = "Test log." |
| 63 | + |
| 64 | + configure_local_logger(name=FAKE_LOGGER_NAME, plugin_uuid=FAKE_PLUGIN_UUID) |
| 65 | + logger = logging.getLogger(FAKE_LOGGER_NAME) |
| 66 | + |
| 67 | + logger.info(LOG_STATEMENT) |
| 68 | + |
| 69 | + log_file = ( |
| 70 | + fake_plugin_local_log_dir |
| 71 | + / "com.elgato.StreamDeck/Plugins" |
| 72 | + / FAKE_PLUGIN_UUID |
| 73 | + / f"logs/{FAKE_LOGGER_NAME}.log" |
| 74 | + ) |
| 75 | + |
| 76 | + assert log_file.exists() |
| 77 | + |
| 78 | + with log_file.open("r") as f: |
| 79 | + actual_log_file_output = f.read() |
| 80 | + # We don't want to make too many assumptions here about the format of the log output. |
| 81 | + assert "INFO" in actual_log_file_output |
| 82 | + assert LOG_STATEMENT in actual_log_file_output |
| 83 | + # Probably don't need to assert that the logger's name is in the log. |
| 84 | + # assert fake_name in actual_log_file_output |
| 85 | + |
| 86 | + |
| 87 | +def test_streamdeck_logger(fake_streamdeck_log_dir: Path): |
| 88 | + """Test the configuration of a centralized Stream Deck logger. |
| 89 | +
|
| 90 | + This test verifies that a logger configured for the Stream Deck writes logs to the correct centralized directory. |
| 91 | +
|
| 92 | + Args: |
| 93 | + fake_streamdeck_log_dir (Path): The fake Stream Deck log directory path provided by the fixture. |
| 94 | + """ |
| 95 | + FAKE_LOGGER_NAME = "my_test_name" |
| 96 | + FAKE_PLUGIN_UUID = "com.test.plugin" |
| 97 | + LOG_STATEMENT = "Testing 123..." |
| 98 | + |
| 99 | + configure_streamdeck_logger(name=FAKE_LOGGER_NAME, plugin_uuid=FAKE_PLUGIN_UUID) |
| 100 | + logger = logging.getLogger(FAKE_LOGGER_NAME) |
| 101 | + |
| 102 | + logger.info(LOG_STATEMENT) |
| 103 | + |
| 104 | + log_file = fake_streamdeck_log_dir / "ElgatoStreamDeck" / f"{FAKE_PLUGIN_UUID}.log" |
| 105 | + |
| 106 | + assert log_file.exists() |
| 107 | + with log_file.open("r") as f: |
| 108 | + actual_log_file_output = f.read() |
| 109 | + # We don't want to make too many assumptions here about the format of the log output. |
| 110 | + assert "INFO" in actual_log_file_output |
| 111 | + assert LOG_STATEMENT in actual_log_file_output |
| 112 | + # Probably don't need to assert that the logger's name is in the log. |
| 113 | + assert FAKE_LOGGER_NAME in actual_log_file_output |
0 commit comments