From c2f09b1715aa72821572c6841ba4b747c7728a7a Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 13:14:27 -0700 Subject: [PATCH 01/12] fixes to test_comm_helpers --- src/octoprint/util/comm.py | 2 +- tests/util/test_comm_helpers.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 3397ac396..4b1a8e474 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -4219,7 +4219,7 @@ def parse_temperature_line(line, current): tool = values["tool"] toolnum = values.get("toolnum", None) toolNumber = int(toolnum) if toolnum is not None and len(toolnum) else None - if toolNumber > maxToolNum: + if toolNumber and toolNumber > maxToolNum: maxToolNum = toolNumber try: diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 119ff406d..21a733d8b 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -103,15 +103,15 @@ class TestCommHelpers(unittest.TestCase): m.update(input) return m.hexdigest() - temp_regex = "T:((\d*\.)\d+)" - temp_template = "Temp: {}" - temp2_template = "Temperature: {}" + temp_regex = b'T:((\d*\.)\d+)' + temp_template = b"Temp: {}" + temp2_template = b"Temperature: {}" temp_key = md5sum(temp_regex) temp_template_key = md5sum(temp_template) temp2_template_key = md5sum(temp2_template) - x_regex = "X:(?P\d+)" - x_template = "X: {x}" + x_regex = b"X:(?P\d+)" + x_template = b"X: {x}" x_key = md5sum(x_regex) x_template_key = md5sum(x_template) From 52fe19e5f9291c182d3d380ec269da8918bf6ecf Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 15:54:29 -0700 Subject: [PATCH 02/12] fix util.to_unicode for py2/py3. --- src/octoprint/util/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 9184077be..912f197a7 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -510,7 +510,7 @@ def to_str(s_or_u, encoding="utf-8", errors="strict"): def to_unicode(s_or_u, encoding="utf-8", errors="strict"): """Make sure ``s_or_u`` is a unicode string.""" - if isinstance(s_or_u, str): + if isinstance(s_or_u, bytes): return s_or_u.decode(encoding, errors=errors) else: return s_or_u From bd35de80c36d9b87fc06e7197b15ce839bb55fe6 Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 15:55:19 -0700 Subject: [PATCH 03/12] fix test_file_helpers, couldn't figureout a non-conditional way to do this --- tests/util/test_file_helpers.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/util/test_file_helpers.py b/tests/util/test_file_helpers.py index 36bf525da..214424891 100644 --- a/tests/util/test_file_helpers.py +++ b/tests/util/test_file_helpers.py @@ -332,12 +332,16 @@ class IsHiddenPathTest(unittest.TestCase): self.path_hidden_on_windows = os.path.join(self.basepath, "hidden_on_windows.txt") self.path_always_hidden = os.path.join(self.basepath, ".always_hidden.txt") + import sys for attr in ("path_always_visible", "path_hidden_on_windows", "path_always_hidden"): path = getattr(self, attr) - with open(path, "w+b") as f: - f.write(attr) + if sys.version_info[0] >= 3: + with open(path, "w+", encoding='utf-8') as f: + f.write(attr) + else: + with open(path, "wb+") as f: + f.write(attr) - import sys if sys.platform == "win32": # we use ctypes and the windows API to set the hidden attribute on the file # only hidden on windows From 472fda6645c0cb92e2686d29953f942c8d7e3ad2 Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 16:02:44 -0700 Subject: [PATCH 04/12] setuptools py3 compatability --- src/octoprint_setuptools/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/octoprint_setuptools/__init__.py b/src/octoprint_setuptools/__init__.py index 794bc2619..9c1b24eb6 100644 --- a/src/octoprint_setuptools/__init__.py +++ b/src/octoprint_setuptools/__init__.py @@ -8,6 +8,7 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms import os import shutil import glob +from past.builtins import basestring from setuptools import Command from distutils.command.clean import clean as _clean From aa383e3ca12eb3d3a6812b3b575e04e5017e4a46 Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 16:15:22 -0700 Subject: [PATCH 05/12] fix all __builtin__.open patch/mock calls --- tests/plugins/pi_support/test_pi_support.py | 5 +++-- tests/test_daemon.py | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/plugins/pi_support/test_pi_support.py b/tests/plugins/pi_support/test_pi_support.py index be33f8c71..52c1baa38 100644 --- a/tests/plugins/pi_support/test_pi_support.py +++ b/tests/plugins/pi_support/test_pi_support.py @@ -2,6 +2,7 @@ import unittest import ddt import mock +from builtins import open # lets py2 use 'builtins.open' instead of '__builtin__.open' in mocking OCTOPI_VERSION = "0.14.0" @@ -14,7 +15,7 @@ class PiSupportTestCase(unittest.TestCase): def test_get_octopi_version(self): from octoprint.plugins.pi_support import get_octopi_version - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch("builtins.open", mock.mock_open(), create=True) as m: m.return_value.readline.return_value = OCTOPI_VERSION version = get_octopi_version() @@ -24,7 +25,7 @@ class PiSupportTestCase(unittest.TestCase): def test_get_proc_dt_model(self): from octoprint.plugins.pi_support import get_proc_dt_model - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch("builtins.open", mock.mock_open(), create=True) as m: m.return_value.readline.return_value = DT_MODEL model = get_proc_dt_model() diff --git a/tests/test_daemon.py b/tests/test_daemon.py index 61211b2c9..bec339c5d 100644 --- a/tests/test_daemon.py +++ b/tests/test_daemon.py @@ -1,5 +1,6 @@ # coding=utf-8 from __future__ import absolute_import, division, print_function +from builtins import open # lets py2 use 'builtins.open' instead of '__builtin__.open' in mocking import unittest import mock @@ -102,7 +103,7 @@ class DaemonTest(unittest.TestCase): @mock.patch("sys.stdout") @mock.patch("sys.stderr") @mock.patch("os.devnull") - @mock.patch("__builtin__.open") + @mock.patch("builtins.open") @mock.patch("os.dup2") def test_redirect_io(self, mock_dup2, mock_open, mock_devnull, mock_stderr, mock_stdout, mock_stdin): # setup @@ -390,7 +391,7 @@ class DaemonTest(unittest.TestCase): pid = 1234 # test - with mock.patch("__builtin__.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: + with mock.patch("builtins.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: result = self.daemon.get_pid() # assert @@ -403,7 +404,7 @@ class DaemonTest(unittest.TestCase): handle.__enter__.side_effect = IOError() # test - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch("builtins.open", mock.mock_open(), create=True) as m: result = self.daemon.get_pid() # assert @@ -415,7 +416,7 @@ class DaemonTest(unittest.TestCase): pid = "not an integer" # test - with mock.patch("__builtin__.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: + with mock.patch("builtins.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: result = self.daemon.get_pid() # assert @@ -427,7 +428,7 @@ class DaemonTest(unittest.TestCase): pid = "1234" # test - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch("builtins.open", mock.mock_open(), create=True) as m: self.daemon.set_pid(pid) # assert @@ -440,7 +441,7 @@ class DaemonTest(unittest.TestCase): pid = 1234 # test - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch("builtins.open", mock.mock_open(), create=True) as m: self.daemon.set_pid(pid) # assert From 22fdc1d933308a434d5894fdaeee7924b6f9e450 Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 16:58:24 -0700 Subject: [PATCH 06/12] final open mock tactic: add a central global. --- tests/_fixups.py | 7 +++++++ tests/plugins/pi_support/test_pi_support.py | 9 ++++++--- tests/test_daemon.py | 14 +++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 tests/_fixups.py diff --git a/tests/_fixups.py b/tests/_fixups.py new file mode 100644 index 000000000..4e2c3ab55 --- /dev/null +++ b/tests/_fixups.py @@ -0,0 +1,7 @@ + + + +import sys + + +OPEN_SIGNATURE = 'builtins.open' if sys.version_info[0] >= 3 else '__builtin__.open' diff --git a/tests/plugins/pi_support/test_pi_support.py b/tests/plugins/pi_support/test_pi_support.py index 52c1baa38..05354f713 100644 --- a/tests/plugins/pi_support/test_pi_support.py +++ b/tests/plugins/pi_support/test_pi_support.py @@ -2,7 +2,8 @@ import unittest import ddt import mock -from builtins import open # lets py2 use 'builtins.open' instead of '__builtin__.open' in mocking + +import _fixups OCTOPI_VERSION = "0.14.0" @@ -14,8 +15,9 @@ class PiSupportTestCase(unittest.TestCase): def test_get_octopi_version(self): from octoprint.plugins.pi_support import get_octopi_version + #import _fixups - with mock.patch("builtins.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: m.return_value.readline.return_value = OCTOPI_VERSION version = get_octopi_version() @@ -24,8 +26,9 @@ class PiSupportTestCase(unittest.TestCase): def test_get_proc_dt_model(self): from octoprint.plugins.pi_support import get_proc_dt_model + #import _fixups - with mock.patch("builtins.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: m.return_value.readline.return_value = DT_MODEL model = get_proc_dt_model() diff --git a/tests/test_daemon.py b/tests/test_daemon.py index bec339c5d..96974aca2 100644 --- a/tests/test_daemon.py +++ b/tests/test_daemon.py @@ -1,6 +1,6 @@ # coding=utf-8 from __future__ import absolute_import, division, print_function -from builtins import open # lets py2 use 'builtins.open' instead of '__builtin__.open' in mocking +import _fixups import unittest import mock @@ -103,7 +103,7 @@ class DaemonTest(unittest.TestCase): @mock.patch("sys.stdout") @mock.patch("sys.stderr") @mock.patch("os.devnull") - @mock.patch("builtins.open") + @mock.patch(_fixups.OPEN_SIGNATURE) @mock.patch("os.dup2") def test_redirect_io(self, mock_dup2, mock_open, mock_devnull, mock_stderr, mock_stdout, mock_stdin): # setup @@ -391,7 +391,7 @@ class DaemonTest(unittest.TestCase): pid = 1234 # test - with mock.patch("builtins.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: result = self.daemon.get_pid() # assert @@ -404,7 +404,7 @@ class DaemonTest(unittest.TestCase): handle.__enter__.side_effect = IOError() # test - with mock.patch("builtins.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: result = self.daemon.get_pid() # assert @@ -416,7 +416,7 @@ class DaemonTest(unittest.TestCase): pid = "not an integer" # test - with mock.patch("builtins.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: result = self.daemon.get_pid() # assert @@ -428,7 +428,7 @@ class DaemonTest(unittest.TestCase): pid = "1234" # test - with mock.patch("builtins.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: self.daemon.set_pid(pid) # assert @@ -441,7 +441,7 @@ class DaemonTest(unittest.TestCase): pid = 1234 # test - with mock.patch("builtins.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: self.daemon.set_pid(pid) # assert From 791e759b4039f31f731f8a3e558e48366d66771f Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 17:21:05 -0700 Subject: [PATCH 07/12] keys() doesn't return a list, so restoring the contract. --- src/octoprint/slicing/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/slicing/__init__.py b/src/octoprint/slicing/__init__.py index e919f1727..dfb07d627 100644 --- a/src/octoprint/slicing/__init__.py +++ b/src/octoprint/slicing/__init__.py @@ -157,7 +157,7 @@ class SlicingManager(object): Returns: (list of str) Identifiers of all available slicers. """ - return self._slicers.keys() + return list(self._slicers.keys()) @property def configured_slicers(self): From df706f61db051487dd7d88a435efe5f71eb48ff4 Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 17:21:33 -0700 Subject: [PATCH 08/12] import locally for py3 mock compatability --- tests/slicing/test_slicingmanager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/slicing/test_slicingmanager.py b/tests/slicing/test_slicingmanager.py index d3ef60ea6..138f69223 100644 --- a/tests/slicing/test_slicingmanager.py +++ b/tests/slicing/test_slicingmanager.py @@ -28,6 +28,7 @@ class TestSlicingManager(unittest.TestCase): self._mock_slicer_plugins(self.slicer_plugin) # mock profile manager + import octoprint.printer.profile self.printer_profile_manager = mock.MagicMock(spec=octoprint.printer.profile.PrinterProfileManager) # mock settings From 743ff35bcf563643f773c5cbd352d535f54614fa Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 18:05:10 -0700 Subject: [PATCH 09/12] add some yaml wrappers for the test environment --- tests/settings/test_settings.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index 9c8ab966e..3e110d6ab 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -13,6 +13,7 @@ import unittest import shutil import contextlib import os +import sys import tempfile import yaml import hashlib @@ -24,15 +25,28 @@ import octoprint.settings @ddt.ddt class TestSettings(unittest.TestCase): + def _load_yaml(self, fname): + if sys.version_info[0] >= 3: + with open(fname, "w+", encoding='utf-8') as f: + return yaml.safe_load(f) + else: + with open(fname, "wb+") as f: + return yaml.safe_load(f) + def _dump_yaml(self, fname, config): + if sys.version_info[0] >= 3: + with open(fname, "w+", encoding='utf-8') as f: + yaml.safe_dump(config, f) + else: + with open(fname, "wb+") as f: + yaml.safe_dump(config, f) + def setUp(self): self.base_path = os.path.join(os.path.dirname(__file__), "_files") self.config_path = os.path.realpath(os.path.join(self.base_path, "config.yaml")) self.defaults_path = os.path.realpath(os.path.join(self.base_path, "defaults.yaml")) - with open(self.config_path, "r+b") as f: - self.config = yaml.safe_load(f) - with open(self.defaults_path, "r+b") as f: - self.defaults = yaml.safe_load(f) + self.config = self._load_yaml(self.config_path) + self.defaults = self._load_yaml(self.defaults_path) from octoprint.util import dict_merge self.expected_effective = dict_merge(self.defaults, self.config) @@ -503,11 +517,9 @@ class TestSettings(unittest.TestCase): self.assertEqual("0.0.0.0", settings.get(["server", "host"])) # modify yaml file externally - with open(configfile, "r+b") as f: - config = yaml.safe_load(f) + config = self._load_yaml(configfile) config["server"]["host"] = "127.0.0.1" - with open(configfile, "w+b") as f: - yaml.safe_dump(config, f) + self._dump_yaml(configfile, config) # set some value, should also reload file before setting new api key settings.set(["api", "key"], "key") From 9cb3631ad02334edc02099b4f29c9a537982e69b Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 18:05:32 -0700 Subject: [PATCH 10/12] first py3 fix --- src/octoprint/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 08eb032bd..fe8c50fcc 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -1502,7 +1502,7 @@ class Settings(object): return value is not None def getBaseFolder(self, type, create=True, allow_fallback=True, log_error=False, check_writable=True, deep_check_writable=False): - if type not in default_settings["folder"].keys() + ["base"]: + if type not in list(default_settings["folder"].keys()) + ["base"]: return None if type == "base": From b3cc44cea5b03de5742f633f22b83f9bb0d9edaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 30 Oct 2018 16:46:12 +0100 Subject: [PATCH 11/12] Fix build error `octoprint_setuptools` is imported into the build before the dependencies are installed, hence we don't have `past.builtins` yet. Since the `basestring` type was only needed for some asserts here that *usually* shouldn't be needed we can just comment out that stuff for now. --- src/octoprint_setuptools/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/octoprint_setuptools/__init__.py b/src/octoprint_setuptools/__init__.py index 9c1b24eb6..d7b0884d7 100644 --- a/src/octoprint_setuptools/__init__.py +++ b/src/octoprint_setuptools/__init__.py @@ -8,7 +8,6 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms import os import shutil import glob -from past.builtins import basestring from setuptools import Command from distutils.command.clean import clean as _clean @@ -55,9 +54,9 @@ def has_requirement(requirement, requirements): if requirement is None or requirements is None: return False - assert isinstance(requirement, basestring) - assert isinstance(requirements, (list, tuple)) - assert all(list(map(lambda x: x is not None and isinstance(x, basestring), requirements))) + #assert isinstance(requirement, basestring) + #assert isinstance(requirements, (list, tuple)) + #assert all(list(map(lambda x: x is not None and isinstance(x, basestring), requirements))) requirement = requirement.lower() requirements = [r.lower() for r in requirements] From 3ad4818488dbec7864151ca8afb3b12647322833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 30 Oct 2018 16:56:14 +0100 Subject: [PATCH 12/12] Use read instead of write mode in _load_yaml --- tests/settings/test_settings.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index 3e110d6ab..39d6570b1 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -27,11 +27,12 @@ class TestSettings(unittest.TestCase): def _load_yaml(self, fname): if sys.version_info[0] >= 3: - with open(fname, "w+", encoding='utf-8') as f: + with open(fname, "r+", encoding='utf-8') as f: return yaml.safe_load(f) else: - with open(fname, "wb+") as f: + with open(fname, "r+b") as f: return yaml.safe_load(f) + def _dump_yaml(self, fname, config): if sys.version_info[0] >= 3: with open(fname, "w+", encoding='utf-8') as f: