diff --git a/dodgy/checks.py b/dodgy/checks.py index 93474e9..918590c 100644 --- a/dodgy/checks.py +++ b/dodgy/checks.py @@ -67,8 +67,11 @@ def check_line(line, check_list): def check_file(filepath): - with open(filepath) as to_check: - return check_file_contents(to_check.read()) + try: + with open(filepath) as to_check: + return check_file_contents(to_check.read()) + except UnicodeDecodeError as e: + return [(0, 'unicode_decode_error', str(e))] def check_file_contents(file_contents): diff --git a/dodgy/run.py b/dodgy/run.py index 55fb332..f284600 100644 --- a/dodgy/run.py +++ b/dodgy/run.py @@ -40,6 +40,9 @@ def run_checks(directory, ignore_paths=None): mimetype = mimetypes.guess_type(filepath) if mimetype[0] is None or not mimetype[0].startswith('text/'): continue + # Also skip anything with an encoding (e.g., a gzipped CSS). + if mimetype[1]: + continue for msg_parts in check_file(filepath): warnings.append({ diff --git a/tests/test_checks.py b/tests/test_checks.py index 79b3be8..dc800d8 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -1,4 +1,5 @@ import os +import sys from unittest import TestCase from dodgy.checks import check_file @@ -48,3 +49,10 @@ def test_ssh_privatekey(self): def test_ssh_publickey(self): self._do_test('ssh_public_key.pub', 'ssh_rsa_public_key') + + def test_bad_unicode(self): + """Test that we handle errors during Python 3's required Unicode + decoding.""" + if sys.version_info > (3, 0): + self._do_test('bad_utf8.txt', 'unicode_decode_error') + diff --git a/tests/testdata/bad_utf8.txt b/tests/testdata/bad_utf8.txt new file mode 100644 index 0000000..06c0962 --- /dev/null +++ b/tests/testdata/bad_utf8.txt @@ -0,0 +1 @@ +Ã( \ No newline at end of file diff --git a/tox.ini b/tox.ini index 7c78e47..f6c7f3d 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py26,py27,py33 +envlist = py26,py27,py33,py34 [testenv] deps=nose