@@ -2147,10 +2147,47 @@ def test_url_requests(self):
21472147
21482148
21492149class TestHelper (unittest .TestCase ):
2150+ def mock_interactive_session (self , inputs ):
2151+ """
2152+ Given a list of inputs, run an interactive help session. Returns a string
2153+ of what would be shown on screen.
2154+ """
2155+ input_iter = iter (inputs )
2156+
2157+ def mock_getline (prompt ):
2158+ output .write (prompt )
2159+ next_input = next (input_iter )
2160+ output .write (next_input + os .linesep )
2161+ return next_input
2162+
2163+ with captured_stdout () as output :
2164+ helper = pydoc .Helper (output = output )
2165+ with unittest .mock .patch .object (helper , "getline" , mock_getline ):
2166+ helper .interact ()
2167+
2168+ # handle different line endings across platforms consistently
2169+ return output .getvalue ().strip ().splitlines (keepends = False )
2170+
21502171 def test_keywords (self ):
21512172 self .assertEqual (sorted (pydoc .Helper .keywords ),
21522173 sorted (keyword .kwlist ))
21532174
2175+ def test_interact_empty_line_continues (self ):
2176+ # gh-138568: test pressing Enter without input should continue in help session
2177+ self .assertEqual (
2178+ self .mock_interactive_session (["" , " " , "quit" ]),
2179+ ["help> " , "help> " , "help> quit" ],
2180+ )
2181+
2182+ def test_interact_quit_commands_exit (self ):
2183+ quit_commands = ["quit" , "q" , "exit" ]
2184+ for quit_cmd in quit_commands :
2185+ with self .subTest (quit_command = quit_cmd ):
2186+ self .assertEqual (
2187+ self .mock_interactive_session ([quit_cmd ]),
2188+ [f"help> { quit_cmd } " ],
2189+ )
2190+
21542191
21552192class PydocWithMetaClasses (unittest .TestCase ):
21562193 def tearDown (self ):
0 commit comments