Skip to content

Commit 0b5f99a

Browse files
bpo-46421: Fix unittest filename evaluation when called as a module (GH-30654)
(cherry picked from commit a0db11b) Co-authored-by: Bader Zaidan <bader@zaidan.pw>
1 parent ba76f90 commit 0b5f99a

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

Lib/test/test_cmd_line.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ def test_run_module_bug1764407(self):
137137
self.assertTrue(data.find(b'1 loop') != -1)
138138
self.assertTrue(data.find(b'__main__.Timer') != -1)
139139

140+
def test_relativedir_bug46421(self):
141+
# Test `python -m unittest` with a relative directory beginning with ./
142+
# Note: We have to switch to the project's top module's directory, as per
143+
# the python unittest wiki. We will switch back when we are done.
144+
defaultwd = os.getcwd()
145+
projectlibpath = os.path.dirname(__file__).removesuffix("test")
146+
with os_helper.change_cwd(projectlibpath):
147+
# Testing with and without ./
148+
assert_python_ok('-m', 'unittest', "test/test_longexp.py")
149+
assert_python_ok('-m', 'unittest', "./test/test_longexp.py")
150+
140151
def test_run_code(self):
141152
# Test expected operation of the '-c' switch
142153
# Switch needs an argument

Lib/unittest/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _convert_name(name):
3939
name = rel_path
4040
# on Windows both '\' and '/' are used as path
4141
# separators. Better to replace both than rely on os.path.sep
42-
return name[:-3].replace('\\', '.').replace('/', '.')
42+
return os.path.normpath(name)[:-3].replace('\\', '.').replace('/', '.')
4343
return name
4444

4545
def _convert_names(names):

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,7 @@ Masazumi Yoshikawa
19831983
Arnaud Ysmal
19841984
Bernard Yue
19851985
Moshe Zadka
1986+
Bader Zaidan
19861987
Elias Zamaria
19871988
Milan Zamazal
19881989
Artur Zaprzala
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a unittest issue where if the command was invoked as ``python -m
2+
unittest`` and the filename(s) began with a dot (.), a ``ValueError`` is
3+
returned.

0 commit comments

Comments
 (0)