Skip to content

unittest/discover: Reverse order of parent/top in sys.path. #872

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 1 commit into from
Jul 3, 2024
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
2 changes: 1 addition & 1 deletion python-stdlib/unittest-discover/manifest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
metadata(version="0.1.2")
metadata(version="0.1.3")

require("argparse")
require("fnmatch")
Expand Down
1 change: 1 addition & 0 deletions python-stdlib/unittest-discover/tests/sub/sub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
imported = True
13 changes: 13 additions & 0 deletions python-stdlib/unittest-discover/tests/sub/test_module_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
import unittest


class TestModuleImport(unittest.TestCase):
def test_ModuleImportPath(self):
try:
from sub.sub import imported
assert imported
except ImportError:
print("This test is intended to be run with unittest discover"
"from the unittest-discover/tests dir. sys.path:", sys.path)
raise
19 changes: 13 additions & 6 deletions python-stdlib/unittest-discover/unittest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
from fnmatch import fnmatch
from micropython import const

from unittest import TestRunner, TestResult, TestSuite
try:
from unittest import TestRunner, TestResult, TestSuite
except ImportError:
print("Error: This must be used from an installed copy of unittest-discover which will"
" also install base unittest module.")
raise


# Run a single test in a clean environment.
def _run_test_module(runner: TestRunner, module_name: str, *extra_paths: list[str]):
module_snapshot = {k: v for k, v in sys.modules.items()}
path_snapshot = sys.path[:]
try:
for path in reversed(extra_paths):
for path in extra_paths:
if path:
sys.path.insert(0, path)

module = __import__(module_name)
module = __import__(module_name, None, None, module_name)
suite = TestSuite(module_name)
suite._load_module(module)
return runner.run(suite)
Expand All @@ -36,16 +41,18 @@ def _run_all_in_dir(runner: TestRunner, path: str, pattern: str, top: str):
for fname, ftype, *_ in os.ilistdir(path):
if fname in ("..", "."):
continue
fpath = "/".join((path, fname))
if ftype == _DIR_TYPE:
result += _run_all_in_dir(
runner=runner,
path="/".join((path, fname)),
path=fpath,
pattern=pattern,
top=top,
)
if fnmatch(fname, pattern):
module_name = fname.rsplit(".", 1)[0]
result += _run_test_module(runner, module_name, path, top)
module_path = fpath.rsplit(".", 1)[0] # remove ext
module_path = module_path.replace("/", ".").strip(".")
result += _run_test_module(runner, module_path, top)
return result


Expand Down
Loading