From 6b149e2fc9c142fbb35887eb0a7c4fdf114d4909 Mon Sep 17 00:00:00 2001 From: MirceaDan Date: Sat, 1 Dec 2018 11:57:10 -0800 Subject: [PATCH] fixed unitests for io.open change the fixups needed change to use io.open in mock some io.open needed to be read as text --- src/octoprint/plugins/pi_support/__init__.py | 4 ++-- tests/_fixups.py | 2 +- tests/filemanager/test_filemanager.py | 12 +++++------- tests/plugins/pi_support/test_pi_support.py | 4 ++-- tests/test_daemon.py | 12 ++++++------ 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/octoprint/plugins/pi_support/__init__.py b/src/octoprint/plugins/pi_support/__init__.py index 24faba68e..c40476731 100644 --- a/src/octoprint/plugins/pi_support/__init__.py +++ b/src/octoprint/plugins/pi_support/__init__.py @@ -125,7 +125,7 @@ def get_proc_dt_model(): global _proc_dt_model if _proc_dt_model is None: - with io.open(_PROC_DT_MODEL_PATH, 'rb') as f: + with io.open(_PROC_DT_MODEL_PATH, 'rt', encoding='utf-8') as f: _proc_dt_model = f.readline().strip(" \t\r\n\0") return _proc_dt_model @@ -151,7 +151,7 @@ def get_octopi_version(): global _octopi_version if _octopi_version is None: - with io.open(_OCTOPI_VERSION_PATH, 'rb') as f: + with io.open(_OCTOPI_VERSION_PATH, 'rt', encoding='utf-8') as f: _octopi_version = f.readline().strip(" \t\r\n\0") return _octopi_version diff --git a/tests/_fixups.py b/tests/_fixups.py index 4e2c3ab55..1a9bb7131 100644 --- a/tests/_fixups.py +++ b/tests/_fixups.py @@ -4,4 +4,4 @@ import sys -OPEN_SIGNATURE = 'builtins.open' if sys.version_info[0] >= 3 else '__builtin__.open' +OPEN_SIGNATURE = 'io.open' diff --git a/tests/filemanager/test_filemanager.py b/tests/filemanager/test_filemanager.py index 2f0e608d8..c52417a26 100644 --- a/tests/filemanager/test_filemanager.py +++ b/tests/filemanager/test_filemanager.py @@ -16,9 +16,7 @@ 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__" +import _fixups class FilemanagerMethodTest(unittest.TestCase): @@ -292,7 +290,7 @@ class FileManagerTest(unittest.TestCase): mock_time.return_value = now self.local_storage.path_in_storage.return_value = path - with mock.patch("{}.open".format(BUILTINS), mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, 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) @@ -317,8 +315,8 @@ class FileManagerTest(unittest.TestCase): mock_yaml_safe_dump.side_effect = RuntimeError - 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) + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: + self.file_manager.save_recovery_data(octoprint.filemanager.FileDestinations.LOCAL, path, pos) @mock.patch("os.path.isfile") @mock.patch("os.remove") @@ -361,7 +359,7 @@ class FileManagerTest(unittest.TestCase): date=123456789) text_data = yaml.dump(data) - with mock.patch("{}.open".format(BUILTINS), mock.mock_open(read_data=text_data)) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, 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() diff --git a/tests/plugins/pi_support/test_pi_support.py b/tests/plugins/pi_support/test_pi_support.py index 05354f713..3c68d6ded 100644 --- a/tests/plugins/pi_support/test_pi_support.py +++ b/tests/plugins/pi_support/test_pi_support.py @@ -21,7 +21,7 @@ class PiSupportTestCase(unittest.TestCase): m.return_value.readline.return_value = OCTOPI_VERSION version = get_octopi_version() - m.assert_called_once_with("/etc/octopi_version", "r") + m.assert_called_once_with("/etc/octopi_version", "rb") self.assertEqual(version, OCTOPI_VERSION) def test_get_proc_dt_model(self): @@ -32,7 +32,7 @@ class PiSupportTestCase(unittest.TestCase): m.return_value.readline.return_value = DT_MODEL model = get_proc_dt_model() - m.assert_called_once_with("/proc/device-tree/model", "r") + m.assert_called_once_with("/proc/device-tree/model", "rb") self.assertEqual(model, DT_MODEL) def test_get_vcgencmd_throttle_state(self): diff --git a/tests/test_daemon.py b/tests/test_daemon.py index 96974aca2..18fc29e25 100644 --- a/tests/test_daemon.py +++ b/tests/test_daemon.py @@ -128,7 +128,7 @@ class DaemonTest(unittest.TestCase): mock_stderr.flush.assert_called_once_with() self.assertListEqual(mock_open.mock_calls, - [mock.call(mock_devnull, "r"), + [mock.call(mock_devnull, "rb"), mock.call(mock_devnull, "a+"), mock.call(mock_devnull, "a+")]) self.assertListEqual(mock_dup2.mock_calls, @@ -396,7 +396,7 @@ class DaemonTest(unittest.TestCase): # assert self.assertEqual(result, pid) - m.assert_called_once_with(self.pidfile, "r") + m.assert_called_once_with(self.pidfile, "rb") def test_get_pid_ioerror(self): # setup @@ -409,7 +409,7 @@ class DaemonTest(unittest.TestCase): # assert self.assertIsNone(result) - m.assert_called_once_with(self.pidfile, "r") + m.assert_called_once_with(self.pidfile, "rb") def test_get_pid_valueerror(self): # setup @@ -421,7 +421,7 @@ class DaemonTest(unittest.TestCase): # assert self.assertIsNone(result) - m.assert_called_once_with(self.pidfile, "r") + m.assert_called_once_with(self.pidfile, "rb") def test_set_pid(self): # setup @@ -432,7 +432,7 @@ class DaemonTest(unittest.TestCase): self.daemon.set_pid(pid) # assert - m.assert_called_once_with(self.pidfile, "w+") + m.assert_called_once_with(self.pidfile, "wb+") handle = m() handle.write.assert_called_once_with("{}\n".format(pid)) @@ -445,7 +445,7 @@ class DaemonTest(unittest.TestCase): self.daemon.set_pid(pid) # assert - m.assert_called_once_with(self.pidfile, "w+") + m.assert_called_once_with(self.pidfile, "wb+") handle = m() handle.write.assert_called_once_with("{}\n".format(pid))