From d86f51fb0f147c594c02a817d722e2e8128a5a2b Mon Sep 17 00:00:00 2001 From: Bader Zaidan Date: Fri, 18 Mar 2022 00:37:52 +0100 Subject: [PATCH 1/2] bpo-46421: Fix unittest filename evaluation when called as a module (GH-30654) (cherry picked from commit a0db11b10fca0fee6bb2b8d6277e266bad8c0fdb) Co-authored-by: Bader Zaidan --- Lib/test/test_cmd_line.py | 11 +++++++++++ Lib/unittest/main.py | 2 +- Misc/ACKS | 1 + .../Library/2022-01-18-01-29-38.bpo-46421.9LdmNr.rst | 3 +++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-01-18-01-29-38.bpo-46421.9LdmNr.rst diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 712d861c4d5a3f..b1b3776eaa1762 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -136,6 +136,17 @@ def test_run_module_bug1764407(self): self.assertTrue(data.find(b'1 loop') != -1) self.assertTrue(data.find(b'__main__.Timer') != -1) + def test_relativedir_bug46421(self): + # Test `python -m unittest` with a relative directory beginning with ./ + # Note: We have to switch to the project's top module's directory, as per + # the python unittest wiki. We will switch back when we are done. + defaultwd = os.getcwd() + projectlibpath = os.path.dirname(__file__).removesuffix("test") + with os_helper.change_cwd(projectlibpath): + # Testing with and without ./ + assert_python_ok('-m', 'unittest', "test/test_longexp.py") + assert_python_ok('-m', 'unittest', "./test/test_longexp.py") + def test_run_code(self): # Test expected operation of the '-c' switch # Switch needs an argument diff --git a/Lib/unittest/main.py b/Lib/unittest/main.py index e62469aa2a170f..88a188c545cd07 100644 --- a/Lib/unittest/main.py +++ b/Lib/unittest/main.py @@ -39,7 +39,7 @@ def _convert_name(name): name = rel_path # on Windows both '\' and '/' are used as path # separators. Better to replace both than rely on os.path.sep - return name[:-3].replace('\\', '.').replace('/', '.') + return os.path.normpath(name)[:-3].replace('\\', '.').replace('/', '.') return name def _convert_names(names): diff --git a/Misc/ACKS b/Misc/ACKS index 2a26fdcd16b439..d043195a7b550a 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1947,6 +1947,7 @@ Masazumi Yoshikawa Arnaud Ysmal Bernard Yue Moshe Zadka +Bader Zaidan Elias Zamaria Milan Zamazal Artur Zaprzala diff --git a/Misc/NEWS.d/next/Library/2022-01-18-01-29-38.bpo-46421.9LdmNr.rst b/Misc/NEWS.d/next/Library/2022-01-18-01-29-38.bpo-46421.9LdmNr.rst new file mode 100644 index 00000000000000..03ff27fd7d1a70 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-18-01-29-38.bpo-46421.9LdmNr.rst @@ -0,0 +1,3 @@ +Fix a unittest issue where if the command was invoked as ``python -m +unittest`` and the filename(s) began with a dot (.), a ``ValueError`` is +returned. From 45278f5566ae38adf7f572645b58182ab8069f29 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 17 Mar 2022 18:45:32 -0700 Subject: [PATCH 2/2] Update Lib/test/test_cmd_line.py --- Lib/test/test_cmd_line.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index b1b3776eaa1762..4b3e33c4fd3544 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -142,7 +142,7 @@ def test_relativedir_bug46421(self): # the python unittest wiki. We will switch back when we are done. defaultwd = os.getcwd() projectlibpath = os.path.dirname(__file__).removesuffix("test") - with os_helper.change_cwd(projectlibpath): + with support.change_cwd(projectlibpath): # Testing with and without ./ assert_python_ok('-m', 'unittest', "test/test_longexp.py") assert_python_ok('-m', 'unittest', "./test/test_longexp.py")