-
-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new byte-order-marker checker/fixer
- Loading branch information
Showing
5 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import argparse | ||
from typing import Optional | ||
from typing import Sequence | ||
|
||
|
||
def main(argv: Optional[Sequence[str]] = None) -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
'-f', '--fix', | ||
choices=('auto', 'no'), | ||
default='auto', | ||
help='Remove the byte-order marker. Default is "auto"', | ||
) | ||
parser.add_argument('filenames', nargs='*', help='Filenames to check') | ||
args = parser.parse_args(argv) | ||
|
||
retv = 0 | ||
|
||
for filename in args.filenames: | ||
f = open(filename, 'rb') | ||
if f.read(3) == b'\xef\xbb\xbf': | ||
f.close() | ||
if args.fix == 'no': | ||
print(f'{filename}: Found byte-order marker') | ||
else: | ||
with open( | ||
filename, newline='', | ||
encoding='utf-8-sig', | ||
) as rewritefile: | ||
contents = rewritefile.read() | ||
|
||
with open( | ||
filename, 'w', newline='', | ||
encoding='utf-8', | ||
) as rewritefile: | ||
rewritefile.writelines(contents) | ||
|
||
print(f'{filename}: Removed byte-order marker') | ||
retv = 1 | ||
|
||
return retv | ||
|
||
|
||
if __name__ == '__main__': | ||
exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from pre_commit_hooks import byte_order_marker | ||
|
||
|
||
def test_failure(tmpdir): | ||
f = tmpdir.join('f.txt') | ||
f.write_text('ohai', encoding='utf-8-sig') | ||
assert byte_order_marker.main((str(f),)) == 1 | ||
|
||
|
||
def test_success(tmpdir): | ||
f = tmpdir.join('f.txt') | ||
f.write_text('ohai', encoding='utf-8') | ||
assert byte_order_marker.main((str(f),)) == 0 | ||
|
||
|
||
def test_failure_nofix(tmpdir): | ||
f = tmpdir.join('f.txt') | ||
f.write_text('ohai', encoding='utf-8-sig') | ||
assert byte_order_marker.main((str(f), '--fix=no')) == 1 | ||
|
||
|
||
def test_success_nofix(tmpdir): | ||
f = tmpdir.join('f.txt') | ||
f.write_text('ohai', encoding='utf-8') | ||
assert byte_order_marker.main((str(f), '--fix=no')) == 0 |