Skip to content
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

Don't error out on compressed files or on non-Unicode files #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dodgy/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions dodgy/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 8 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from unittest import TestCase
from dodgy.checks import check_file

Expand Down Expand Up @@ -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')

1 change: 1 addition & 0 deletions tests/testdata/bad_utf8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�(
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py26,py27,py33
envlist = py26,py27,py33,py34

[testenv]
deps=nose
Expand Down