From 746b4b2135be63b4bcc8cd3e13fc20cdd189f22a Mon Sep 17 00:00:00 2001 From: Filip Kotoucek Date: Wed, 14 Aug 2024 14:58:34 +0200 Subject: [PATCH] WIP --- prusaerrors/sl1/codes.py | 7 ++++--- tests/test_sl1.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/prusaerrors/sl1/codes.py b/prusaerrors/sl1/codes.py index c2e1ad2..d365339 100644 --- a/prusaerrors/sl1/codes.py +++ b/prusaerrors/sl1/codes.py @@ -16,6 +16,8 @@ from prusaerrors.shared.codes import Category, Code, Printer, unique_codes, Codes +PRINTER_MODEL_PATH = Path("/run/model") + if "_" not in vars(builtins): def _(value): @@ -32,14 +34,13 @@ def decor(cls): assert "Errors" in data printer = Printer.SL1 - path = Path("/run/model") - model = [x.name for x in path.iterdir() if x.is_file()] + model = [x.name for x in PRINTER_MODEL_PATH.iterdir() if x.is_file()] if len(model) == 1: a = Printer.M1.name.lower() if a in model: printer = Printer.M1 else: - raise KeyError("None or multiple model files found. Check /run/model folder.") + raise KeyError("None or multiple model files found. Check %s folder.", PRINTER_MODEL_PATH) re = re_compile( r"^(?P([0-9][0-9]|XX))" diff --git a/tests/test_sl1.py b/tests/test_sl1.py index 8e50777..000896a 100644 --- a/tests/test_sl1.py +++ b/tests/test_sl1.py @@ -6,7 +6,9 @@ # pylint: disable = missing-class-docstring # pylint: disable = missing-module-docstring +from tempfile import TemporaryDirectory import unittest +from unittest.mock import MagicMock, PropertyMock, patch from prusaerrors.sl1.codes import Sl1Codes @@ -15,8 +17,18 @@ class TestErrors(unittest.TestCase): def test_str_conversion(self): self.assertEqual("#10500", str(Sl1Codes.NONE)) + def test_code_lookup(self): - self.assertEqual(Sl1Codes.NONE, Sl1Codes.get("#10500")) + with patch("prusaerrors.sl1.codes.PRINTER_MODEL_PATH") as mock: + with TemporaryDirectory() as dir: + mock.return_value = dir + + with open(str(dir) + "/sl1", "w+", encoding="utf-8") as f: + self.assertEqual(Sl1Codes.NONE, Sl1Codes.get("#10500")) + with open(str(dir) + "/sl1s", "w+", encoding="utf-8") as f: + self.assertEqual(Sl1Codes.NONE, Sl1Codes.get("#10500")) + with open(str(dir) + "/m1", "w+", encoding="utf-8") as f: + self.assertEqual(Sl1Codes.NONE, Sl1Codes.get("#29500")) def test_unknown_code_lookup(self): self.assertEqual(Sl1Codes.UNKNOWN, Sl1Codes.get("random string"))