Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #36 from prusse-martin/fb-python-no-bom
Browse files Browse the repository at this point in the history
Remove UTF-8 BOM from python files
  • Loading branch information
prusse-martin authored Dec 10, 2018
2 parents d3057eb + 4936be3 commit c1190b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ History
=======


Unreleased
1.7.0
----------

* Check if .cpp file is non-ascii, ensure it has BOM at the beginning of the file.
* Emmit message when clang-format is not installed (or usable).
* Ensure python files do not include BOM.

1.6.0
------
Expand Down
8 changes: 8 additions & 0 deletions esss_fix_format/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ def _process_file(filename, check, format_code):
error_msg = 'Error formatting code: %s' % (e,)
click.secho(error_msg, fg='red')
errors.append(error_msg)

if new_contents and (new_contents[0] == codecs.BOM_UTF8.decode('UTF-8')):
msg = ': ERROR python file should not have a BOM.'
error_msg = click.format_filename(filename) + msg
click.secho(error_msg, fg='red')
errors.append(error_msg)
new_contents = new_contents[1:]

elif is_cpp(filename):
formatter = 'legacy formatter'

Expand Down
20 changes: 20 additions & 0 deletions tests/test_esss_fix_format.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
import io
import os
import subprocess
Expand Down Expand Up @@ -210,6 +211,25 @@ def test_empty_file(tmpdir, sort_cfg_to_tmpdir):
run([str(filename)], expected_exit=0)


@pytest.mark.parametrize('check', [True, False])
def test_python_with_bom(tmpdir, sort_cfg_to_tmpdir, check):
filename = tmpdir.join('test.py')
original_contents = codecs.BOM_UTF8 + b'import io\r\n'
filename.write(original_contents, 'wb')

args = [str(filename)]
if check:
args = ['--check'] + args

run(args, expected_exit=1)

current_contents = filename.read('rb')
if check:
assert current_contents == original_contents
else:
assert current_contents == original_contents[len(codecs.BOM_UTF8):]


@pytest.mark.parametrize(
'source',
[
Expand Down

0 comments on commit c1190b1

Please sign in to comment.