From 242183223967bc0f3fcd72866a8caff17bea8a2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damien=20Nad=C3=A9?= Date: Wed, 22 May 2019 13:04:23 +0200 Subject: [PATCH 1/7] bpo-37008: unittest.mock.mock_open handle is now able to honor next() --- Lib/unittest/mock.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 654462cbf7e792..b14bf01b28fdbd 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -2680,6 +2680,11 @@ def _iter_side_effect(): for line in _state[0]: yield line + def _next_side_effect(): + if handle.readline.return_value is not None: + return handle.readline.return_value + return next(_state[0]) + global file_spec if file_spec is None: import _io @@ -2701,6 +2706,7 @@ def _iter_side_effect(): handle.readline.side_effect = _state[1] handle.readlines.side_effect = _readlines_side_effect handle.__iter__.side_effect = _iter_side_effect + handle.__next__.side_effect = _next_side_effect def reset_data(*args, **kwargs): _state[0] = _to_stream(read_data) From c35f1d6df4a51a49393907a77b3f17c6d39c2f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damien=20Nad=C3=A9?= Date: Wed, 22 May 2019 15:12:04 +0200 Subject: [PATCH 2/7] bpo-37008: test mock_open with next and __next__ --- Lib/unittest/test/testmock/testmock.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 307b8b7657afa1..d3ade5153eabaf 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1702,6 +1702,15 @@ def test_mock_open_dunder_iter_issue(self): self.assertEqual(lines[1], 'Norwegian Blue') self.assertEqual(list(f1), []) + def test_mock_open_using_next(self): + mocked_open = mock.mock_open(read_data='1st line\n2nd line\n3rd line') + f1 = mocked_open('a-name') + self.assertEqual(next(f1), '1st line\n') + self.assertEqual(f1.__next__(), '2nd line\n') + lines = [line for line in f1] + self.assertEqual(lines[0], '3rd line') + self.assertEqual(list(f1), []) + def test_mock_open_write(self): # Test exception in file writing write() mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV')) From 0437d87262445a333ca3dab5bf480116baf4211d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damien=20Nad=C3=A9?= Date: Wed, 22 May 2019 15:26:37 +0200 Subject: [PATCH 3/7] bpo-37008: make mock_open handle able to honor next() --- .../next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst diff --git a/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst b/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst new file mode 100644 index 00000000000000..185ccb0464f76c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst @@ -0,0 +1,2 @@ +Add support for calling `next` with the mock resulting from +:func:`unittest.mock.mock_open` From a6947b0b4a8d17916c2795c4a8a6102d190ca5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damien=20Nad=C3=A9?= Date: Wed, 22 May 2019 15:51:22 +0200 Subject: [PATCH 4/7] test unittest.mock.mock_open and next() iteractions --- Lib/unittest/test/testmock/testwith.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py index 5172c222d97a78..86b00240d517b9 100644 --- a/Lib/unittest/test/testmock/testwith.py +++ b/Lib/unittest/test/testmock/testwith.py @@ -231,6 +231,19 @@ def test_dunder_iter_data(self): self.assertEqual(lines[2], 'baz\n') self.assertEqual(h.readline(), '') + def test_next_data(self): + # Check that next will correctly return the next available + # line and plays well with the dunder_iter part. + mock = mock_open(read_data='foo\nbar\nbaz\n') + with patch('%s.open' % __name__, mock, create=True): + h = open('bar') + line1 = next(h) + line2 = next(h) + lines = [l for l in h] + self.assertEqual(line1, 'foo\n') + self.assertEqual(line2, 'bar\n') + self.assertEqual(lines[0], 'baz\n') + self.assertEqual(h.readline(), '') def test_readlines_data(self): # Test that emulating a file that ends in a newline character works From 001f9895979d8c7313ab1a6a98cbe5a1212ee88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damien=20Nad=C3=A9?= Date: Wed, 22 May 2019 15:55:26 +0200 Subject: [PATCH 5/7] assert that next can raise StopIteration on mock_open handle --- Lib/unittest/test/testmock/testmock.py | 2 ++ Lib/unittest/test/testmock/testwith.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index d3ade5153eabaf..4deefa0e4ea817 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1710,6 +1710,8 @@ def test_mock_open_using_next(self): lines = [line for line in f1] self.assertEqual(lines[0], '3rd line') self.assertEqual(list(f1), []) + with self.assertRaises(StopIteration): + next(h) def test_mock_open_write(self): # Test exception in file writing write() diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py index 86b00240d517b9..42ebf3898c89e6 100644 --- a/Lib/unittest/test/testmock/testwith.py +++ b/Lib/unittest/test/testmock/testwith.py @@ -230,6 +230,8 @@ def test_dunder_iter_data(self): self.assertEqual(lines[1], 'bar\n') self.assertEqual(lines[2], 'baz\n') self.assertEqual(h.readline(), '') + with self.assertRaises(StopIteration): + next(h) def test_next_data(self): # Check that next will correctly return the next available From 69372d72e7ce5e3f6d9ea1fbe827dea7bd8de085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damien=20Nad=C3=A9?= Date: Wed, 22 May 2019 15:58:42 +0200 Subject: [PATCH 6/7] improve test_mock_open_using_next readability by gathering all assertEqual lines together --- Lib/unittest/test/testmock/testmock.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 4deefa0e4ea817..0f30bccc9cf0df 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1705,13 +1705,15 @@ def test_mock_open_dunder_iter_issue(self): def test_mock_open_using_next(self): mocked_open = mock.mock_open(read_data='1st line\n2nd line\n3rd line') f1 = mocked_open('a-name') - self.assertEqual(next(f1), '1st line\n') - self.assertEqual(f1.__next__(), '2nd line\n') + line1 = next(f1) + line2 = f1.__next__() lines = [line for line in f1] + self.assertEqual(line1, '1st line\n') + self.assertEqual(line2, '2nd line\n') self.assertEqual(lines[0], '3rd line') self.assertEqual(list(f1), []) with self.assertRaises(StopIteration): - next(h) + next(f1) def test_mock_open_write(self): # Test exception in file writing write() From 0cc78ca03dc8f7b26b6e21d23e57d69f2d68b1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damien=20Nad=C3=A9?= Date: Wed, 22 May 2019 16:47:14 +0200 Subject: [PATCH 7/7] fixup! bpo-37008: make mock_open handle able to honor next() --- .../next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst b/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst index 185ccb0464f76c..42747aead49a49 100644 --- a/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst +++ b/Misc/NEWS.d/next/Library/2019-05-22-15-26-08.bpo-37008.WPbv31.rst @@ -1,2 +1,2 @@ -Add support for calling `next` with the mock resulting from +Add support for calling :func:`next` with the mock resulting from :func:`unittest.mock.mock_open`