final open mock tactic: add a central global.
This commit is contained in:
3 files changed
+20
-10
No files matched your search
@@ -0,0 +1,7 @@
|
||||
|
||||
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
OPEN_SIGNATURE = 'builtins.open' if sys.version_info[0] >= 3 else '__builtin__.open'
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user