Skip to content

Commit

Permalink
default to adding __init__.py files to directories
Browse files Browse the repository at this point in the history
Python unittest discover still relies on __init__.py files and doesn't
support namespace packages. By default we can create these but allow for
their suppression if using other test frameworks.
  • Loading branch information
Matt Katz authored and ryneeverett committed Aug 5, 2020
1 parent 2d56225 commit c27b4b8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion mkcodes.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ def get_files(inputs):
elif path.suffix in markdown_extensions:
yield path, path.parent

def add_inits_to_dir(path):
"""Recursively add __init__.py files to a directory
This compensates for https://bugs.python.org/issue23882 and https://bugs.python.org/issue35617
"""
for child in path.rglob('*'):
if child.is_dir():
(child / '__init__.py').touch()



@click.command()
@click.argument(
Expand All @@ -88,7 +97,9 @@ def get_files(inputs):
help='Github-flavored fence blocks or pure markdown.')
@click.option('--safe/--unsafe', default=True,
help='Allow code blocks without language hints.')
def main(inputs, output, github, safe):
@click.option('--package-python', default=True,
help='Add __init__.py files to python output to aid in test discovery')
def main(inputs, output, github, safe, package_python):
collect_codeblocks = github_codeblocks if github else markdown_codeblocks
outputbasedir = Path(output).parent
outputbasename = Path(output).name
Expand All @@ -104,4 +115,7 @@ def main(inputs, output, github, safe):

outputfilename.parent.mkdir(parents=True, exist_ok=True)
outputfilename.write_text('\n\n'.join(codeblocks))
if package_python:
add_inits_to_dir(outputbasedir)


0 comments on commit c27b4b8

Please sign in to comment.