Skip to content

Commit 154328e

Browse files
committed
gh-131178: Add tests for pydoc command-line interface
1 parent ad38cf8 commit 154328e

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lib/pydoc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,7 @@ class BadUsage(Exception): pass
28502850
directory. If <name> contains a '{sep}', it is treated as a filename; if
28512851
it names a directory, documentation is written for all the contents.
28522852
""".format(cmd=cmd, sep=os.sep))
2853+
sys.exit(1)
28532854

28542855
if __name__ == '__main__':
28552856
cli()

Lib/test/test_pydoc/test_pydoc.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,6 +2462,67 @@ def __version__(self):
24622462
self.assertEqual(len(w), 1)
24632463

24642464

2465+
@support.force_not_colorized_test_class
2466+
class TestPydocCLI(unittest.TestCase):
2467+
"""Tests for the pydoc command-line interface."""
2468+
2469+
def test_show_doc(self):
2470+
rc, out, err = assert_python_ok('-m', 'pydoc', 'os')
2471+
self.assertEqual(rc, 0)
2472+
self.assertIn(b'os', out)
2473+
self.assertEqual(err, b'')
2474+
2475+
def test_show_doc_builtin(self):
2476+
rc, out, err = assert_python_ok('-m', 'pydoc', 'list')
2477+
self.assertEqual(rc, 0)
2478+
self.assertIn(b'list', out)
2479+
self.assertEqual(err, b'')
2480+
2481+
def test_show_doc_keyword(self):
2482+
rc, out, err = assert_python_ok('-m', 'pydoc', 'for')
2483+
self.assertEqual(rc, 0)
2484+
self.assertIn(b'for', out)
2485+
self.assertEqual(err, b'')
2486+
2487+
def test_keyword_search(self):
2488+
rc, out, err = assert_python_ok('-m', 'pydoc', '-k', 'thread')
2489+
self.assertEqual(rc, 0)
2490+
# Either matching modules are listed or no documentation is found.
2491+
self.assertTrue(
2492+
b'thread' in out.lower() or
2493+
b'no Python documentation found' in out,
2494+
out
2495+
)
2496+
self.assertEqual(err, b'')
2497+
2498+
def test_write_html(self):
2499+
with os_helper.temp_dir() as tmpdir:
2500+
rc, out, err = assert_python_ok('-m', 'pydoc', '-w', 'os',
2501+
__cwd=tmpdir)
2502+
self.assertEqual(rc, 0)
2503+
self.assertIn(b'wrote os.html', out.lower())
2504+
self.assertEqual(err, b'')
2505+
self.assertTrue(os.path.exists(os.path.join(tmpdir, 'os.html')))
2506+
2507+
def test_nonexistent_module(self):
2508+
rc, out, err = assert_python_failure('-m', 'pydoc', 'nonexistent_module')
2509+
self.assertEqual(rc, 1)
2510+
self.assertIn(b'nonexistent_module', out)
2511+
self.assertEqual(err, b'')
2512+
2513+
def test_no_args(self):
2514+
rc, out, err = assert_python_failure('-m', 'pydoc')
2515+
self.assertEqual(rc, 1)
2516+
self.assertIn(b'pydoc - the Python documentation tool', out)
2517+
self.assertEqual(err, b'')
2518+
2519+
def test_invalid_flag(self):
2520+
rc, out, err = assert_python_failure('-m', 'pydoc', '--xyz')
2521+
self.assertEqual(rc, 1)
2522+
self.assertIn(b'pydoc - the Python documentation tool', out)
2523+
self.assertEqual(err, b'')
2524+
2525+
24652526
def setUpModule():
24662527
thread_info = threading_helper.threading_setup()
24672528
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)

0 commit comments

Comments
 (0)