Skip to content

Commit 394119a

Browse files
Anvilmiss-islington
authored andcommitted
bpo-37008: make mock_open handle able to honor next() (pythonGH-13492)
I've reported the issue on https://bugs.python.org/issue37008 and now I'm trying to bring a solution to this minor issue. I think it could be trivially backported to 3.7 branch. https://bugs.python.org/issue37008
1 parent 51aa35e commit 394119a

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

Lib/unittest/mock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,11 @@ def _iter_side_effect():
26802680
for line in _state[0]:
26812681
yield line
26822682

2683+
def _next_side_effect():
2684+
if handle.readline.return_value is not None:
2685+
return handle.readline.return_value
2686+
return next(_state[0])
2687+
26832688
global file_spec
26842689
if file_spec is None:
26852690
import _io
@@ -2701,6 +2706,7 @@ def _iter_side_effect():
27012706
handle.readline.side_effect = _state[1]
27022707
handle.readlines.side_effect = _readlines_side_effect
27032708
handle.__iter__.side_effect = _iter_side_effect
2709+
handle.__next__.side_effect = _next_side_effect
27042710

27052711
def reset_data(*args, **kwargs):
27062712
_state[0] = _to_stream(read_data)

Lib/unittest/test/testmock/testmock.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,19 @@ def test_mock_open_dunder_iter_issue(self):
17021702
self.assertEqual(lines[1], 'Norwegian Blue')
17031703
self.assertEqual(list(f1), [])
17041704

1705+
def test_mock_open_using_next(self):
1706+
mocked_open = mock.mock_open(read_data='1st line\n2nd line\n3rd line')
1707+
f1 = mocked_open('a-name')
1708+
line1 = next(f1)
1709+
line2 = f1.__next__()
1710+
lines = [line for line in f1]
1711+
self.assertEqual(line1, '1st line\n')
1712+
self.assertEqual(line2, '2nd line\n')
1713+
self.assertEqual(lines[0], '3rd line')
1714+
self.assertEqual(list(f1), [])
1715+
with self.assertRaises(StopIteration):
1716+
next(f1)
1717+
17051718
def test_mock_open_write(self):
17061719
# Test exception in file writing write()
17071720
mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV'))

Lib/unittest/test/testmock/testwith.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,22 @@ def test_dunder_iter_data(self):
230230
self.assertEqual(lines[1], 'bar\n')
231231
self.assertEqual(lines[2], 'baz\n')
232232
self.assertEqual(h.readline(), '')
233+
with self.assertRaises(StopIteration):
234+
next(h)
233235

236+
def test_next_data(self):
237+
# Check that next will correctly return the next available
238+
# line and plays well with the dunder_iter part.
239+
mock = mock_open(read_data='foo\nbar\nbaz\n')
240+
with patch('%s.open' % __name__, mock, create=True):
241+
h = open('bar')
242+
line1 = next(h)
243+
line2 = next(h)
244+
lines = [l for l in h]
245+
self.assertEqual(line1, 'foo\n')
246+
self.assertEqual(line2, 'bar\n')
247+
self.assertEqual(lines[0], 'baz\n')
248+
self.assertEqual(h.readline(), '')
234249

235250
def test_readlines_data(self):
236251
# Test that emulating a file that ends in a newline character works
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for calling :func:`next` with the mock resulting from
2+
:func:`unittest.mock.mock_open`

0 commit comments

Comments
 (0)