Skip to content

Commit 4b14303

Browse files
committed
system independent path separators; pytest for windows fixes
1 parent a79e17a commit 4b14303

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

meshtastic/analysis/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import logging
5+
from pathlib import Path
56
from typing import cast, List
67

78
import dash_bootstrap_components as dbc # type: ignore[import-untyped]
@@ -190,7 +191,7 @@ def main():
190191
parser = create_argparser()
191192
args = parser.parse_args()
192193
if not args.slog:
193-
args.slog = f"{root_dir()}/latest"
194+
args.slog = str(Path(root_dir(), "latest"))
194195

195196
app = create_dash(slog_path=args.slog)
196197
port = 8051

meshtastic/slog/slog.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import atexit
44
import io
55
import logging
6-
import os
76
import re
87
import threading
98
import time
109
from dataclasses import dataclass
1110
from datetime import datetime
1211
from functools import reduce
12+
from pathlib import Path
1313
from typing import Optional, List, Tuple
1414

1515
import parse # type: ignore[import-untyped]
@@ -22,16 +22,17 @@
2222

2323
from .arrow import FeatherWriter
2424

25+
logger = logging.getLogger(__name__)
2526

2627
def root_dir() -> str:
2728
"""Return the root directory for slog files."""
2829

2930
app_name = "meshtastic"
3031
app_author = "meshtastic"
3132
app_dir = platformdirs.user_data_dir(app_name, app_author)
32-
dir_name = f"{app_dir}/slogs"
33-
os.makedirs(dir_name, exist_ok=True)
34-
return dir_name
33+
dir_name = Path(app_dir, "slogs")
34+
dir_name.mkdir(exist_ok=True)
35+
return str(dir_name)
3536

3637

3738
@dataclass(init=False)
@@ -197,7 +198,7 @@ def _onLogMessage(self, line: str) -> None:
197198
if m:
198199
src = m.group(1)
199200
args = m.group(2)
200-
logging.debug(f"SLog {src}, args: {args}")
201+
logger.debug(f"SLog {src}, args: {args}")
201202

202203
d = log_defs.get(src)
203204
if d:
@@ -219,9 +220,9 @@ def _onLogMessage(self, line: str) -> None:
219220
# If the last field is an empty string, remove it
220221
del di[last_field[0]]
221222
else:
222-
logging.warning(f"Failed to parse slog {line} with {d.format}")
223+
logger.warning(f"Failed to parse slog {line} with {d.format}")
223224
else:
224-
logging.warning(f"Unknown Structured Log: {line}")
225+
logger.warning(f"Unknown Structured Log: {line}")
225226

226227
# Store our structured log record
227228
if di or self.include_raw:
@@ -256,18 +257,22 @@ def __init__(
256257

257258
if not dir_name:
258259
app_dir = root_dir()
259-
dir_name = f"{app_dir}/{datetime.now().strftime('%Y%m%d-%H%M%S')}"
260-
os.makedirs(dir_name, exist_ok=True)
260+
dir_name = Path(app_dir, datetime.now().strftime('%Y%m%d-%H%M%S'))
261+
dir_name.mkdir(exist_ok=True)
261262

262263
# Also make a 'latest' directory that always points to the most recent logs
264+
latest_dir = Path(app_dir, "latest")
265+
latest_dir.unlink(missing_ok=True)
266+
263267
# symlink might fail on some platforms, if it does fail silently
264-
if os.path.exists(f"{app_dir}/latest"):
265-
os.unlink(f"{app_dir}/latest")
266-
os.symlink(dir_name, f"{app_dir}/latest", target_is_directory=True)
268+
try:
269+
latest_dir.symlink_to(dir_name, target_is_directory=True)
270+
except OSError as e:
271+
pass
267272

268273
self.dir_name = dir_name
269274

270-
logging.info(f"Writing slogs to {dir_name}")
275+
logger.info(f"Writing slogs to {dir_name}")
271276

272277
self.power_logger: Optional[PowerLogger] = (
273278
None
@@ -286,7 +291,7 @@ def close(self) -> None:
286291
"""Close the log set."""
287292

288293
if self.slog_logger:
289-
logging.info(f"Closing slogs in {self.dir_name}")
294+
logger.info(f"Closing slogs in {self.dir_name}")
290295
atexit.unregister(
291296
self.atexit_handler
292297
) # docs say it will silently ignore if not found

meshtastic/tests/test_main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,7 +2683,6 @@ def test_tunnel_no_args(capsys):
26832683
assert re.search(r"usage: ", err, re.MULTILINE)
26842684

26852685

2686-
@pytest.mark.skipif(sys.platform == "win32", reason="Linux is forced in test and no termios")
26872686
@pytest.mark.unit
26882687
@pytest.mark.usefixtures("reset_mt_config")
26892688
@patch("meshtastic.util.findPorts", return_value=[])
@@ -2707,7 +2706,6 @@ def test_tunnel_tunnel_arg_with_no_devices(mock_platform_system, caplog, capsys)
27072706
assert err == ""
27082707

27092708

2710-
@pytest.mark.skipif(sys.platform == "win32", reason="Linux is forced in test and no termios")
27112709
@pytest.mark.unit
27122710
@pytest.mark.usefixtures("reset_mt_config")
27132711
@patch("meshtastic.util.findPorts", return_value=[])

meshtastic/tests/test_serial_interface.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Meshtastic unit tests for serial_interface.py"""
2+
import platform
23
# pylint: disable=R0917
34

45
import re
@@ -28,9 +29,13 @@ def test_SerialInterface_single_port(
2829
iface.close()
2930
mocked_findPorts.assert_called()
3031
mocked_serial.assert_called()
31-
mocked_open.assert_called()
32-
mock_get.assert_called()
33-
mock_set.assert_called()
32+
33+
# doesn't get called in SerialInterface.__init__ on windows
34+
if platform.system() != "Windows":
35+
mocked_open.assert_called()
36+
mock_get.assert_called()
37+
mock_set.assert_called()
38+
3439
mock_sleep.assert_called()
3540
out, err = capsys.readouterr()
3641
assert re.search(r"Nodes in mesh", out, re.MULTILINE)

0 commit comments

Comments
 (0)