From 22fdc1d933308a434d5894fdaeee7924b6f9e450 Mon Sep 17 00:00:00 2001 From: tedder Date: Sat, 22 Sep 2018 16:58:24 -0700 Subject: [PATCH] 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