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
This commit is contained in:
MirceaDan committed 2018-12-01 11:57:10 -08:00
1 parent 337e49208a
commit 6b149e2fc9
5 files changed
+16 -18

No files matched your search

+2 -2
View File
@@ -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
+1 -1
View File
@@ -4,4 +4,4 @@
import sys
OPEN_SIGNATURE = 'builtins.open' if sys.version_info[0] >= 3 else '__builtin__.open'
OPEN_SIGNATURE = 'io.open'
+5 -7
View File
@@ -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()
+2 -2
View File
@@ -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):
+6 -6
View File
@@ -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))