mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
Add missing init hook
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-init-py-trap
This commit is contained in:
parent
31853d6c43
commit
513a24cde9
5 changed files with 103 additions and 0 deletions
62
tests/check_missing_inits_test.py
Normal file
62
tests/check_missing_inits_test.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue