-
-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempt to add fixing of BOMs #522
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,8 @@ | |
language: python | ||
types: [python] | ||
- id: check-byte-order-marker | ||
name: Check for byte-order marker | ||
description: Forbid files which have a UTF-8 byte-order marker | ||
name: 'check BOM - deprecated: use fix-byte-order-marker' | ||
description: forbid files which have a UTF-8 byte-order marker | ||
entry: check-byte-order-marker | ||
language: python | ||
types: [text] | ||
|
@@ -131,6 +131,12 @@ | |
entry: file-contents-sorter | ||
language: python | ||
files: '^$' | ||
- id: fix-byte-order-marker | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed this to |
||
name: fix UTF-8 byte order marker | ||
description: removes UTF-8 byte order marker | ||
entry: fix-byte-order-marker | ||
language: python | ||
types: [text] | ||
- id: fix-encoding-pragma | ||
name: Fix python encoding pragma | ||
language: python | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import argparse | ||
from typing import Optional | ||
from typing import Sequence | ||
|
||
|
||
def main(argv: Optional[Sequence[str]] = None) -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('filenames', nargs='*', help='Filenames to check') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the |
||
args = parser.parse_args(argv) | ||
|
||
retv = 0 | ||
|
||
for filename in args.filenames: | ||
with open(filename, 'rb') as f_b: | ||
bts = f_b.read(3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there was a leaking file descriptor here, I fixed that |
||
|
||
if bts == b'\xef\xbb\xbf': | ||
with open(filename, newline='', encoding='utf-8-sig') as f: | ||
contents = f.read() | ||
with open(filename, 'w', newline='', encoding='utf-8') as f: | ||
f.write(contents) | ||
|
||
print(f'{filename}: removed byte-order marker') | ||
retv = 1 | ||
|
||
return retv | ||
|
||
|
||
if __name__ == '__main__': | ||
exit(main()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pre_commit_hooks import fix_byte_order_marker | ||
|
||
|
||
def test_failure(tmpdir): | ||
f = tmpdir.join('f.txt') | ||
f.write_text('ohai', encoding='utf-8-sig') | ||
assert fix_byte_order_marker.main((str(f),)) == 1 | ||
|
||
|
||
def test_success(tmpdir): | ||
f = tmpdir.join('f.txt') | ||
f.write_text('ohai', encoding='utf-8') | ||
assert fix_byte_order_marker.main((str(f),)) == 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I shortened the name here so it doesn't wrap -- I should probably add a test for this