mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
Add check-docstring-first hook.
This commit is contained in:
parent
7a5a46a1dc
commit
53f1dc0163
5 changed files with 142 additions and 4 deletions
67
tests/check_docstring_first_test.py
Normal file
67
tests/check_docstring_first_test.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import io
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.check_docstring_first import check_docstring_first
|
||||
from pre_commit_hooks.check_docstring_first import main
|
||||
|
||||
|
||||
# Contents, expected, expected_output
|
||||
TESTS = (
|
||||
# trivial
|
||||
('', 0, ''),
|
||||
# Acceptable
|
||||
('"foo"', 0, ''),
|
||||
# Docstrin after code
|
||||
(
|
||||
'from __future__ import unicode_literals\n'
|
||||
'"foo"\n',
|
||||
1,
|
||||
'{filename}:2 Module docstring appears after code '
|
||||
'(code seen on line 1).\n'
|
||||
),
|
||||
# Test double docstring
|
||||
(
|
||||
'"The real docstring"\n'
|
||||
'from __future__ import absolute_import\n'
|
||||
'"fake docstring"\n',
|
||||
1,
|
||||
'{filename}:3 Multiple module docstrings '
|
||||
'(first docstring on line 1).\n'
|
||||
),
|
||||
# Test multiple lines of code above
|
||||
(
|
||||
'import os\n'
|
||||
'import sys\n'
|
||||
'"docstring"\n',
|
||||
1,
|
||||
'{filename}:3 Module docstring appears after code '
|
||||
'(code seen on line 1).\n',
|
||||
),
|
||||
# String literals in expressions are ok.
|
||||
('x = "foo"\n', 0, ''),
|
||||
)
|
||||
|
||||
|
||||
all_tests = pytest.mark.parametrize(
|
||||
('contents', 'expected', 'expected_out'), TESTS,
|
||||
)
|
||||
|
||||
|
||||
@all_tests
|
||||
def test_unit(capsys, contents, expected, expected_out):
|
||||
assert check_docstring_first(contents) == expected
|
||||
assert capsys.readouterr()[0] == expected_out.format(filename='<unknown>')
|
||||
|
||||
|
||||
@all_tests
|
||||
def test_integration(tmpdir, capsys, contents, expected, expected_out):
|
||||
tmpfilename = tmpdir.join('test.py').strpath
|
||||
with io.open(tmpfilename, 'w') as tmpfile:
|
||||
tmpfile.write(contents)
|
||||
|
||||
assert main([tmpfilename]) == expected
|
||||
assert capsys.readouterr()[0] == expected_out.format(filename=tmpfilename)
|
||||
Loading…
Add table
Add a link
Reference in a new issue