def test_is_user_ignored_ignores_errors(ignore_list, error_code): """Verify we detect users explicitly ignoring an error.""" guide = style_guide.StyleGuide(create_options(ignore=ignore_list), listener_trie=None, formatter=None) assert guide.is_user_ignored(error_code) is style_guide.Ignored.Explicitly
def test_is_user_ignored_implicitly_selects_errors(ignore_list, error_code): """Verify we detect users does not explicitly ignore an error.""" guide = style_guide.StyleGuide(create_options(ignore=ignore_list), listener_trie=None, formatter=None) assert guide.is_user_ignored(error_code) is style_guide.Selected.Implicitly
def test_is_user_selected_excludes_errors(select_list, error_code): """Verify we detect users implicitly excludes an error.""" guide = style_guide.StyleGuide(create_options(select=select_list), listener_trie=None, formatter=None) assert guide.is_user_selected(error_code) is style_guide.Ignored.Implicitly
def test_should_report_error(select_list, ignore_list, error_code, expected): """Verify we decide when to report an error.""" guide = style_guide.StyleGuide(create_options(select=select_list, ignore=ignore_list), listener_trie=None, formatter=None) assert guide.should_report_error(error_code) is expected
def test_style_guide_applies_to(style_guide_file, filename, expected): """Verify that we match a file to its style guide.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options() guide = style_guide.StyleGuide(options, formatter=formatter, filename=style_guide_file) assert guide.applies_to(filename) is expected
def test_is_user_selected_selects_errors(select_list, error_code): """Verify we detect users explicitly selecting an error.""" guide = style_guide.StyleGuide(create_options(select=select_list), listener_trie=None, formatter=None) assert (guide.is_user_selected(error_code) is style_guide.Selected.Explicitly)
def test_handle_error_does_not_raise_type_errors(): """Verify that we handle our inputs better.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) guide = style_guide.StyleGuide(create_options(select=['T111'], ignore=[]), formatter=formatter) assert 1 == guide.handle_error( 'T111', 'file.py', 1, None, 'error found', 'a = 1' )
def make_guide(self): # type: () -> NoneType """Initialize our StyleGuide.""" if self.guide is None: self.guide = style_guide.StyleGuide( self.options, self.listener_trie, self.formatter ) if self.running_against_diff: self.guide.add_diff_ranges(self.parsed_diff)
def test_handle_error_does_not_raise_type_errors(): """Verify that we handle our inputs better.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) guide = style_guide.StyleGuide( create_options(select=["T111"], ignore=[]), formatter=formatter, stats=statistics.Statistics(), ) assert 1 == guide.handle_error("T111", "file.py", 1, None, "error found", "a = 1")
def test_disable_is_inline_ignored(): """Verify that is_inline_ignored exits immediately if disabling NoQA.""" guide = style_guide.StyleGuide(create_options(disable_noqa=True), listener_trie=None, formatter=None) error = style_guide.Error('E121', 'filename.py', 1, 1, 'error text', 'line') with mock.patch('linecache.getline') as getline: assert guide.is_inline_ignored(error) is False assert getline.called is False
def test_is_inline_ignored(error_code, physical_line, expected_result): """Verify that we detect inline usage of ``# noqa``.""" guide = style_guide.StyleGuide(create_options(select=['E', 'W', 'F']), listener_trie=None, formatter=None) error = style_guide.Error(error_code, 'filename.py', 1, 1, 'error text', None) # We want `None` to be passed as the physical line so we actually use our # monkey-patched linecache.getline value. with mock.patch('linecache.getline', return_value=physical_line): assert guide.is_inline_ignored(error) is expected_result
def test_handle_error_does_not_notify_listeners(select_list, ignore_list, error_code): """Verify that error codes notify the listener trie appropriately.""" listener_trie = mock.create_autospec(notifier.Notifier, instance=True) formatter = mock.create_autospec(base.BaseFormatter, instance=True) guide = style_guide.StyleGuide(create_options(select=select_list, ignore=ignore_list), listener_trie=listener_trie, formatter=formatter) with mock.patch('linecache.getline', return_value=''): guide.handle_error(error_code, 'stdin', 1, 1, 'error found') assert listener_trie.notify.called is False assert formatter.handle.called is False
def test_is_user_selected_implicitly_selects_errors(): """Verify we detect users implicitly selecting an error.""" select_list = [] error_code = 'E121' guide = style_guide.StyleGuide( create_options( select=select_list, extended_default_select=['E'], ), listener_trie=None, formatter=None, ) assert (guide.is_user_selected(error_code) is style_guide.Selected.Implicitly)
def test_decision_for_logic(select, ignore, extend_select, enabled_extensions, error_code, expected): """Verify the complicated logic of StyleGuide._decision_for. I usually avoid testing private methods, but this one is very important in our conflict resolution work in Flake8. """ guide = style_guide.StyleGuide( create_options( select=select, ignore=ignore, extended_default_select=extend_select, enable_extensions=enabled_extensions, ), listener_trie=None, formatter=None, ) assert guide._decision_for(error_code) is expected