diff options
| -rw-r--r-- | lirc/connection/default_connection.py | 33 | ||||
| -rw-r--r-- | lirc/exceptions.py | 7 | ||||
| -rw-r--r-- | tests/test_default_connection.py | 62 |
3 files changed, 96 insertions, 6 deletions
diff --git a/lirc/connection/default_connection.py b/lirc/connection/default_connection.py index e607e0b..0ac2a12 100644 --- a/lirc/connection/default_connection.py +++ b/lirc/connection/default_connection.py @@ -2,6 +2,8 @@ import platform import socket from typing import Tuple, Union +from lirc.exceptions import UnsupportedOperatingSystemError + class DefaultConnection: def __init__(self): @@ -9,7 +11,7 @@ class DefaultConnection: This class provides our operating specific default connection parameters. """ - self.__operating_system = platform.system().capitalize() + self.__operating_system = platform.system() @property def socket(self) -> socket.socket: @@ -19,6 +21,10 @@ class DefaultConnection: Returns: A socket.socket setup correctly for the current OS. + + Raises: + UnsupportedOperatingSystemError: If the OS is not Linux, Windows, + or Darwin. """ if self.__operating_system == "Linux" or self.__operating_system == "Darwin": return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) @@ -26,6 +32,11 @@ class DefaultConnection: if self.__operating_system == "Windows": return socket.socket(socket.AF_INET, socket.SOCK_STREAM) + raise UnsupportedOperatingSystemError( + f"{self.__operating_system} is not supported for a default OS " + "socket to lircd." + ) + @property def address(self) -> Union[str, Tuple[str, int]]: """ @@ -36,9 +47,19 @@ class DefaultConnection: A str path to the lircd socket on the file system if on Linux or MacOS; a Tuple of the address and port if on Windows. + + Raises: + UnsupportedOperatingSystemError: If the OS is not Linux, Windows, + or Darwin. """ - return { - "Linux": "/var/run/lirc/lircd", - "Windows": ("localhost", 8765), - "Darwin": "/opt/run/var/run/lirc/lircd", - }[self.__operating_system] + try: + return { + "Linux": "/var/run/lirc/lircd", + "Windows": ("localhost", 8765), + "Darwin": "/opt/run/var/run/lirc/lircd", + }[self.__operating_system] + except KeyError: + raise UnsupportedOperatingSystemError( + f"{self.__operating_system} is not supported for a default OS " + "address to lircd." + ) diff --git a/lirc/exceptions.py b/lirc/exceptions.py index f16b61e..a689b42 100644 --- a/lirc/exceptions.py +++ b/lirc/exceptions.py @@ -24,3 +24,10 @@ class LircdCommandFailureError(LircError): For when we send a command to the LIRC server and that command fails to send, for whatever reason. """ + + +class UnsupportedOperatingSystemError(LircError): + """ + Raised when there is an attempt to use an unsupported + operating system. + """ diff --git a/tests/test_default_connection.py b/tests/test_default_connection.py new file mode 100644 index 0000000..bc4fa09 --- /dev/null +++ b/tests/test_default_connection.py @@ -0,0 +1,62 @@ +from unittest.mock import patch +import socket + +from lirc.connection.default_connection import DefaultConnection +from lirc.exceptions import UnsupportedOperatingSystemError + +import pytest + + +@pytest.mark.parametrize("os, expected_socket, expected_address", [ + ( + "Windows", + socket.socket(socket.AF_INET, socket.SOCK_STREAM), + ("localhost", 8765) + ), + ( + "Linux", + socket.socket(socket.AF_UNIX, socket.SOCK_STREAM), + "/var/run/lirc/lircd" + ), + ( + "Darwin", + socket.socket(socket.AF_UNIX, socket.SOCK_STREAM), + "/opt/run/var/run/lirc/lircd" + ) +]) +@patch("platform.system") +def test_default_connection_has_correct_os_specific_socket_and_address( + patched_system, os, expected_socket, expected_address +): + """ + lirc.connection.default_connection.DefaultConnection + + Ensure that the DefaultConnection retrieves the correct socket + and address for each support operating system. + """ + patched_system.return_value = os + + default_conn = DefaultConnection() # SUT + + assert default_conn.socket.family == expected_socket.family + assert default_conn.socket.type == expected_socket.type + assert default_conn.address == expected_address + + +@pytest.mark.parametrize("prop", ["socket", "address"]) +@patch("platform.system") +def test_default_connection_raises_error_on_unsupported_os(patched_system, prop): + """ + lirc.connection.default_connection.DefaultConnection.socket + lirc.connection.default_connection.DefaultConnection.address + + Ensure that trying to retrieve the socket or address on an unsupported + OS raises an UnsupportedOperatingSystemError. + """ + patched_system.return_value = "FreeBSD" + + with pytest.raises(UnsupportedOperatingSystemError) as error: + default_conn = DefaultConnection() + getattr(default_conn, prop) # SUT + + assert "FreeBSD is not supported" in str(error) |