Skip to content

Commit 1d4f683

Browse files
authored
Add System tests (#32)
* first pass of system test (not working) * fully working "attach" system test * linting * add a detach system test * split up the system test file * add CLI system tests * add CLI system tests * add more CLI system tests
1 parent 2909ac1 commit 1d4f683

File tree

7 files changed

+957
-3
lines changed

7 files changed

+957
-3
lines changed

src/usb_remote/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
detach_command,
1414
find_command,
1515
)
16-
from .config import SERVER_PORT, get_timeout
16+
from .config import get_server_port, get_timeout
1717
from .port import Port
1818
from .usbdevice import DeviceNotFoundError, MultipleDevicesError, UsbDevice
1919
from .utility import run_command
2020

2121
logger = logging.getLogger(__name__)
2222

23+
# Will be fetched from config when needed
24+
SERVER_PORT = None
2325
# Default connection timeout in seconds
2426
DEFAULT_TIMEOUT = 2.0
2527

2628

2729
def send_request(
2830
request: ListRequest | DeviceRequest,
2931
server_host: str = "localhost",
30-
server_port: int = SERVER_PORT,
32+
server_port: int | None = None,
3133
timeout: float | None = None,
3234
) -> ListResponse | DeviceResponse:
3335
"""
@@ -49,6 +51,9 @@ def send_request(
4951
TimeoutError: If connection or receive times out
5052
OSError: If connection fails
5153
"""
54+
if server_port is None:
55+
server_port = get_server_port()
56+
5257
logger.debug(f"Connecting to server at {server_host}:{server_port}")
5358

5459
if timeout is None:

src/usb_remote/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class UsbRemoteConfig(BaseModel):
2222
servers: list[str] = Field(default_factory=list)
2323
server_ranges: list[str] = Field(default_factory=list)
2424
timeout: float = Field(default=DEFAULT_TIMEOUT, gt=0)
25+
server_port: int = Field(default=SERVER_PORT)
2526
model_config = ConfigDict(extra="forbid")
2627

2728
@classmethod
@@ -183,6 +184,17 @@ def get_timeout() -> float:
183184
return config.timeout
184185

185186

187+
def get_server_port() -> int:
188+
"""
189+
Read server port from config file.
190+
191+
Returns:
192+
Server port number. Returns default if not configured.
193+
"""
194+
config = get_config()
195+
return config.server_port
196+
197+
186198
def save_servers(servers: list[str]) -> None:
187199
"""
188200
Save list of server addresses to config file.

src/usb_remote/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23
import socket
34
import threading
45
from typing import Literal
@@ -29,8 +30,11 @@
2930

3031

3132
class CommandServer:
32-
def __init__(self, host: str = "0.0.0.0", port: int = SERVER_PORT):
33+
def __init__(self, host: str = "0.0.0.0", port: int | None = None):
3334
self.host = host
35+
# Allow server port to be overridden via environment variable
36+
if port is None:
37+
port = int(os.environ.get("USB_REMOTE_SERVER_PORT", SERVER_PORT))
3438
self.port = port
3539
self.server_socket = None
3640
self.running = False

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from usb_remote.config import UsbRemoteConfig
1010
from usb_remote.usbdevice import UsbDevice
1111

12+
# Load system integration test fixtures from conftest_system.py
13+
pytest_plugins = ["tests.conftest_system"]
14+
1215

1316
@pytest.fixture
1417
def mock_config():

0 commit comments

Comments
 (0)