From 074d6c6442fe28b65cdfc2607b1fa9aa1b35a372 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sun, 2 Jul 2023 23:29:05 +0300 Subject: [PATCH 1/4] gh-102541: Add test case for `Helper.help('module')` for non-existent module --- Lib/test/test_pydoc.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index 115ffd3c29d4d6..568c18db5c81a3 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -24,7 +24,9 @@ from urllib.request import urlopen, urlcleanup from test.support import import_helper from test.support import os_helper -from test.support.script_helper import assert_python_ok, assert_python_failure +from test.support.script_helper import (assert_python_ok, kill_python, + assert_python_failure, spawn_python) + from test.support import threading_helper from test.support import (reap_children, captured_output, captured_stdout, captured_stderr, is_emscripten, is_wasi, @@ -631,6 +633,17 @@ def test_builtin_on_metaclasses(self): # Testing that the subclasses section does not appear self.assertNotIn('Built-in subclasses', text) + def test_fail_help_cli(self): + fd, name = tempfile.mkstemp() + self.addCleanup(os_helper.unlink, name) + with open(fd, "w") as f: + f.write("help('abd')") + with spawn_python(name) as proc: + out, _ = proc.communicate() + out = out.decode() + expected = missing_pattern % "abd" + self.assertEqual(expected, out.strip()) + def test_fail_help_output_redirect(self): with StringIO() as buf: helper = pydoc.Helper(output=buf) From 2c7e16b478da8af5df180e9952daa0e756c35f76 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sun, 2 Jul 2023 23:30:04 +0300 Subject: [PATCH 2/4] Remove redundant import --- Lib/test/test_pydoc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index 568c18db5c81a3..aa48a9ef09b1e6 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -24,9 +24,8 @@ from urllib.request import urlopen, urlcleanup from test.support import import_helper from test.support import os_helper -from test.support.script_helper import (assert_python_ok, kill_python, +from test.support.script_helper import (assert_python_ok, assert_python_failure, spawn_python) - from test.support import threading_helper from test.support import (reap_children, captured_output, captured_stdout, captured_stderr, is_emscripten, is_wasi, From 6cb8bbb0debe4b5391fb278144240799608d4e99 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Sun, 2 Jul 2023 23:46:24 +0300 Subject: [PATCH 3/4] Rewrite test using help CLI --- Lib/test/test_pydoc.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index aa48a9ef09b1e6..b732819204d96a 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -636,11 +636,15 @@ def test_fail_help_cli(self): fd, name = tempfile.mkstemp() self.addCleanup(os_helper.unlink, name) with open(fd, "w") as f: - f.write("help('abd')") + f.write("help()") with spawn_python(name) as proc: - out, _ = proc.communicate() - out = out.decode() - expected = missing_pattern % "abd" + out, _ = proc.communicate(b"abd") + pretty_string = out.decode().replace("help> ", "") + pretty_expected = missing_pattern.replace(os.linesep, "") + lines = pretty_string.splitlines() + last_lines = lines[-10:-6] + out = "".join(last_lines) + expected = pretty_expected % "abd" self.assertEqual(expected, out.strip()) def test_fail_help_output_redirect(self): From fa73458d3ab295da30bc5aaf15ea91ecdb98fd9a Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Tue, 11 Jul 2023 12:48:00 -0400 Subject: [PATCH 4/4] Simplify the new test --- Lib/test/test_pydoc.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py index b732819204d96a..ddb5187f90da9b 100644 --- a/Lib/test/test_pydoc.py +++ b/Lib/test/test_pydoc.py @@ -633,19 +633,12 @@ def test_builtin_on_metaclasses(self): self.assertNotIn('Built-in subclasses', text) def test_fail_help_cli(self): - fd, name = tempfile.mkstemp() - self.addCleanup(os_helper.unlink, name) - with open(fd, "w") as f: - f.write("help()") - with spawn_python(name) as proc: + elines = (missing_pattern % 'abd').splitlines() + with spawn_python("-c" "help()") as proc: out, _ = proc.communicate(b"abd") - pretty_string = out.decode().replace("help> ", "") - pretty_expected = missing_pattern.replace(os.linesep, "") - lines = pretty_string.splitlines() - last_lines = lines[-10:-6] - out = "".join(last_lines) - expected = pretty_expected % "abd" - self.assertEqual(expected, out.strip()) + olines = out.decode().splitlines()[-9:-6] + olines[0] = olines[0].removeprefix('help> ') + self.assertEqual(elines, olines) def test_fail_help_output_redirect(self): with StringIO() as buf: