]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-46421: Fix unittest filename evaluation when called as a module (GH-30654...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 18 Mar 2022 03:24:59 +0000 (20:24 -0700)
committerGitHub <noreply@github.com>
Fri, 18 Mar 2022 03:24:59 +0000 (20:24 -0700)
(cherry picked from commit a0db11b10fca0fee6bb2b8d6277e266bad8c0fdb)

Co-authored-by: Bader Zaidan <bader@zaidan.pw>
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 712d861c4d5a3f95fedfe8836c3e5dfb302d0656..4b3e33c4fd3544b32dafbea134b923b64220f14c 100644 (file)
@@ -136,6 +136,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 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")
+
     def test_run_code(self):
         # Test expected operation of the '-c' switch
         # Switch needs an argument
index e62469aa2a170f2bc1894adc232cf46cdeb2b4d9..88a188c545cd073acef54789fe07ca9875c5cf27 100644 (file)
@@ -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):
index 2a26fdcd16b439d47944d2f481d59c33d1e2d862..d043195a7b550ac1ec52a2c211880f9844f036a0 100644 (file)
--- 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 (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.