From 705c16a2686ef561112854f180b7c79377049922 Mon Sep 17 00:00:00 2001 From: Byeonghoon Yoo Date: Wed, 6 Nov 2019 00:28:09 +0900 Subject: [PATCH] Fix codes --- src/flake8/processor.py | 9 +++--- tests/unit/test_file_processor.py | 50 ++++++++++++++++++------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index d92f0ba..0fd650b 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -36,7 +36,6 @@ class FileProcessor(object): - :attr:`blank_before` - :attr:`blank_lines` - :attr:`checker_state` - - :attr:`disable_noqa` - :attr:`indent_char` - :attr:`indent_level` - :attr:`line_number` @@ -78,8 +77,6 @@ class FileProcessor(object): self._checker_states = {} # type: Dict[str, Dict[Any, Any]] #: Current checker state self.checker_state = {} # type: Dict[Any, Any] - #: Disable all noqa comments - self.disable_noqa = options.disable_noqa # type: bool #: User provided option for hang closing self.hang_closing = options.hang_closing #: Character used for indentation @@ -349,8 +346,10 @@ class FileProcessor(object): :rtype: bool """ - if not self.disable_noqa \ - and any(defaults.NOQA_FILE.match(line) for line in self.lines): + if ( + not self.options.disable_noqa + and any(defaults.NOQA_FILE.match(line) for line in self.lines) + ): return True elif any(defaults.NOQA_FILE.search(line) for line in self.lines): LOG.warning( diff --git a/tests/unit/test_file_processor.py b/tests/unit/test_file_processor.py index ad2f5bd..9acd5ae 100644 --- a/tests/unit/test_file_processor.py +++ b/tests/unit/test_file_processor.py @@ -57,32 +57,42 @@ def test_strip_utf_bom(first_line, default_options): assert file_processor.lines[0] == '"""Module docstring."""\n' -@pytest.mark.parametrize('lines, expected, disable_noqa', [ - (['\xEF\xBB\xBF"""Module docstring."""\n'], False, False), - ([u'\uFEFF"""Module docstring."""\n'], False, False), - (['#!/usr/bin/python', '# flake8 is great', 'a = 1'], False, False), - (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], True, False), - (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], False, True), - (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], True, False), - (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], False, True), - (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], True, False), - (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], False, True), - (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], True, False), - (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], False, True), - (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], True, False), - (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], False, True), - (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], True, False), - (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], False, True), - (['#!/usr/bin/python', 'a = 1 # flake8: noqa'], False, False), - (['#!/usr/bin/python', 'a = 1 # flake8:noqa'], False, False), +@pytest.mark.parametrize('lines, expected', [ + (['\xEF\xBB\xBF"""Module docstring."""\n'], False), + ([u'\uFEFF"""Module docstring."""\n'], False), + (['#!/usr/bin/python', '# flake8 is great', 'a = 1'], False), + (['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], True), + (['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], True), + (['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], True), + (['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], True), + (['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], True), + (['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], True), + (['#!/usr/bin/python', 'a = 1 # flake8: noqa'], False), + (['#!/usr/bin/python', 'a = 1 # flake8:noqa'], False), ]) -def test_should_ignore_file(lines, expected, disable_noqa, default_options): +def test_should_ignore_file(lines, expected, default_options): """Verify that we ignore a file if told to.""" - default_options.disable_noqa = disable_noqa file_processor = processor.FileProcessor('-', default_options, lines) assert file_processor.should_ignore_file() is expected +@pytest.mark.parametrize('lines', [ + ['#!/usr/bin/python', '# flake8: noqa', 'a = 1'], + ['#!/usr/bin/python', '# flake8:noqa', 'a = 1'], + ['# flake8: noqa', '#!/usr/bin/python', 'a = 1'], + ['# flake8:noqa', '#!/usr/bin/python', 'a = 1'], + ['#!/usr/bin/python', 'a = 1', '# flake8: noqa'], + ['#!/usr/bin/python', 'a = 1', '# flake8:noqa'], +]) +def test_should_ignore_file_to_handle_disable_noqa(lines, default_options): + """Verify that we ignore a file if told to.""" + file_processor = processor.FileProcessor('-', default_options, lines) + assert file_processor.should_ignore_file() is True + default_options.disable_noqa = True + file_processor = processor.FileProcessor('-', default_options, lines) + assert file_processor.should_ignore_file() is False + + @mock.patch('flake8.utils.stdin_get_value') def test_read_lines_from_stdin(stdin_get_value, default_options): """Verify that we use our own utility function to retrieve stdin."""