From 7668afcff7316fd164e2cb75c72dd3e2d28daf1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 11 Mar 2021 17:06:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Fix=20unit=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit io.open is no longer used, mocks needed adjustments --- tests/_fixups.py | 3 --- tests/filemanager/test_filemanager.py | 8 +++----- tests/plugins/pi_support/test_pi_support.py | 11 ++++------- tests/test_daemon.py | 22 ++++++++++----------- tests/util/test_file_helpers.py | 3 +-- 5 files changed, 18 insertions(+), 29 deletions(-) delete mode 100644 tests/_fixups.py diff --git a/tests/_fixups.py b/tests/_fixups.py deleted file mode 100644 index 1dac32bc3..000000000 --- a/tests/_fixups.py +++ /dev/null @@ -1,3 +0,0 @@ -import sys # noqa: F401 - -OPEN_SIGNATURE = "io.open" diff --git a/tests/filemanager/test_filemanager.py b/tests/filemanager/test_filemanager.py index c92bcb8d0..a8e2836ad 100644 --- a/tests/filemanager/test_filemanager.py +++ b/tests/filemanager/test_filemanager.py @@ -7,8 +7,6 @@ import io import unittest import unittest.mock as mock -import _fixups - import octoprint.filemanager import octoprint.filemanager.util import octoprint.settings @@ -393,7 +391,7 @@ class FileManagerTest(unittest.TestCase): mock_time.return_value = now self.local_storage.path_in_storage.return_value = path - with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True): + with mock.patch("builtins.open", mock.mock_open(), create=True): self.file_manager.save_recovery_data( octoprint.filemanager.FileDestinations.LOCAL, path, pos ) @@ -429,7 +427,7 @@ class FileManagerTest(unittest.TestCase): mock_yaml_safe_dump.side_effect = RuntimeError - with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True): + with mock.patch("builtins.open", mock.mock_open(), create=True): self.file_manager.save_recovery_data( octoprint.filemanager.FileDestinations.LOCAL, path, pos ) @@ -480,7 +478,7 @@ class FileManagerTest(unittest.TestCase): } text_data = yaml.dump(data) - with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(read_data=text_data)) as m: + with mock.patch("builtins.open", 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 9e7baf5d5..f837d7ecb 100644 --- a/tests/plugins/pi_support/test_pi_support.py +++ b/tests/plugins/pi_support/test_pi_support.py @@ -1,7 +1,6 @@ import unittest import unittest.mock as mock -import _fixups import ddt from octoprint.util.platform import CLOSE_FDS @@ -19,23 +18,21 @@ class PiSupportTestCase(unittest.TestCase): # import _fixups - with mock.patch(_fixups.OPEN_SIGNATURE, 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() - m.assert_called_once_with("/etc/octopi_version", "rt", encoding="utf-8") + m.assert_called_once_with("/etc/octopi_version", encoding="utf-8") self.assertEqual(version, OCTOPI_VERSION) def test_get_proc_dt_model(self): from octoprint.plugins.pi_support import get_proc_dt_model - # import _fixups - - with mock.patch(_fixups.OPEN_SIGNATURE, 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() - m.assert_called_once_with("/proc/device-tree/model", "rt", encoding="utf-8") + m.assert_called_once_with("/proc/device-tree/model", encoding="utf-8") 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 98a69ff1d..17b3823f1 100644 --- a/tests/test_daemon.py +++ b/tests/test_daemon.py @@ -1,8 +1,6 @@ import unittest import unittest.mock as mock -import _fixups - import octoprint.daemon @@ -104,7 +102,7 @@ class DaemonTest(unittest.TestCase): @mock.patch("sys.stdout") @mock.patch("sys.stderr") @mock.patch("os.devnull") - @mock.patch(_fixups.OPEN_SIGNATURE) + @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 @@ -133,7 +131,7 @@ class DaemonTest(unittest.TestCase): self.assertListEqual( mock_open.mock_calls, [ - mock.call(mock_devnull, "rt", encoding="utf-8"), + mock.call(mock_devnull, encoding="utf-8"), mock.call(mock_devnull, "at+", encoding="utf-8"), mock.call(mock_devnull, "at+", encoding="utf-8"), ], @@ -402,7 +400,7 @@ class DaemonTest(unittest.TestCase): # test with mock.patch( - _fixups.OPEN_SIGNATURE, + "builtins.open", mock.mock_open(read_data=f"{pid}\n"), create=True, ) as m: @@ -410,7 +408,7 @@ class DaemonTest(unittest.TestCase): # assert self.assertEqual(result, pid) - m.assert_called_once_with(self.pidfile, "rt", encoding="utf-8") + m.assert_called_once_with(self.pidfile, encoding="utf-8") def test_get_pid_ioerror(self): # setup @@ -418,12 +416,12 @@ class DaemonTest(unittest.TestCase): handle.__enter__.side_effect = IOError() # test - with mock.patch(_fixups.OPEN_SIGNATURE, 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 self.assertIsNone(result) - m.assert_called_once_with(self.pidfile, "rt", encoding="utf-8") + m.assert_called_once_with(self.pidfile, encoding="utf-8") def test_get_pid_valueerror(self): # setup @@ -431,7 +429,7 @@ class DaemonTest(unittest.TestCase): # test with mock.patch( - _fixups.OPEN_SIGNATURE, + "builtins.open", mock.mock_open(read_data=f"{pid}\n"), create=True, ) as m: @@ -439,14 +437,14 @@ class DaemonTest(unittest.TestCase): # assert self.assertIsNone(result) - m.assert_called_once_with(self.pidfile, "rt", encoding="utf-8") + m.assert_called_once_with(self.pidfile, encoding="utf-8") def test_set_pid(self): # setup pid = "1234" # test - with mock.patch(_fixups.OPEN_SIGNATURE, 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 @@ -459,7 +457,7 @@ class DaemonTest(unittest.TestCase): pid = 1234 # test - with mock.patch(_fixups.OPEN_SIGNATURE, 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 diff --git a/tests/util/test_file_helpers.py b/tests/util/test_file_helpers.py index 62c2d889e..e4d50259f 100644 --- a/tests/util/test_file_helpers.py +++ b/tests/util/test_file_helpers.py @@ -1,7 +1,6 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms of the AGPLv3 License" -import io import os import sys import unittest @@ -77,7 +76,7 @@ class BomAwareOpenTest(unittest.TestCase): def test_bom_aware_open_parameters_text_mode(self): """Tests that the parameters are propagated properly in text mode.""" - with mock.patch("io.open", wraps=io.open) as mock_open: + with mock.patch("builtins.open", wraps=open) as mock_open: with octoprint.util.bom_aware_open( self.filename_utf8_without_bom, mode="rt",