Skip to content

bpo-46421: Fix unittest filename evaluation when called as a module #30654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ def test_run_module_bug1764407(self):
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
Expand Down
2 changes: 1 addition & 1 deletion Lib/unittest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,7 @@ Masazumi Yoshikawa
Arnaud Ysmal
Bernard Yue
Moshe Zadka
Bader Zaidan
Elias Zamaria
Milan Zamazal
Artur Zaprzala
Expand Down
Original file line number Diff line number Diff line change
@@ -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.