@@ -2188,10 +2188,47 @@ def test_url_requests(self):
21882188
21892189
21902190class TestHelper (unittest .TestCase ):
2191+ def mock_interactive_session (self , inputs ):
2192+ """
2193+ Given a list of inputs, run an interactive help session. Returns a string
2194+ of what would be shown on screen.
2195+ """
2196+ input_iter = iter (inputs )
2197+
2198+ def mock_getline (prompt ):
2199+ output .write (prompt )
2200+ next_input = next (input_iter )
2201+ output .write (next_input + os .linesep )
2202+ return next_input
2203+
2204+ with captured_stdout () as output :
2205+ helper = pydoc .Helper (output = output )
2206+ with unittest .mock .patch .object (helper , "getline" , mock_getline ):
2207+ helper .interact ()
2208+
2209+ # handle different line endings across platforms consistently
2210+ return output .getvalue ().strip ().splitlines (keepends = False )
2211+
21912212 def test_keywords (self ):
21922213 self .assertEqual (sorted (pydoc .Helper .keywords ),
21932214 sorted (keyword .kwlist ))
21942215
2216+ def test_interact_empty_line_continues (self ):
2217+ # gh-138568: test pressing Enter without input should continue in help session
2218+ self .assertEqual (
2219+ self .mock_interactive_session (["" , " " , "quit" ]),
2220+ ["help> " , "help> " , "help> quit" ],
2221+ )
2222+
2223+ def test_interact_quit_commands_exit (self ):
2224+ quit_commands = ["quit" , "q" , "exit" ]
2225+ for quit_cmd in quit_commands :
2226+ with self .subTest (quit_command = quit_cmd ):
2227+ self .assertEqual (
2228+ self .mock_interactive_session ([quit_cmd ]),
2229+ [f"help> { quit_cmd } " ],
2230+ )
2231+
21952232
21962233class PydocWithMetaClasses (unittest .TestCase ):
21972234 def tearDown (self ):
0 commit comments