blob: 8bafae8287d3112b0dd0904ef9e75a62cef92c8f [file] [log] [blame]
Anthony Sottile8f615292022-01-15 19:24:05 -05001from __future__ import annotations
2
Anthony Sottile53f1dc02015-01-04 13:06:21 -08003import pytest
4
5from pre_commit_hooks.check_docstring_first import check_docstring_first
6from pre_commit_hooks.check_docstring_first import main
7
8
9# Contents, expected, expected_output
10TESTS = (
11 # trivial
Anthony Sottile2f6a2512019-03-30 15:31:42 -070012 (b'', 0, ''),
Anthony Sottile53f1dc02015-01-04 13:06:21 -080013 # Acceptable
Anthony Sottile2f6a2512019-03-30 15:31:42 -070014 (b'"foo"', 0, ''),
Ken Struys12f02df2015-02-05 19:58:20 -080015 # Docstring after code
Anthony Sottile53f1dc02015-01-04 13:06:21 -080016 (
Anthony Sottile2f6a2512019-03-30 15:31:42 -070017 b'from __future__ import unicode_literals\n'
18 b'"foo"\n',
Anthony Sottile53f1dc02015-01-04 13:06:21 -080019 1,
Anthony Sottileb13ff9b2022-04-06 16:55:26 -040020 '{filename}:2: Module docstring appears after code '
Anthony Sottile2a902e02017-07-12 18:35:24 -070021 '(code seen on line 1).\n',
Anthony Sottile53f1dc02015-01-04 13:06:21 -080022 ),
23 # Test double docstring
24 (
Anthony Sottile2f6a2512019-03-30 15:31:42 -070025 b'"The real docstring"\n'
26 b'from __future__ import absolute_import\n'
27 b'"fake docstring"\n',
Anthony Sottile53f1dc02015-01-04 13:06:21 -080028 1,
Anthony Sottileb13ff9b2022-04-06 16:55:26 -040029 '{filename}:3: Multiple module docstrings '
Anthony Sottile2a902e02017-07-12 18:35:24 -070030 '(first docstring on line 1).\n',
Anthony Sottile53f1dc02015-01-04 13:06:21 -080031 ),
32 # Test multiple lines of code above
33 (
Anthony Sottile2f6a2512019-03-30 15:31:42 -070034 b'import os\n'
35 b'import sys\n'
36 b'"docstring"\n',
Anthony Sottile53f1dc02015-01-04 13:06:21 -080037 1,
Anthony Sottileb13ff9b2022-04-06 16:55:26 -040038 '{filename}:3: Module docstring appears after code '
Anthony Sottile53f1dc02015-01-04 13:06:21 -080039 '(code seen on line 1).\n',
40 ),
41 # String literals in expressions are ok.
Anthony Sottile2f6a2512019-03-30 15:31:42 -070042 (b'x = "foo"\n', 0, ''),
Anthony Sottile53f1dc02015-01-04 13:06:21 -080043)
44
45
46all_tests = pytest.mark.parametrize(
47 ('contents', 'expected', 'expected_out'), TESTS,
48)
49
50
51@all_tests
52def test_unit(capsys, contents, expected, expected_out):
53 assert check_docstring_first(contents) == expected
54 assert capsys.readouterr()[0] == expected_out.format(filename='<unknown>')
55
56
57@all_tests
58def test_integration(tmpdir, capsys, contents, expected, expected_out):
Anthony Sottilea99475a2016-05-27 14:09:50 -070059 f = tmpdir.join('test.py')
Anthony Sottile2f6a2512019-03-30 15:31:42 -070060 f.write_binary(contents)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040061 assert main([str(f)]) == expected
62 assert capsys.readouterr()[0] == expected_out.format(filename=str(f))
Anthony Sottile2f6a2512019-03-30 15:31:42 -070063
64
65def test_arbitrary_encoding(tmpdir):
66 f = tmpdir.join('f.py')
67 contents = '# -*- coding: cp1252\nx = "£"'.encode('cp1252')
68 f.write_binary(contents)
Max Rozentsveygf35bfed2020-05-20 12:07:45 -040069 assert main([str(f)]) == 0