mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 19:46:54 +00:00
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-init-py-trap
62 lines
1.4 KiB
Python
62 lines
1.4 KiB
Python
import os
|
|
|
|
import pytest
|
|
|
|
from pre_commit_hooks.check_missing_inits import main
|
|
|
|
|
|
@pytest.fixture
|
|
def filepaths(tmpdir):
|
|
dirs = ['a', 'b']
|
|
files = ['a.py', 'b.py', '__init__.py']
|
|
paths = []
|
|
for d in dirs:
|
|
directory = tmpdir / d
|
|
os.mkdir(str(directory))
|
|
for f in files:
|
|
path = (directory / f)
|
|
paths.append(str(path))
|
|
# Nested directory
|
|
if d == 'b':
|
|
directory = directory / 'c'
|
|
os.mkdir(str(directory))
|
|
for f in files:
|
|
path = (directory / f)
|
|
paths.append(str(path))
|
|
return paths
|
|
|
|
|
|
def test_has_inits(filepaths):
|
|
for f in filepaths:
|
|
with open(f, 'w'):
|
|
pass
|
|
|
|
assert main(argv=filepaths) == 0
|
|
|
|
|
|
def test_missing_inits(filepaths):
|
|
remove = ''
|
|
for f in filepaths:
|
|
# Remove the init py file from the b directory
|
|
if os.path.join('b', '__init__.py') in f:
|
|
remove = str(f)
|
|
continue
|
|
with open(f, 'w'):
|
|
pass
|
|
filepaths.remove(remove)
|
|
|
|
assert main(argv=filepaths) == 1
|
|
|
|
|
|
def test_nested_dirs_missing_inits(filepaths):
|
|
remove = ''
|
|
for f in filepaths:
|
|
# Remove the init py file from a nested directory
|
|
if os.path.join('b', 'c', '__init__.py') in f:
|
|
remove = str(f)
|
|
continue
|
|
with open(f, 'w'):
|
|
pass
|
|
filepaths.remove(remove)
|
|
|
|
assert main(argv=filepaths) == 1
|