-
-
Notifications
You must be signed in to change notification settings - Fork 707
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
56 additions
and
5 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,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') | ||
args = parser.parse_args(argv) | ||
|
||
retv = 0 | ||
|
||
for filename in args.filenames: | ||
with open(filename, 'rb') as f_b: | ||
bts = f_b.read(3) | ||
|
||
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()) |
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,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 |