From 86c637eb2d386ece494247083919988d36522512 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Tue, 16 Apr 2024 12:26:41 +0300 Subject: [PATCH 1/2] Catch stderr --- Lib/test/test_webbrowser.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py index 849665294c3dfa..c63f330074ba2e 100644 --- a/Lib/test/test_webbrowser.py +++ b/Lib/test/test_webbrowser.py @@ -461,11 +461,25 @@ def test_parse_args_error(self): "https://example.com --new-window --new-tab", "https://example.com -n --new-tab", "https://example.com --new-window -t", - # Ensure ambiguous shortening fails - "https://example.com --new", ]: + with support.captured_stderr() as stderr: + with self.assertRaises(SystemExit): + webbrowser.parse_args(shlex.split(command)) + self.assertEqual( + stderr.getvalue(), + "usage: __main__.py [-h] [-n | -t] url\n" + "__main__.py: error: argument -t/--new-tab: not allowed with argument -n/--new-window\n" + ) + + # Ensure ambiguous shortening fails + with support.captured_stderr() as stderr: with self.assertRaises(SystemExit): - webbrowser.parse_args(shlex.split(command)) + webbrowser.parse_args(shlex.split("https://example.com --new")) + self.assertEqual( + stderr.getvalue(), + "usage: __main__.py [-h] [-n | -t] url\n" + "__main__.py: error: ambiguous option: --new could match --new-window, --new-tab\n" + ) def test_main(self): for command, expected_url, expected_new_win in [ From 864aee512dbefb1a079a7807f1f41b0ec40cbc79 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 17 Apr 2024 07:55:00 +0300 Subject: [PATCH 2/2] Replace assertEquals to assertIn --- Lib/test/test_webbrowser.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py index c63f330074ba2e..ae8d776e8413ff 100644 --- a/Lib/test/test_webbrowser.py +++ b/Lib/test/test_webbrowser.py @@ -465,20 +465,18 @@ def test_parse_args_error(self): with support.captured_stderr() as stderr: with self.assertRaises(SystemExit): webbrowser.parse_args(shlex.split(command)) - self.assertEqual( + self.assertIn( + 'error: argument -t/--new-tab: not allowed with argument -n/--new-window', stderr.getvalue(), - "usage: __main__.py [-h] [-n | -t] url\n" - "__main__.py: error: argument -t/--new-tab: not allowed with argument -n/--new-window\n" ) # Ensure ambiguous shortening fails with support.captured_stderr() as stderr: with self.assertRaises(SystemExit): webbrowser.parse_args(shlex.split("https://example.com --new")) - self.assertEqual( - stderr.getvalue(), - "usage: __main__.py [-h] [-n | -t] url\n" - "__main__.py: error: ambiguous option: --new could match --new-window, --new-tab\n" + self.assertIn( + 'error: ambiguous option: --new could match --new-window, --new-tab', + stderr.getvalue() ) def test_main(self):