Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Allow --path to take a direct module path (#110)
Browse files Browse the repository at this point in the history
Allow --path to take a direct module path
  • Loading branch information
darrenburns authored Feb 1, 2020
2 parents 6d714d7 + 982114b commit de22ed4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
"contributions": [
"code"
]
},
{
"login": "onlyanegg",
"name": "Tyler Couto",
"avatar_url": "https://avatars0.githubusercontent.com/u/7731128?v=4",
"profile": "https://github.com/onlyanegg",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Ward
![](https://github.com/darrenburns/ward/workflows/Ward%20CI/badge.svg)
[![PyPI version](https://badge.fury.io/py/ward.svg)](https://badge.fury.io/py/ward)
[![Gitter](https://badges.gitter.im/python-ward/community.svg)](https://gitter.im/python-ward/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)<!-- ALL-CONTRIBUTORS-BADGE:END -->
[![Gitter](https://badges.gitter.im/python-ward/community.svg)](https://gitter.im/python-ward/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->[![All Contributors](https://img.shields.io/badge/all_contributors-7-orange.svg?style=flat-square)](#contributors-)<!-- ALL-CONTRIBUTORS-BADGE:END -->

See the full documentation and feature set [here](https://wardpy.com).

Expand Down Expand Up @@ -90,6 +90,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://www.indeliblebluepen.com"><img src="https://avatars2.githubusercontent.com/u/7471402?v=4" width="60px;" alt="Jason C. McDonald"/><br /><sub><b>Jason C. McDonald</b></sub></a><br /><a href="https://github.com/darrenburns/ward/commits?author=CodeMouse92" title="Code">💻</a> <a href="#ideas-CodeMouse92" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://github.com/AndydeCleyre"><img src="https://avatars3.githubusercontent.com/u/1787385?v=4" width="60px;" alt="Andy Kluger"/><br /><sub><b>Andy Kluger</b></sub></a><br /><a href="https://github.com/darrenburns/ward/commits?author=AndydeCleyre" title="Code">💻</a> <a href="#ideas-AndydeCleyre" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="https://forum.thebigmunch.me"><img src="https://avatars0.githubusercontent.com/u/118418?v=4" width="60px;" alt="thebigmunch"/><br /><sub><b>thebigmunch</b></sub></a><br /><a href="https://github.com/darrenburns/ward/commits?author=thebigmunch" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/onlyanegg"><img src="https://avatars0.githubusercontent.com/u/7731128?v=4" width="60px;" alt="Tyler Couto"/><br /><sub><b>Tyler Couto</b></sub></a><br /><a href="https://github.com/darrenburns/ward/commits?author=onlyanegg" title="Code">💻</a></td>
</tr>
</table>

Expand Down
28 changes: 19 additions & 9 deletions ward/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@ def get_info_for_modules(
# If multiple paths are specified, remove duplicates
paths = list(set(paths))

checked_dirs: Set[Path] = set(p for p in paths)
# Handle case where path points directly to modules
for path in paths:
if path.is_file():
# Ensure we only yield a module once if it's specified directly
# and also is part of a specified directory.
if not any(dir_path in path.parents for dir_path in paths if dir_path.is_dir()):
spec = importlib.util.spec_from_file_location(path.stem, path)
module = importlib.util.module_from_spec(spec)
yield module

# Check for modules at the root of the specified path (or paths)
for module in pkgutil.iter_modules([str(p) for p in paths]):
for module in pkgutil.iter_modules([str(p) for p in paths if p.is_dir()]):
if is_test_module(module):
yield module

# Now check for modules in every subdirectory
checked_dirs: Set[Path] = set(p for p in paths)
for p in paths:
for root, dirs, _ in os.walk(str(p)):
for dir_name in dirs:
dir_path = Path(root, dir_name)
# if we have seen this directory before, skip it
# if we have seen this path before, skip it
if dir_path not in checked_dirs:
checked_dirs.add(dir_path)
for module in pkgutil.iter_modules([str(dir_path)]):
Expand All @@ -45,12 +54,13 @@ def get_info_for_modules(

def load_modules(modules: Iterable[pkgutil.ModuleInfo]) -> Generator[Any, None, None]:
for m in modules:
file_finder: FileFinder = m.module_finder
spec: ModuleSpec = file_finder.find_spec(m.name)
mod = importlib.util.module_from_spec(spec)
if mod.__name__.startswith("test_"):
spec.loader.exec_module(mod)
yield mod
if hasattr(m, "module_finder"):
file_finder: FileFinder = m.module_finder
spec: ModuleSpec = file_finder.find_spec(m.name)
m = importlib.util.module_from_spec(spec)
if is_test_module_name(m.__name__):
m.__loader__.exec_module(m)
yield m


def get_tests_in_modules(
Expand Down

0 comments on commit de22ed4

Please sign in to comment.