Skip to content

Commit dc28d04

Browse files
committed
Add first tests
Signed-off-by: Nicolai Buchwitz <nb@tipi-net.de>
1 parent e955603 commit dc28d04

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

tests/helper.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import sys
3+
4+
import pytest
5+
import pytest_subprocess
6+
7+
currentdir = os.path.dirname(os.path.realpath(__file__))
8+
parentdir = os.path.dirname(currentdir)
9+
sys.path.append(parentdir)
10+
11+
from uhubctl import *
12+
13+
class MockHub(Hub):
14+
def __init__(self, path: str, num_ports: int = 5) -> None:
15+
self.path = path
16+
self.num_ports = num_ports
17+
self.status = []
18+
19+
# initialize status
20+
for _ in range(self.num_ports):
21+
self.status.append(True)
22+
23+
super().__init__(path, False)
24+
25+
def __stdout(self, prefix: str = "Current", port_filter: int = None):
26+
stdout = [
27+
f"{prefix} status for hub {self.path} [0424:9512, USB 2.00, {self.num_ports} ports, ppps]".encode()]
28+
29+
for idx in range(self.num_ports):
30+
if port_filter is not None and idx+1 != int(port_filter):
31+
continue
32+
33+
stdout.append(f" Port {idx+1}: 0100 {self.__power_status(idx)}".encode())
34+
35+
return stdout
36+
37+
def __status(self, status: bool):
38+
if status:
39+
return "power"
40+
else:
41+
return "off"
42+
43+
def __power_status(self, port_number: int):
44+
assert port_number <= self.num_ports
45+
46+
return self.__status(self.status[port_number])
47+
48+
def cmd(self, fp: pytest_subprocess.FakeProcess, port_number: int = None, new_status: bool = None) -> None:
49+
if port_number is not None:
50+
assert port_number <= self.num_ports
51+
52+
cmd = ["uhubctl", "-N", "-l", str(self.path)]
53+
stdout = self.__stdout(port_filter=port_number)
54+
55+
if port_number is not None:
56+
cmd += [ "-p", str(port_number)]
57+
58+
if new_status is not None:
59+
self.status[port_number] = new_status
60+
61+
cmd.append("-a")
62+
cmd.append(self.__status(new_status))
63+
64+
stdout.append("Sent power on request".encode())
65+
stdout += self.__stdout("New", port_filter=port_number)
66+
67+
fp.register(cmd, stdout=stdout)
68+
69+
@pytest.fixture
70+
def mock_hub():
71+
path = "1-2"
72+
num_ports = 5
73+
hub = MockHub(path, num_ports)
74+
75+
return hub

tests/test_hub.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
import pytest_subprocess
3+
4+
import helper
5+
from helper import MockHub, mock_hub
6+
from uhubctl import Hub
7+
8+
def test_empty_hub():
9+
hub_path = "1-2"
10+
hub = Hub(hub_path, enumerate_ports=False)
11+
12+
assert hub.path == hub_path
13+
assert hub.ports == []
14+
15+
def test_multiple_manual_ports():
16+
hub_path = "1-2"
17+
hub = Hub(hub_path, enumerate_ports=False)
18+
19+
hub.add_port(1)
20+
21+
assert len(hub.ports) == 1
22+
23+
hub.add_ports(2,10)
24+
25+
assert len(hub.ports) == 10
26+
27+
28+
29+
def test_port_enumeration(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
30+
mock_hub.cmd(fp)
31+
mock_hub.discover_ports()
32+
33+
assert len(mock_hub.ports) == 5

tests/test_port.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
import pytest_subprocess
3+
4+
import helper
5+
from helper import mock_hub, MockHub
6+
from uhubctl import Hub, Port
7+
8+
9+
def test_manual_port_creation():
10+
port_number = 1
11+
hub_path = "1"
12+
13+
hub = Hub(hub_path, enumerate_ports=False)
14+
port = Port(hub, port_number)
15+
16+
assert port.port_number == port_number
17+
18+
19+
def test_port_status(mock_hub: MockHub, fp: pytest_subprocess.FakeProcess):
20+
for port in mock_hub.ports:
21+
mock_hub.cmd(fp, port.port_number)
22+
assert port.status is True
23+
24+
mock_hub.cmd(fp, port.port_number, False)
25+
port.status = False
26+
27+
mock_hub.cmd(fp, port.port_number)
28+
assert port.status is False

tests/test_uhubctl_binary.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
import helper
4+
5+
6+
def test_uhubctl_binary_invalid():
7+
import uhubctl
8+
uhubctl.utils.UHUBCTL_BINARY = "invalid-uhubctl"
9+
10+
with pytest.raises(Exception):
11+
uhubctl.discover_hubs()
12+
13+
14+
def test_uhubctl_binary_echo():
15+
import uhubctl
16+
uhubctl.utils.UHUBCTL_BINARY = "/bin/echo"
17+
18+
assert uhubctl.discover_hubs() == []

0 commit comments

Comments
 (0)