Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2850,6 +2850,7 @@ class BadUsage(Exception): pass
directory. If <name> contains a '{sep}', it is treated as a filename; if
it names a directory, documentation is written for all the contents.
""".format(cmd=cmd, sep=os.sep))
sys.exit(1)

if __name__ == '__main__':
cli()
61 changes: 61 additions & 0 deletions Lib/test/test_pydoc/test_pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2462,6 +2462,67 @@ def __version__(self):
self.assertEqual(len(w), 1)


@support.force_not_colorized_test_class
class TestPydocCLI(unittest.TestCase):
"""Tests for the pydoc command-line interface."""

def test_show_doc(self):
rc, out, err = assert_python_ok('-m', 'pydoc', 'os')
self.assertEqual(rc, 0)
self.assertIn(b'os', out)
self.assertEqual(err, b'')

def test_show_doc_builtin(self):
rc, out, err = assert_python_ok('-m', 'pydoc', 'list')
self.assertEqual(rc, 0)
self.assertIn(b'list', out)
self.assertEqual(err, b'')

def test_show_doc_keyword(self):
rc, out, err = assert_python_ok('-m', 'pydoc', 'for')
self.assertEqual(rc, 0)
self.assertIn(b'for', out)
self.assertEqual(err, b'')

def test_keyword_search(self):
rc, out, err = assert_python_ok('-m', 'pydoc', '-k', 'thread')
self.assertEqual(rc, 0)
# Either matching modules are listed or no documentation is found.
self.assertTrue(
b'thread' in out.lower() or
b'no Python documentation found' in out,
out
)
self.assertEqual(err, b'')

def test_write_html(self):
with os_helper.temp_dir() as tmpdir:
rc, out, err = assert_python_ok('-m', 'pydoc', '-w', 'os',
__cwd=tmpdir)
self.assertEqual(rc, 0)
self.assertIn(b'wrote os.html', out.lower())
self.assertEqual(err, b'')
self.assertTrue(os.path.exists(os.path.join(tmpdir, 'os.html')))

def test_nonexistent_module(self):
rc, out, err = assert_python_failure('-m', 'pydoc', 'nonexistent_module')
self.assertEqual(rc, 1)
self.assertIn(b'nonexistent_module', out)
self.assertEqual(err, b'')

def test_no_args(self):
rc, out, err = assert_python_failure('-m', 'pydoc')
self.assertEqual(rc, 1)
self.assertIn(b'pydoc - the Python documentation tool', out)
self.assertEqual(err, b'')

def test_invalid_flag(self):
rc, out, err = assert_python_failure('-m', 'pydoc', '--xyz')
self.assertEqual(rc, 1)
self.assertIn(b'pydoc - the Python documentation tool', out)
self.assertEqual(err, b'')


def setUpModule():
thread_info = threading_helper.threading_setup()
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :mod:`pydoc` exit with code 1 when invoked with no arguments or an invalid option.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add CLI tests for :mod:`pydoc`.
Loading