Skip to content

Commit 3d81c05

Browse files
rudy-07orsenthil
andauthored
gh-131178: Fix test_cli_retrieve_file when stdout is StringIO in test_ftplib (#161135)
* gh-131178: Mock sys.stdout in test_ftplib CLI retrieve test Mock sys.stdout in CommandLineTest.test_cli_retrieve_file so that accessing sys.stdout.buffer does not fail when regrtest or other test runners wrap sys.stdout in an io.StringIO object. * Fix the refleaks by not creating an instance of OSError --------- Co-authored-by: Senthil Kumaran <senthil@python.org>
1 parent 4594f5a commit 3d81c05

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

‎Lib/test/test_ftplib.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,15 +1242,18 @@ def test_cli_commands(self, mock_netrc, mock_ftp_cls):
12421242
@mock.patch('netrc.netrc', side_effect=OSError)
12431243
def test_cli_retrieve_file(self, mock_netrc, mock_ftp_cls):
12441244
mock_ftp = mock_ftp_cls.return_value
1245-
with mock.patch('sys.argv', ['ftplib', 'example.com', 'testfile.txt']):
1245+
with mock.patch('sys.argv', ['ftplib', 'example.com', 'testfile.txt']), \
1246+
mock.patch('sys.stdout') as mock_stdout:
12461247
ftplib.test()
1247-
mock_ftp.retrbinary.assert_called_once()
1248-
args, kwargs = mock_ftp.retrbinary.call_args
1249-
self.assertEqual(args[0], 'RETR testfile.txt')
1248+
mock_ftp.retrbinary.assert_called_once_with(
1249+
'RETR testfile.txt', mock_stdout.buffer.write, 1024
1250+
)
1251+
mock_stdout.buffer.flush.assert_called_once()
1252+
mock_stdout.flush.assert_called_once()
12501253
mock_ftp.quit.assert_called_once()
12511254

12521255
@mock.patch('ftplib.FTP')
1253-
@mock.patch('netrc.netrc', side_effect=OSError('file not found'))
1256+
@mock.patch('netrc.netrc', side_effect=OSError)
12541257
def test_cli_custom_rcfile_error(self, mock_netrc, mock_ftp_cls):
12551258
with mock.patch('sys.argv', ['ftplib', '-rnonexistent.netrc', 'example.com']), \
12561259
support.captured_stderr() as stderr:

0 commit comments

Comments
 (0)