From 8a0252ccfea190a4858f2258933c914a094c99a3 Mon Sep 17 00:00:00 2001 From: tedder Date: Sun, 2 Sep 2018 00:16:06 -0700 Subject: [PATCH] add patch workaround for py2/py3 --- tests/filemanager/test_filemanager.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/filemanager/test_filemanager.py b/tests/filemanager/test_filemanager.py index 6de49d48d..de134e8bc 100644 --- a/tests/filemanager/test_filemanager.py +++ b/tests/filemanager/test_filemanager.py @@ -9,12 +9,17 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms import io import unittest import mock +import six import octoprint.filemanager import octoprint.filemanager.util import octoprint.settings +# we can't seem to get this from six.moves, so we have to work around it to ensure py2/py3 compatability +BUILTINS = "builtins" +if six.PY2: BUILTINS = "__builtin__" + class FilemanagerMethodTest(unittest.TestCase): def setUp(self): @@ -287,7 +292,7 @@ class FileManagerTest(unittest.TestCase): mock_time.return_value = now self.local_storage.path_in_storage.return_value = path - with mock.patch("builtins.open", mock.mock_open(), create=True) as m: + with mock.patch("{}.open".format(BUILTINS), mock.mock_open(), create=True) as m: self.file_manager.save_recovery_data(octoprint.filemanager.FileDestinations.LOCAL, path, pos) mock_atomic_write.assert_called_with(recovery_file, max_permissions=0o666) @@ -312,7 +317,7 @@ class FileManagerTest(unittest.TestCase): mock_yaml_safe_dump.side_effect = RuntimeError - with mock.patch("builtins.open", mock.mock_open(), create=True) as m: + with mock.patch("{}.open".format(BUILTINS), mock.mock_open(), create=True) as m: self.file_manager.save_recovery_data(octoprint.filemanager.FileDestinations.LOCAL, path, pos) @mock.patch("os.path.isfile") @@ -356,7 +361,7 @@ class FileManagerTest(unittest.TestCase): date=123456789) text_data = yaml.dump(data) - with mock.patch("__builtin__.open", mock.mock_open(read_data=text_data)) as m: + with mock.patch("{}.open".format(BUILTINS), mock.mock_open(read_data=text_data)) as m: # moved safe_load to here so we could mock up the return value properly with mock.patch("yaml.safe_load", return_value=data) as n: result = self.file_manager.get_recovery_data()