summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBader Zaidan <bader@zaidan.pw>2022-03-17 23:37:52 (GMT)
committerGitHub <noreply@github.com>2022-03-17 23:37:52 (GMT)
commita0db11b10fca0fee6bb2b8d6277e266bad8c0fdb (patch)
tree156a2aa456e37ceaa2eed93c5baf03f19c912829
parentac8308d3eaf2526318c1bbf13d4a214fd24605d2 (diff)
downloadcpython-a0db11b10fca0fee6bb2b8d6277e266bad8c0fdb.zip
cpython-a0db11b10fca0fee6bb2b8d6277e266bad8c0fdb.tar.gz
cpython-a0db11b10fca0fee6bb2b8d6277e266bad8c0fdb.tar.bz2
bpo-46421: Fix unittest filename evaluation when called as a module (GH-30654)
-rw-r--r--Lib/test/test_cmd_line.py11
-rw-r--r--Lib/unittest/main.py2
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2022-01-18-01-29-38.bpo-46421.9LdmNr.rst3
4 files changed, 16 insertions, 1 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 1521b5b..84eab71 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -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
diff --git a/Lib/unittest/main.py b/Lib/unittest/main.py
index cb8f1f3..046fbd3 100644
--- a/Lib/unittest/main.py
+++ b/Lib/unittest/main.py
@@ -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):
diff --git a/Misc/ACKS b/Misc/ACKS
index 00194f4..895813e 100644
--- 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
index 0000000..03ff27f
--- /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.