# Path: pre_commit_hooks/fix_encoding_pragma.py
# def _normalize_pragma(pragma: str) -> bytes:
#     return pragma.encode().rstrip()
# 
# Path: pre_commit_hooks/fix_encoding_pragma.py
# def fix_encoding_pragma(
#         f: IO[bytes],
#         remove: bool = False,
#         expected_pragma: bytes = DEFAULT_PRAGMA,
# ) -> int:
#     expected = _get_expected_contents(
#         f.readline(), f.readline(), f.read(), expected_pragma,
#     )
# 
#     # Special cases for empty files
#     if not expected.rest.strip():
#         # If a file only has a shebang or a coding pragma, remove it
#         if expected.has_any_pragma or expected.shebang:
#             f.seek(0)
#             f.truncate()
#             f.write(b'')
#             return 1
#         else:
#             return 0
# 
#     if expected.is_expected_pragma(remove):
#         return 0
# 
#     # Otherwise, write out the new file
#     f.seek(0)
#     f.truncate()
#     f.write(expected.shebang)
#     if not remove:
#         f.write(expected_pragma + expected.ending)
#     f.write(expected.rest)
# 
#     return 1
# 
# Path: pre_commit_hooks/fix_encoding_pragma.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser(
#         'Fixes the encoding pragma of python files',
#     )
#     parser.add_argument('filenames', nargs='*', help='Filenames to fix')
#     parser.add_argument(
#         '--pragma', default=DEFAULT_PRAGMA, type=_normalize_pragma,
#         help=(
#             f'The encoding pragma to use.  '
#             f'Default: {DEFAULT_PRAGMA.decode()}'
#         ),
#     )
#     parser.add_argument(
#         '--remove', action='store_true',
#         help='Remove the encoding pragma (Useful in a python3-only codebase)',
#     )
#     args = parser.parse_args(argv)
# 
#     retv = 0
# 
#     if args.remove:
#         fmt = 'Removed encoding pragma from {filename}'
#     else:
#         fmt = 'Added `{pragma}` to {filename}'
# 
#     for filename in args.filenames:
#         with open(filename, 'r+b') as f:
#             file_ret = fix_encoding_pragma(
#                 f, remove=args.remove, expected_pragma=args.pragma,
#             )
#             retv |= file_ret
#             if file_ret:
#                 print(
#                     fmt.format(pragma=args.pragma.decode(), filename=filename),
#                 )
# 
#     return retv


import io
import pytest
from pre_commit_hooks.fix_encoding_pragma import _normalize_pragma
from pre_commit_hooks.fix_encoding_pragma import fix_encoding_pragma
from pre_commit_hooks.fix_encoding_pragma import main
    bytesio.seek(0)
    assert bytesio.read() == output


def test_ok_input_alternate_pragma():
    input_s = b'# coding: utf-8\nx = 1\n'
    bytesio = io.BytesIO(input_s)
    ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
    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')
    ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
    assert ret == 1
    bytesio.seek(0)
    assert bytesio.read() == b'# coding: utf-8\nx = 1\n'


@pytest.mark.parametrize(
    ('input_s', 'expected'),
    (
        ('# coding: utf-8', b'# coding: utf-8'),
        # trailing whitespace
        ('# coding: utf-8\n', b'# coding: utf-8'),
    ),
)
def test_normalize_pragma(input_s, expected):

# Path: pre_commit_hooks/fix_encoding_pragma.py
# def _normalize_pragma(pragma: str) -> bytes:
#     return pragma.encode().rstrip()
# 
# Path: pre_commit_hooks/fix_encoding_pragma.py
# def fix_encoding_pragma(
#         f: IO[bytes],
#         remove: bool = False,
#         expected_pragma: bytes = DEFAULT_PRAGMA,
# ) -> int:
#     expected = _get_expected_contents(
#         f.readline(), f.readline(), f.read(), expected_pragma,
#     )
# 
#     # Special cases for empty files
#     if not expected.rest.strip():
#         # If a file only has a shebang or a coding pragma, remove it
#         if expected.has_any_pragma or expected.shebang:
#             f.seek(0)
#             f.truncate()
#             f.write(b'')
#             return 1
#         else:
#             return 0
# 
#     if expected.is_expected_pragma(remove):
#         return 0
# 
#     # Otherwise, write out the new file
#     f.seek(0)
#     f.truncate()
#     f.write(expected.shebang)
#     if not remove:
#         f.write(expected_pragma + expected.ending)
#     f.write(expected.rest)
# 
#     return 1
# 
# Path: pre_commit_hooks/fix_encoding_pragma.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser(
#         'Fixes the encoding pragma of python files',
#     )
#     parser.add_argument('filenames', nargs='*', help='Filenames to fix')
#     parser.add_argument(
#         '--pragma', default=DEFAULT_PRAGMA, type=_normalize_pragma,
#         help=(
#             f'The encoding pragma to use.  '
#             f'Default: {DEFAULT_PRAGMA.decode()}'
#         ),
#     )
#     parser.add_argument(
#         '--remove', action='store_true',
#         help='Remove the encoding pragma (Useful in a python3-only codebase)',
#     )
#     args = parser.parse_args(argv)
# 
#     retv = 0
# 
#     if args.remove:
#         fmt = 'Removed encoding pragma from {filename}'
#     else:
#         fmt = 'Added `{pragma}` to {filename}'
# 
#     for filename in args.filenames:
#         with open(filename, 'r+b') as f:
#             file_ret = fix_encoding_pragma(
#                 f, remove=args.remove, expected_pragma=args.pragma,
#             )
#             retv |= file_ret
#             if file_ret:
#                 print(
#                     fmt.format(pragma=args.pragma.decode(), filename=filename),
#                 )
# 
#     return retv


# Path: tests/fix_encoding_pragma_test.py
import io
import pytest
from pre_commit_hooks.fix_encoding_pragma import _normalize_pragma
from pre_commit_hooks.fix_encoding_pragma import fix_encoding_pragma
from pre_commit_hooks.fix_encoding_pragma import main

    bytesio.seek(0)
    assert bytesio.read() == output


def test_ok_input_alternate_pragma():
    input_s = b'# coding: utf-8\nx = 1\n'
    bytesio = io.BytesIO(input_s)
    ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
    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')
    ret = fix_encoding_pragma(bytesio, expected_pragma=b'# coding: utf-8')
    assert ret == 1
    bytesio.seek(0)
    assert bytesio.read() == b'# coding: utf-8\nx = 1\n'


@pytest.mark.parametrize(
    ('input_s', 'expected'),
    (
        ('# coding: utf-8', b'# coding: utf-8'),
        # trailing whitespace
        ('# coding: utf-8\n', b'# coding: utf-8'),
    ),
)
def test_normalize_pragma(input_s, expected):

    assert _normalize_pragma(input_s) == expected