@@ -2175,10 +2175,47 @@ def test_url_requests(self):
21752175
21762176
21772177class TestHelper (unittest .TestCase ):
2178+ def mock_interactive_session (self , inputs ):
2179+ """
2180+ Given a list of inputs, run an interactive help session. Returns a string
2181+ of what would be shown on screen.
2182+ """
2183+ input_iter = iter (inputs )
2184+
2185+ def mock_getline (prompt ):
2186+ output .write (prompt )
2187+ next_input = next (input_iter )
2188+ output .write (next_input + os .linesep )
2189+ return next_input
2190+
2191+ with captured_stdout () as output :
2192+ helper = pydoc .Helper (output = output )
2193+ with unittest .mock .patch .object (helper , "getline" , mock_getline ):
2194+ helper .interact ()
2195+
2196+ # handle different line endings across platforms consistently
2197+ return output .getvalue ().strip ().splitlines (keepends = False )
2198+
21782199 def test_keywords (self ):
21792200 self .assertEqual (sorted (pydoc .Helper .keywords ),
21802201 sorted (keyword .kwlist ))
21812202
2203+ def test_interact_empty_line_continues (self ):
2204+ # gh-138568: test pressing Enter without input should continue in help session
2205+ self .assertEqual (
2206+ self .mock_interactive_session (["" , " " , "quit" ]),
2207+ ["help> " , "help> " , "help> quit" ],
2208+ )
2209+
2210+ def test_interact_quit_commands_exit (self ):
2211+ quit_commands = ["quit" , "q" , "exit" ]
2212+ for quit_cmd in quit_commands :
2213+ with self .subTest (quit_command = quit_cmd ):
2214+ self .assertEqual (
2215+ self .mock_interactive_session ([quit_cmd ]),
2216+ [f"help> { quit_cmd } " ],
2217+ )
2218+
21822219
21832220class PydocWithMetaClasses (unittest .TestCase ):
21842221 def tearDown (self ):
0 commit comments