Skip to content

Commit 1c9cad7

Browse files
committed
Remove leading slash from topics
1 parent 90e5359 commit 1c9cad7

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

mqterm/terminal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
def format_topic(*parts):
99
"""Create a slash-delimited MQTT topic from a list of strings."""
10-
return "/" + "/".join(part.strip("/") for part in parts if part)
10+
return "/".join(part.strip("/") for part in parts if part)
1111

1212

1313
def format_properties(client_id, seq):

tests/e2e/e2e_file_ops.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
control_client = MQTTClient(control_config, logger=control_logger)
4040

4141
# Set up the terminal
42-
term = MqttTerminal(device_client, topic_prefix="/test")
42+
term = MqttTerminal(device_client, topic_prefix="test")
4343

4444

4545
async def send_file(buffer: BytesIO):
@@ -48,7 +48,7 @@ async def send_file(buffer: BytesIO):
4848
seq = 0
4949
props = format_properties("tty0", seq)
5050
await control_client.publish(
51-
"/test/tty/in", "cp test.txt".encode("utf-8"), properties=props
51+
"test/tty/in", "cp test.txt".encode("utf-8"), properties=props
5252
)
5353

5454
# Send the file in 4-byte chunks; close when done
@@ -62,7 +62,7 @@ async def send_file(buffer: BytesIO):
6262
seq = -1
6363
props = format_properties("tty0", seq)
6464
logger.debug(f"Sending chunk {seq} of size {len(chunk)}: {chunk!r}")
65-
await control_client.publish("/test/tty/in", chunk, properties=props)
65+
await control_client.publish("test/tty/in", chunk, properties=props)
6666
if seq == -1:
6767
break
6868

@@ -84,7 +84,7 @@ async def get_file(buffer: BytesIO):
8484
seq = 0
8585
props = format_properties("tty0", seq)
8686
await control_client.publish(
87-
"/test/tty/in", "cat test.txt".encode(), properties=props
87+
"test/tty/in", "cat test.txt".encode(), properties=props
8888
)
8989

9090
# Wait until the received buffer gets populated with the response
@@ -107,7 +107,7 @@ async def device_handler():
107107
# Handler for control messages that logs and stores them
108108
async def control_handler(buffer):
109109
async for topic, payload, _retained, properties in control_client.queue:
110-
if topic == "/test/tty/err":
110+
if topic == "test/tty/err":
111111
logger.error(f"Control received error: {payload.decode('utf-8')}")
112112
else:
113113
buffer.write(payload) # Don't decode yet
@@ -118,8 +118,8 @@ async def control_handler(buffer):
118118
async def main():
119119
# Connect all clients and the terminal
120120
await control_client.connect(True)
121-
await control_client.subscribe("/test/tty/out")
122-
await control_client.subscribe("/test/tty/err")
121+
await control_client.subscribe("test/tty/out")
122+
await control_client.subscribe("test/tty/err")
123123
await device_client.connect(True)
124124
await term.connect()
125125

tests/test_terminal.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from unittest import TestCase, skip
44

55
from mqterm.jobs import Job, SequentialJob
6-
from mqterm.terminal import MqttTerminal, format_properties
6+
from mqterm.terminal import MqttTerminal, format_properties, format_topic
77
from tests.utils import AsyncMock, Mock, call
88

99

@@ -28,10 +28,19 @@ async def update(self, payload, seq):
2828
def output(self):
2929
return BytesIO(self.contents.encode("utf-8"))
3030

31+
class TestFormatTopic(TestCase):
32+
def test_format_topic(self):
33+
"""Test the format_topic function."""
34+
self.assertEqual(format_topic("a", "b", "c"), "a/b/c")
35+
self.assertEqual(format_topic("a", None, "c"), "a/c")
36+
self.assertEqual(format_topic("", "b", None), "b")
37+
self.assertEqual(format_topic("a/", "b/"), "a/b")
38+
self.assertEqual(format_topic(), "")
39+
3140

3241
class TestMqttTerminal(TestCase):
3342
def setUp(self):
34-
self.in_topic = b"/tty/in"
43+
self.in_topic = b"tty/in"
3544
self.mqtt_client = Mock()
3645
self.mqtt_client.publish = Mock()
3746
self.term = MqttTerminal(self.mqtt_client)
@@ -67,7 +76,7 @@ def test_handle_msg_bad_seq(self):
6776
def test_handle_msg_wrong_topic(self):
6877
"""MqttTerminal should ignore messages on wrong topic"""
6978
payload = "get_file file.txt"
70-
topic = b"/wrong/topic"
79+
topic = b"wrong/topic"
7180
self.term.update_job = AsyncMock()
7281
asyncio.run(
7382
self.term.handle_msg(topic, payload.encode("utf-8"), format_properties("localhost", -1))

0 commit comments

Comments
 (0)