Skip to content

Commit 86169ce

Browse files
miss-islingtonBaderSZ
authored andcommitted
[3.9] bpo-46421: Fix unittest filename evaluation when called as a module (pythonGH-30654) (pythonGH-31970)
(cherry picked from commit a0db11b) Co-authored-by: Bader Zaidan <[email protected]>
1 parent fbc8152 commit 86169ce

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
@@ -136,6 +136,17 @@ def test_run_module_bug1764407(self):
136136
self.assertTrue(data.find(b'1 loop') != -1)
137137
self.assertTrue(data.find(b'__main__.Timer') != -1)
138138

139+
def test_relativedir_bug46421(self):
140+
# Test `python -m unittest` with a relative directory beginning with ./
141+
# Note: We have to switch to the project's top module's directory, as per
142+
# the python unittest wiki. We will switch back when we are done.
143+
defaultwd = os.getcwd()
144+
projectlibpath = os.path.dirname(__file__).removesuffix("test")
145+
with support.change_cwd(projectlibpath):
146+
# Testing with and without ./
147+
assert_python_ok('-m', 'unittest', "test/test_longexp.py")
148+
assert_python_ok('-m', 'unittest', "./test/test_longexp.py")
149+
139150
def test_run_code(self):
140151
# Test expected operation of the '-c' switch
141152
# 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
@@ -1947,6 +1947,7 @@ Masazumi Yoshikawa
19471947
Arnaud Ysmal
19481948
Bernard Yue
19491949
Moshe Zadka
1950+
Bader Zaidan
19501951
Elias Zamaria
19511952
Milan Zamazal
19521953
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)