Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
0 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,29 @@ | ||
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.') | ||
parser.add_argument('--encoding', help='Encoding to assert.') | ||
args = parser.parse_args(argv) | ||
|
||
retval = 0 | ||
for filename in args.filenames: | ||
try: | ||
with open(filename, encoding=args.encoding) as f: | ||
f.read() | ||
except LookupError as exc: | ||
# Unknown encoding, don't bother with the rest | ||
print(f'{__file__}: {exc}') | ||
retval = 2 | ||
break | ||
except Exception as exc: | ||
print(f'{filename}: {exc}') | ||
retval = 1 | ||
return retval | ||
|
||
|
||
if __name__ == '__main__': | ||
raise SystemExit(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,17 @@ | ||
import pytest | ||
|
||
from pre_commit_hooks.check_encoding import main | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('content', 'encoding', 'expected'), | ||
( | ||
(b'Hello!', 'ascii', 0), | ||
(b'Hello!', 'unknown-encoding', 2), | ||
('Hello ☃!'.encode(), 'ascii', 1), | ||
), | ||
) | ||
def test_has_encoding(content, encoding, expected, tmpdir): | ||
path = tmpdir.join('path') | ||
path.write(content, 'wb') | ||
assert main(('--encoding', encoding, str(path))) == expected |