From 1f608a8d05155645880ef6cf0f47b89f41d15b4a Mon Sep 17 00:00:00 2001 From: Joseph Kahn Date: Fri, 4 Jul 2014 01:15:58 -0400 Subject: [PATCH] added tests and a bit more sophisticated detection --- README.rst | 18 ++++++++++++- flake8_print.py | 11 ++++++-- setup.py | 6 +++++ test.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 test.py diff --git a/README.rst b/README.rst index 5c8e53b..92bf4d0 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/flake8_print.py b/flake8_print.py index 532e707..6d5f313 100644 --- a/flake8_print.py +++ b/flake8_print.py @@ -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' diff --git a/setup.py b/setup.py index 4ea5e22..7561ea2 100644 --- a/setup.py +++ b/setup.py @@ -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', @@ -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', diff --git a/test.py b/test.py new file mode 100644 index 0000000..74bd6cd --- /dev/null +++ b/test.py @@ -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))