pre-commit-hooks/tests/check_docstring_first_test.py

70 lines
1.8 KiB
Python
Raw Normal View History

from __future__ import annotations
2015-01-05 05:06:21 +08:00
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
(b'', 0, ''),
2015-01-05 05:06:21 +08:00
# Acceptable
(b'"foo"', 0, ''),
# Docstring after code
2015-01-05 05:06:21 +08:00
(
b'from __future__ import unicode_literals\n'
b'"foo"\n',
2015-01-05 05:06:21 +08:00
1,
'{filename}:2: Module docstring appears after code '
2017-07-13 09:35:24 +08:00
'(code seen on line 1).\n',
2015-01-05 05:06:21 +08:00
),
# Test double docstring
(
b'"The real docstring"\n'
b'from __future__ import absolute_import\n'
b'"fake docstring"\n',
2015-01-05 05:06:21 +08:00
1,
'{filename}:3: Multiple module docstrings '
2017-07-13 09:35:24 +08:00
'(first docstring on line 1).\n',
2015-01-05 05:06:21 +08:00
),
# Test multiple lines of code above
(
b'import os\n'
b'import sys\n'
b'"docstring"\n',
2015-01-05 05:06:21 +08:00
1,
'{filename}:3: Module docstring appears after code '
2015-01-05 05:06:21 +08:00
'(code seen on line 1).\n',
),
# String literals in expressions are ok.
(b'x = "foo"\n', 0, ''),
2015-01-05 05:06:21 +08:00
)
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):
2016-05-28 05:09:50 +08:00
f = tmpdir.join('test.py')
f.write_binary(contents)
2020-05-21 00:07:45 +08:00
assert main([str(f)]) == expected
assert capsys.readouterr()[0] == expected_out.format(filename=str(f))
def test_arbitrary_encoding(tmpdir):
f = tmpdir.join('f.py')
contents = '# -*- coding: cp1252\nx = "£"'.encode('cp1252')
f.write_binary(contents)
2020-05-21 00:07:45 +08:00
assert main([str(f)]) == 0