summaryrefslogtreecommitdiffstats
path: root/tests/trailing_whitespace_fixer_test.py
blob: c07497a2215886873b6b68dfd0e84062960d637c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from __future__ import annotations

import pytest

from pre_commit_hooks.trailing_whitespace_fixer import main


@pytest.mark.parametrize(
    ('input_s', 'expected'),
    (
        ('foo \nbar \n', 'foo\nbar\n'),
        ('bar\t\nbaz\t\n', 'bar\nbaz\n'),
    ),
)
def test_fixes_trailing_whitespace(input_s, expected, tmpdir):
    path = tmpdir.join('file.md')
    path.write(input_s)
    assert main((str(path),)) == 1
    assert path.read() == expected


def test_ok_no_newline_end_of_file(tmpdir):
    filename = tmpdir.join('f')
    filename.write_binary(b'foo\nbar')
    ret = main((str(filename),))
    assert filename.read_binary() == b'foo\nbar'
    assert ret == 0


def test_ok_with_dos_line_endings(tmpdir):
    filename = tmpdir.join('f')
    filename.write_binary(b'foo\r\nbar\r\nbaz\r\n')
    ret = main((str(filename),))
    assert filename.read_binary() == b'foo\r\nbar\r\nbaz\r\n'
    assert ret == 0


@pytest.mark.parametrize('ext', ('md', 'Md', '.md', '*'))
def test_fixes_markdown_files(tmpdir, ext):
    path = tmpdir.join('test.md')
    path.write(
        'foo  \n'  # leaves alone
        'bar \n'  # less than two so it is removed
        'baz    \n'  # more than two so it becomes two spaces
        '\t\n'  # trailing tabs are stripped anyway
        '\n  ',  # whitespace at the end of the file is removed
    )
    ret = main((str(path), f'--markdown-linebreak-ext={ext}'))
    assert ret == 1
    assert path.read() == (
        'foo  \n'
        'bar\n'
        'baz  \n'
        '\n'
        '\n'
    )


@pytest.mark.parametrize('arg', ('--', 'a.b', 'a/b', ''))
def test_markdown_linebreak_ext_badopt(arg):
    with pytest.raises(SystemExit) as excinfo:
        main(['--markdown-linebreak-ext', arg])
    assert excinfo.value.code == 2


def test_prints_warning_with_no_markdown_ext(capsys, tmpdir):
    f = tmpdir.join('f').ensure()
    assert main((str(f), '--no-markdown-linebreak-ext')) == 0
    out, _ = capsys.readouterr()
    assert out == '--no-markdown-linebreak-ext now does nothing!\n'


def test_preserve_non_utf8_file(tmpdir):
    non_utf8_bytes_content = b'<a>\xe9 \n</a>\n'
    path = tmpdir.join('file.txt')
    path.write_binary(non_utf8_bytes_content)
    ret = main([str(path)])
    assert ret == 1
    assert path.size() == (len(non_utf8_bytes_content) - 1)


def test_custom_charset_change(tmpdir):
    # strip spaces only, no tabs
    path = tmpdir.join('file.txt')
    path.write('\ta \t \n')
    ret = main([str(path), '--chars', ' '])
    assert ret == 1
    assert path.read() == '\ta \t\n'


def test_custom_charset_no_change(tmpdir):
    path = tmpdir.join('file.txt')
    path.write('\ta \t\n')
    ret = main([str(path), '--chars', ' '])
    assert ret == 0


def test_markdown_with_custom_charset(tmpdir):
    path = tmpdir.join('file.md')
    path.write('\ta \t   \n')
    ret = main([str(path), '--chars', ' ', '--markdown-linebreak-ext', '*'])
    assert ret == 1
    assert path.read() == '\ta \t  \n'