Skip to content

Commit

Permalink
added tests and a bit more sophisticated detection
Browse files Browse the repository at this point in the history
  • Loading branch information
JBKahn committed Jul 4, 2014
1 parent b8724ab commit 1f608a8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
18 changes: 17 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,25 @@ available in ``flake8``::
2.0 (pep8: 1.4.5, flake8-print: 1.0, pyflakes: 0.6.1)


To Fix
------------

Cover multline comments and doc strings.


Changes
-------

1.0 - 2014-06-30
1.2 - 2014-06-30
````````````````
* Does not catch the word print in single line strings
* Does not catch inline comments with print in it
* Added tests

1.1 - 2014-06-30
````````````````
* First release

1.0 - 2014-06-30
````````````````
* Whoops
11 changes: 9 additions & 2 deletions flake8_print.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import pep8
import re

__version__ = '1.1'
__version__ = '1.2'

PRINT_REGEX = re.compile(r'(print)')
PRINT_ERROR_CODE = 'T001'
PRINT_ERROR_MESSAGE = 'print statement found.'


def check_for_print_statements(physical_line):
if pep8.noqa(physical_line):
return
physical_line = re.sub(r'\"\"\"(.+?)\"\"\"', "", physical_line)
physical_line = re.sub(r'\"(.+?)\"', "", physical_line)
physical_line = re.sub(r'\'\'\'(.+?)\'\'\'', "", physical_line)
physical_line = re.sub(r'\'(.+?)\'', "", physical_line)
physical_line = re.sub(r'#(.+)', "#", physical_line)
match = PRINT_REGEX.search(physical_line)
if match:
return match.start(), 'T000 print statement found.'
return match.start(), '{} {}'.format(PRINT_ERROR_CODE, PRINT_ERROR_MESSAGE)


check_for_print_statements.name = name = 'flake8-print'
Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def get_long_description():
descr.append(f.read())
return '\n\n'.join(descr)

install_requires = ['flake8']

test_requires = ['nose']

setup(
name='flake8-print',
Expand All @@ -36,6 +39,9 @@ def get_long_description():
'flake8_print = flake8_print:check_for_print_statements',
],
},
install_requires=install_requires,
tests_require=test_requires,
test_suite="nose.collector",
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Console',
Expand Down
67 changes: 67 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from flake8_print import check_for_print_statements, PRINT_ERROR_CODE, PRINT_ERROR_MESSAGE
from nose.tools import assert_equal


class Flake8PrintTestCases(object):
def generate_error_statement(self, index):
return (index, '{} {}'.format(PRINT_ERROR_CODE, PRINT_ERROR_MESSAGE))


class TestGenericCases(Flake8PrintTestCases):
def test_skips_noqa(self):
result = check_for_print_statements('44 print 4 # noqa')
assert result is None

def test_catches_simple_print_python2(self):
result = check_for_print_statements('print 4')
assert_equal(result, self.generate_error_statement(0))

def test_catches_simple_print_python3(self):
result = check_for_print_statements('print(4)')
assert_equal(result, self.generate_error_statement(0))


class TestComments(Flake8PrintTestCases):
def test_print_in_inline_comment_is_not_a_false_positive(self):
result = check_for_print_statements('# what should I print ?')
assert result is None

def test_print_same_line_as_comment(self):
result = check_for_print_statements('print 5 # what should I do with 5 ?')
assert_equal(result, self.generate_error_statement(0))


class TestSingleQuotes(Flake8PrintTestCases):
def test_print_in_one_single_quote_single_line_string_not_false_positive(self):
result = check_for_print_statements('a(\'print the things\', 25)')
assert result is None

def test_print_in_between_two_one_single_quote_single_line_string(self):
result = check_for_print_statements('a(\'print the things\' print \'print the things\', 25)')
assert_equal(result, self.generate_error_statement(3))

def test_print_in_three_single_quote_single_line_string_not_false_positive(self):
result = check_for_print_statements('a(\'\'\'print the things\'\'\', 25)')
assert result is None

def test_print_in_between_two_three_single_quote_single_line_string(self):
result = check_for_print_statements('a(\'\'\'print the things\'\'\' print \'\'\'print the things\'\'\', 25)')
assert_equal(result, self.generate_error_statement(3))


class TestDoubleQuotes(Flake8PrintTestCases):
def test_print_in_one_double_quote_single_line_string_not_false_positive(self):
result = check_for_print_statements('a("print the things", 25)')
assert result is None

def test_print_in_between_two_one_double_quote_single_line_string(self):
result = check_for_print_statements('a("print the things" print "print the things", 25)')
assert_equal(result, self.generate_error_statement(3))

def test_print_in_three_double_quote_single_line_string_not_false_positive(self):
result = check_for_print_statements('a("""print the things""", 25)')
assert result is None

def test_print_in_between_two_three_double_quote_single_line_string(self):
result = check_for_print_statements('a("print the things" print "print the things", 25)')
assert_equal(result, self.generate_error_statement(3))

0 comments on commit 1f608a8

Please sign in to comment.