💚 Fix unit tests

io.open is no longer used, mocks needed adjustments
This commit is contained in:
Gina Häußge committed 2021-03-11 17:06:04 +01:00
1 parent fd2ece0f13
commit 7668afcff7
5 files changed
+18 -29

No files matched your search

-3
View File
@@ -1,3 +0,0 @@
import sys # noqa: F401
OPEN_SIGNATURE = "io.open"
+3 -5
View File
@@ -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()
+4 -7
View File
@@ -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):
+10 -12
View File
@@ -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
+1 -2
View File
@@ -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",