2022-01-16 08:24:05 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2015-11-14 04:34:37 +08:00
|
|
|
import io
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2016-08-12 13:56:54 +08:00
|
|
|
from pre_commit_hooks.fix_encoding_pragma import _normalize_pragma
|
2015-11-14 04:34:37 +08:00
|
|
|
from pre_commit_hooks.fix_encoding_pragma import fix_encoding_pragma
|
|
|
|
from pre_commit_hooks.fix_encoding_pragma import main
|
|
|
|
|
|
|
|
|
|
|
|
def test_integration_inserting_pragma(tmpdir):
|
2016-04-28 02:18:14 +08:00
|
|
|
path = tmpdir.join('foo.py')
|
|
|
|
path.write_binary(b'import httplib\n')
|
2015-11-14 04:34:37 +08:00
|
|
|
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(path),)) == 1
|
2015-11-14 04:34:37 +08:00
|
|
|
|
2016-04-28 02:18:14 +08:00
|
|
|
assert path.read_binary() == (
|
|
|
|
b'# -*- coding: utf-8 -*-\n'
|
|
|
|
b'import httplib\n'
|
|
|
|
)
|
2015-11-14 04:34:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_integration_ok(tmpdir):
|
2016-04-28 02:18:14 +08:00
|
|
|
path = tmpdir.join('foo.py')
|
|
|
|
path.write_binary(b'# -*- coding: utf-8 -*-\nx = 1\n')
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(path),)) == 0
|
2016-04-28 02:18:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_integration_remove(tmpdir):
|
|
|
|
path = tmpdir.join('foo.py')
|
|
|
|
path.write_binary(b'# -*- coding: utf-8 -*-\nx = 1\n')
|
|
|
|
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(path), '--remove')) == 1
|
2016-04-28 02:18:14 +08:00
|
|
|
|
|
|
|
assert path.read_binary() == b'x = 1\n'
|
|
|
|
|
|
|
|
|
|
|
|
def test_integration_remove_ok(tmpdir):
|
|
|
|
path = tmpdir.join('foo.py')
|
|
|
|
path.write_binary(b'x = 1\n')
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(path), '--remove')) == 0
|
2015-11-14 04:34:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'input_str',
|
|
|
|
(
|
|
|
|
b'',
|
2016-04-28 02:18:14 +08:00
|
|
|
(
|
|
|
|
b'# -*- coding: utf-8 -*-\n'
|
|
|
|
b'x = 1\n'
|
|
|
|
),
|
2015-11-14 04:34:37 +08:00
|
|
|
(
|
|
|
|
b'#!/usr/bin/env python\n'
|
|
|
|
b'# -*- coding: utf-8 -*-\n'
|
|
|
|
b'foo = "bar"\n'
|
|
|
|
),
|
2017-07-13 09:35:24 +08:00
|
|
|
),
|
2015-11-14 04:34:37 +08:00
|
|
|
)
|
|
|
|
def test_ok_inputs(input_str):
|
|
|
|
bytesio = io.BytesIO(input_str)
|
|
|
|
assert fix_encoding_pragma(bytesio) == 0
|
|
|
|
bytesio.seek(0)
|
|
|
|
assert bytesio.read() == input_str
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('input_str', 'output'),
|
|
|
|
(
|
|
|
|
(
|
|
|
|
b'import httplib\n',
|
|
|
|
b'# -*- coding: utf-8 -*-\n'
|
|
|
|
b'import httplib\n',
|
|
|
|
),
|
|
|
|
(
|
2016-04-28 02:18:14 +08:00
|
|
|
b'#!/usr/bin/env python\n'
|
|
|
|
b'x = 1\n',
|
2015-11-14 04:34:37 +08:00
|
|
|
b'#!/usr/bin/env python\n'
|
|
|
|
b'# -*- coding: utf-8 -*-\n'
|
2016-04-28 02:18:14 +08:00
|
|
|
b'x = 1\n',
|
2015-11-14 04:34:37 +08:00
|
|
|
),
|
|
|
|
(
|
2016-04-28 02:18:14 +08:00
|
|
|
b'#coding=utf-8\n'
|
|
|
|
b'x = 1\n',
|
2015-11-14 04:34:37 +08:00
|
|
|
b'# -*- coding: utf-8 -*-\n'
|
2016-04-28 02:18:14 +08:00
|
|
|
b'x = 1\n',
|
2015-11-14 04:34:37 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
b'#!/usr/bin/env python\n'
|
2016-04-28 02:18:14 +08:00
|
|
|
b'#coding=utf8\n'
|
|
|
|
b'x = 1\n',
|
2015-11-14 04:34:37 +08:00
|
|
|
b'#!/usr/bin/env python\n'
|
2016-04-28 02:18:14 +08:00
|
|
|
b'# -*- coding: utf-8 -*-\n'
|
|
|
|
b'x = 1\n',
|
2015-11-14 04:34:37 +08:00
|
|
|
),
|
2016-04-28 02:18:14 +08:00
|
|
|
# These should each get truncated
|
|
|
|
(b'#coding: utf-8\n', b''),
|
|
|
|
(b'# -*- coding: utf-8 -*-\n', b''),
|
|
|
|
(b'#!/usr/bin/env python\n', b''),
|
|
|
|
(b'#!/usr/bin/env python\n#coding: utf8\n', b''),
|
|
|
|
(b'#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n', b''),
|
2017-07-13 09:35:24 +08:00
|
|
|
),
|
2015-11-14 04:34:37 +08:00
|
|
|
)
|
|
|
|
def test_not_ok_inputs(input_str, output):
|
|
|
|
bytesio = io.BytesIO(input_str)
|
|
|
|
assert fix_encoding_pragma(bytesio) == 1
|
|
|
|
bytesio.seek(0)
|
|
|
|
assert bytesio.read() == output
|
2016-08-12 13:56:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_ok_input_alternate_pragma():
|
|
|
|
input_s = b'# coding: utf-8\nx = 1\n'
|
|
|
|
bytesio = io.BytesIO(input_s)
|
2019-05-16 01:04:18 +08:00
|
|
|
ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
|
2016-08-12 13:56:54 +08:00
|
|
|
assert ret == 0
|
|
|
|
bytesio.seek(0)
|
|
|
|
assert bytesio.read() == input_s
|
|
|
|
|
|
|
|
|
|
|
|
def test_not_ok_input_alternate_pragma():
|
|
|
|
bytesio = io.BytesIO(b'x = 1\n')
|
2019-05-16 01:04:18 +08:00
|
|
|
ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
|
2016-08-12 13:56:54 +08:00
|
|
|
assert ret == 1
|
|
|
|
bytesio.seek(0)
|
|
|
|
assert bytesio.read() == b'# coding: utf-8\nx = 1\n'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('input_s', 'expected'),
|
|
|
|
(
|
2019-05-16 01:04:18 +08:00
|
|
|
('# coding: utf-8', b'# coding: utf-8'),
|
2016-08-12 13:56:54 +08:00
|
|
|
# trailing whitespace
|
2019-05-16 01:04:18 +08:00
|
|
|
('# coding: utf-8\n', b'# coding: utf-8'),
|
2016-08-12 13:56:54 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_normalize_pragma(input_s, expected):
|
|
|
|
assert _normalize_pragma(input_s) == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_integration_alternate_pragma(tmpdir, capsys):
|
|
|
|
f = tmpdir.join('f.py')
|
|
|
|
f.write('x = 1\n')
|
|
|
|
|
|
|
|
pragma = '# coding: utf-8'
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(f), '--pragma', pragma)) == 1
|
2016-08-12 13:56:54 +08:00
|
|
|
assert f.read() == '# coding: utf-8\nx = 1\n'
|
|
|
|
out, _ = capsys.readouterr()
|
2020-05-21 00:07:45 +08:00
|
|
|
assert out == f'Added `# coding: utf-8` to {str(f)}\n'
|
2019-05-16 01:04:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_crlf_ok(tmpdir):
|
|
|
|
f = tmpdir.join('f.py')
|
|
|
|
f.write_binary(b'# -*- coding: utf-8 -*-\r\nx = 1\r\n')
|
2020-05-21 00:07:45 +08:00
|
|
|
assert not main((str(f),))
|
2019-05-16 01:04:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_crfl_adds(tmpdir):
|
|
|
|
f = tmpdir.join('f.py')
|
|
|
|
f.write_binary(b'x = 1\r\n')
|
2020-05-21 00:07:45 +08:00
|
|
|
assert main((str(f),))
|
2019-05-16 01:04:18 +08:00
|
|
|
assert f.read_binary() == b'# -*- coding: utf-8 -*-\r\nx = 1\r\n'
|