2022-01-16 08:24:05 +08:00
|
|
|
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
|
2019-03-31 06:31:42 +08:00
|
|
|
(b'', 0, ''),
|
2015-01-05 05:06:21 +08:00
|
|
|
# Acceptable
|
2019-03-31 06:31:42 +08:00
|
|
|
(b'"foo"', 0, ''),
|
2015-02-06 11:58:20 +08:00
|
|
|
# Docstring after code
|
2015-01-05 05:06:21 +08:00
|
|
|
(
|
2019-03-31 06:31:42 +08:00
|
|
|
b'from __future__ import unicode_literals\n'
|
|
|
|
b'"foo"\n',
|
2015-01-05 05:06:21 +08:00
|
|
|
1,
|
2022-04-07 04:55:26 +08:00
|
|
|
'{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
|
|
|
|
(
|
2019-03-31 06:31:42 +08:00
|
|
|
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,
|
2022-04-07 04:55:26 +08:00
|
|
|
'{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
|
|
|
|
(
|
2019-03-31 06:31:42 +08:00
|
|
|
b'import os\n'
|
|
|
|
b'import sys\n'
|
|
|
|
b'"docstring"\n',
|
2015-01-05 05:06:21 +08:00
|
|
|
1,
|
2022-04-07 04:55:26 +08:00
|
|
|
'{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.
|
2019-03-31 06:31:42 +08:00
|
|
|
(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')
|
2019-03-31 06:31:42 +08:00
|
|
|
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))
|
2019-03-31 06:31:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|