]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46421: Fix unittest filename evaluation when called as a module (GH-30654)
authorBader Zaidan <bader@zaidan.pw>
Thu, 17 Mar 2022 23:37:52 +0000 (00:37 +0100)
committerGitHub <noreply@github.com>
Thu, 17 Mar 2022 23:37:52 +0000 (16:37 -0700)
Lib/test/test_cmd_line.py
Lib/unittest/main.py
Misc/ACKS
Misc/NEWS.d/next/Library/2022-01-18-01-29-38.bpo-46421.9LdmNr.rst [new file with mode: 0644]

index 1521b5b50ca249351ae5867b5faa009819c690a7..84eab71f9770149355a1be5161330a09abce3a1b 100644 (file)
@@ -166,6 +166,17 @@ class CmdLineTest(unittest.TestCase):
         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
index cb8f1f302801dc0ffda14daf0ad51f7b85b4de18..046fbd3a45dcf8e621cf2b5b15af1357597d5efe 100644 (file)
@@ -40,7 +40,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):
index 00194f40f074af51e67847285ba954a0df33a350..895813e8fc208a775ecedd7c768e34e26a84e488 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1994,6 +1994,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 (file)
index 0000000..03ff27f
--- /dev/null
@@ -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.