GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [3.15] gh-131178: Add tests for `ftplib` command-line interface (GH-161080) by miss-islington · Pull Request #161154 · python/cpython

[3.15] gh-131178: Add tests for ftplib command-line interface (GH-161080) #161154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
orsenthil merged 2 commits into python:3.15 from miss-islington:backport-c29f95a-3.15
Nov 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

[3.15] gh-131178: Add tests for ftplib command-line interface (GH-161080) #161154

Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
21bff19
gh-131178: Add tests for `ftplib` command-line interface (GH-161080)
rudy-07 Nov 1, 2026
9f8dcb2
Fix the buildbot.
orsenthil Nov 2, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
gh-131178: Add tests for ftplib command-line interface (GH-161080)
* gh-131178: Add tests for ftplib command-line interface

Add unit tests in Lib/test/test_ftplib.py covering the ftplib command-line interface: verifying usage message on empty invocation, subprocess invocation, host connection with debuglevel, directory and passive mode commands, file retrieval, and custom netrc file error handling.

(cherry picked from commit c29f95a)

Co-authored-by: Rudransh Shekhar <[email protected]>
Co-authored-by: Senthil Kumaran <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@rudy-07 @orsenthil @miss-islington
2 people authored and miss-islington committed Nov 2, 2026
commit 21bff19093a616f65af9eaa3fd026ee09ada8242

There are no files selected for viewing

68 changes: 68 additions & 0 deletions 68 Lib/test/test_ftplib.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,74 @@ def test__all__(self):
support.check__all__(self, ftplib, not_exported=not_exported)


class CommandLineTest(TestCase):
"""Tests for the ftplib command-line interface."""

def test_main_no_arguments_prints_usage(self):
with mock.patch('sys.argv', ['ftplib']), \
support.captured_stdout() as stdout:
with self.assertRaises(SystemExit) as cm:
ftplib.test()
self.assertEqual(cm.exception.code, 0)
output = stdout.getvalue()
self.assertIn('Usage:', output)
self.assertIn('Options:', output)
self.assertIn('Commands:', output)

@requires_subprocess()
def test_subprocess_invocation_no_args(self):
from test.support.script_helper import assert_python_ok
rc, out, err = assert_python_ok('-m', 'ftplib')
self.assertEqual(rc, 0)
self.assertIn(b'Usage:', out)
self.assertIn(b'Options:', out)
self.assertIn(b'Commands:', out)
self.assertEqual(err, b'')

@mock.patch('ftplib.FTP')
@mock.patch('netrc.netrc', side_effect=OSError)
def test_cli_host_and_debugging(self, mock_netrc, mock_ftp_cls):
mock_ftp = mock_ftp_cls.return_value
with mock.patch('sys.argv', ['ftplib', '-d', '-d', 'example.com']):
ftplib.test()
mock_ftp_cls.assert_called_once_with('example.com')
mock_ftp.set_debuglevel.assert_called_once_with(2)
mock_ftp.login.assert_called_once_with('', '', '')
mock_ftp.quit.assert_called_once()

@mock.patch('ftplib.FTP')
@mock.patch('netrc.netrc', side_effect=OSError)
def test_cli_commands(self, mock_netrc, mock_ftp_cls):
mock_ftp = mock_ftp_cls.return_value
with mock.patch('sys.argv', ['ftplib', 'example.com', '-l/pub', '-d/incoming', '-p']):
ftplib.test()
mock_ftp_cls.assert_called_once_with('example.com')
mock_ftp.dir.assert_called_once_with('/pub')
mock_ftp.sendcmd.assert_called_once_with('CWD /incoming')
mock_ftp.set_pasv.assert_called_once()
mock_ftp.quit.assert_called_once()

@mock.patch('ftplib.FTP')
@mock.patch('netrc.netrc', side_effect=OSError)
def test_cli_retrieve_file(self, mock_netrc, mock_ftp_cls):
mock_ftp = mock_ftp_cls.return_value
with mock.patch('sys.argv', ['ftplib', 'example.com', 'testfile.txt']):
ftplib.test()
mock_ftp.retrbinary.assert_called_once()
args, kwargs = mock_ftp.retrbinary.call_args
self.assertEqual(args[0], 'RETR testfile.txt')
mock_ftp.quit.assert_called_once()

@mock.patch('ftplib.FTP')
@mock.patch('netrc.netrc', side_effect=OSError('file not found'))
def test_cli_custom_rcfile_error(self, mock_netrc, mock_ftp_cls):
with mock.patch('sys.argv', ['ftplib', '-rnonexistent.netrc', 'example.com']), \
support.captured_stderr() as stderr:
ftplib.test()
self.assertIn("Could not open account file -- using anonymous login.",
stderr.getvalue())


def setUpModule():
thread_info = threading_helper.threading_setup()
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.