Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 3 | import pytest |
| 4 | |
| 5 | from pre_commit_hooks.check_docstring_first import check_docstring_first |
| 6 | from pre_commit_hooks.check_docstring_first import main |
| 7 | |
| 8 | |
| 9 | # Contents, expected, expected_output |
| 10 | TESTS = ( |
| 11 | # trivial |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 12 | (b'', 0, ''), |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 13 | # Acceptable |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 14 | (b'"foo"', 0, ''), |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 15 | # Docstring after code |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 16 | ( |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 17 | b'from __future__ import unicode_literals\n' |
| 18 | b'"foo"\n', |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 19 | 1, |
Anthony Sottile | b13ff9b | 2022-04-06 16:55:26 -0400 | [diff] [blame] | 20 | '{filename}:2: Module docstring appears after code ' |
Anthony Sottile | 2a902e0 | 2017-07-12 18:35:24 -0700 | [diff] [blame] | 21 | '(code seen on line 1).\n', |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 22 | ), |
| 23 | # Test double docstring |
| 24 | ( |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 25 | b'"The real docstring"\n' |
| 26 | b'from __future__ import absolute_import\n' |
| 27 | b'"fake docstring"\n', |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 28 | 1, |
Anthony Sottile | b13ff9b | 2022-04-06 16:55:26 -0400 | [diff] [blame] | 29 | '{filename}:3: Multiple module docstrings ' |
Anthony Sottile | 2a902e0 | 2017-07-12 18:35:24 -0700 | [diff] [blame] | 30 | '(first docstring on line 1).\n', |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 31 | ), |
| 32 | # Test multiple lines of code above |
| 33 | ( |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 34 | b'import os\n' |
| 35 | b'import sys\n' |
| 36 | b'"docstring"\n', |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 37 | 1, |
Anthony Sottile | b13ff9b | 2022-04-06 16:55:26 -0400 | [diff] [blame] | 38 | '{filename}:3: Module docstring appears after code ' |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 39 | '(code seen on line 1).\n', |
| 40 | ), |
| 41 | # String literals in expressions are ok. |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 42 | (b'x = "foo"\n', 0, ''), |
Anthony Sottile | 53f1dc0 | 2015-01-04 13:06:21 -0800 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | |
| 46 | all_tests = pytest.mark.parametrize( |
| 47 | ('contents', 'expected', 'expected_out'), TESTS, |
| 48 | ) |
| 49 | |
| 50 | |
| 51 | @all_tests |
| 52 | def 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 |
| 58 | def test_integration(tmpdir, capsys, contents, expected, expected_out): |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 59 | f = tmpdir.join('test.py') |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 60 | f.write_binary(contents) |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 61 | assert main([str(f)]) == expected |
| 62 | assert capsys.readouterr()[0] == expected_out.format(filename=str(f)) |
Anthony Sottile | 2f6a251 | 2019-03-30 15:31:42 -0700 | [diff] [blame] | 63 | |
| 64 | |
| 65 | def test_arbitrary_encoding(tmpdir): |
| 66 | f = tmpdir.join('f.py') |
| 67 | contents = '# -*- coding: cp1252\nx = "£"'.encode('cp1252') |
| 68 | f.write_binary(contents) |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 69 | assert main([str(f)]) == 0 |