summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2016-05-16 03:22:45 (GMT)
committerRobert Collins <rbtcollins@hp.com>2016-05-16 03:22:45 (GMT)
commitc85dd85f1a7fe2d019cf75e93936ad2bc94a50e2 (patch)
treefa3f327fc24335054998ac4646b43c428c05e134 /Lib/unittest
parent2b6c032e6a93dbbe79d267c1dc4ae720e2f4a4d3 (diff)
parent9549a3e3d4be2a15c222996abff8cb97180ee9be (diff)
downloadcpython-c85dd85f1a7fe2d019cf75e93936ad2bc94a50e2.zip
cpython-c85dd85f1a7fe2d019cf75e93936ad2bc94a50e2.tar.gz
cpython-c85dd85f1a7fe2d019cf75e93936ad2bc94a50e2.tar.bz2
Issue #26807: mock_open 'files' no longer error on readline at end of file.
Patch from Yolanda Robla.
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/mock.py2
-rw-r--r--Lib/unittest/test/testmock/testmock.py12
2 files changed, 14 insertions, 0 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 7400fb7..123c156 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2341,6 +2341,8 @@ def mock_open(mock=None, read_data=''):
yield handle.readline.return_value
for line in _state[0]:
yield line
+ while True:
+ yield type(read_data)()
global file_spec
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 526eeab..9910ab9 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1440,6 +1440,18 @@ class MockTest(unittest.TestCase):
self.assertEqual('abc', first)
self.assertEqual('abc', second)
+ def test_mock_open_after_eof(self):
+ # read, readline and readlines should work after end of file.
+ _open = mock.mock_open(read_data='foo')
+ h = _open('bar')
+ h.read()
+ self.assertEqual('', h.read())
+ self.assertEqual('', h.read())
+ self.assertEqual('', h.readline())
+ self.assertEqual('', h.readline())
+ self.assertEqual([], h.readlines())
+ self.assertEqual([], h.readlines())
+
def test_mock_parents(self):
for Klass in Mock, MagicMock:
m = Klass()