@@ -2467,6 +2467,41 @@ def test_error_produces_stderr(self, app) -> None:
24672467 _out , err = run_cmd (app , "greet" )
24682468 assert any ("error" in line .lower () or "usage" in line .lower () for line in err )
24692469
2470+ @pytest .mark .parametrize (
2471+ ("app_fixture" , "command" ),
2472+ [
2473+ ("app" , "greet" ),
2474+ ("app" , "flex" ),
2475+ ("subcmd_app" , "manage" ),
2476+ ("subcmd_app" , "manage add" ),
2477+ ("subcmd_app" , "manage member add" ),
2478+ ],
2479+ )
2480+ @pytest .mark .parametrize ("destination" , ["stdout" , ">" , ">>" ])
2481+ def test_help_respects_output_destination (self , request , capsys , tmp_path , app_fixture , command , destination ) -> None :
2482+ """Help follows app.stdout, including file redirection, without leaking to sys.stdout."""
2483+ app = request .getfixturevalue (app_fixture )
2484+ original_stdout = app .stdout
2485+ help_file = tmp_path / "help.txt"
2486+ line = f"{ command } --help"
2487+ if destination != "stdout" :
2488+ help_file .write_text ("existing\n " , encoding = "utf-8" )
2489+ line += f' { destination } "{ help_file } "'
2490+
2491+ out , err = run_cmd (app , line )
2492+
2493+ assert app .stdout is original_stdout
2494+ assert not err
2495+ if destination == "stdout" :
2496+ help_text = "\n " .join (out )
2497+ else :
2498+ assert not out
2499+ help_text = help_file .read_text (encoding = "utf-8" )
2500+ assert help_text .startswith ("existing\n " ) == (destination == ">>" )
2501+ assert f"Usage: { command } " in help_text
2502+ assert "--help" in help_text
2503+ assert capsys .readouterr () == ("" , "" )
2504+
24702505 def test_no_args_raises_type_error (self , app ) -> None :
24712506 with pytest .raises (TypeError , match = "Expected arguments" ):
24722507 app .do_greet ()
0 commit comments