def test_check(self): """Test check() method.""" errors = [] def _mock_handle_style_error(line_number, category, confidence, message): error = (line_number, category, confidence, message) errors.append(error) current_dir = os.path.dirname(__file__) file_path = os.path.join(current_dir, "python_unittest_input.py") checker = PythonChecker(file_path, _mock_handle_style_error) checker.check() self.assertEqual( [ (2, 'pep8/W291', 5, 'trailing whitespace'), (3, 'pep8/E261', 5, 'at least two spaces before inline comment'), (3, 'pep8/E262', 5, "inline comment should start with '# '"), (2, 'pylint/C0303(trailing-whitespace)', 5, '[] Trailing whitespace'), (2, 'pylint/E0602(undefined-variable)', 5, u"[] Undefined variable 'error'"), (3, 'pylint/W0611(unused-import)', 5, '[] Unused import math'), ], errors)
def test_pylint_false_positives(self): """Test that pylint false positives are suppressed.""" errors = [] def _mock_handle_style_error(line_number, category, confidence, message): error = (line_number, category, confidence, message) errors.append(error) current_dir = os.path.dirname(__file__) file_path = os.path.join(current_dir, "python_unittest_falsepositives.py") checker = PythonChecker(file_path, _mock_handle_style_error) checker.check(lines=[]) self.assertEqual(errors, [])
def _create_checker(self, file_type, file_path, handle_style_error, min_confidence): """Instantiate and return a style checker based on file type.""" if file_type == FileType.NONE: checker = None elif file_type == FileType.CPP: file_extension = self._file_extension(file_path) checker = CppChecker(file_path, file_extension, handle_style_error, min_confidence) elif file_type == FileType.JSON: checker = JSONChecker(file_path, handle_style_error) elif file_type == FileType.PYTHON: checker = PythonChecker(file_path, handle_style_error) elif file_type == FileType.XML: checker = XMLChecker(file_path, handle_style_error) elif file_type == FileType.XCODEPROJ: checker = XcodeProjectFileChecker(file_path, handle_style_error) elif file_type == FileType.PNG: checker = PNGChecker(file_path, handle_style_error) elif file_type == FileType.TEXT: basename = os.path.basename(file_path) if basename == 'TestExpectations': checker = TestExpectationsChecker(file_path, handle_style_error) else: checker = TextChecker(file_path, handle_style_error) else: raise ValueError('Invalid file type "%(file_type)s": the only valid file types ' 'are %(NONE)s, %(CPP)s, and %(TEXT)s.' % {'file_type': file_type, 'NONE': FileType.NONE, 'CPP': FileType.CPP, 'TEXT': FileType.TEXT}) return checker
def test_init(self): """Test __init__() method.""" def _mock_handle_style_error(self): pass checker = PythonChecker("foo.txt", _mock_handle_style_error) self.assertEqual(checker._file_path, "foo.txt") self.assertEqual(checker._handle_style_error, _mock_handle_style_error)
def test_check(self): """Test check() method.""" errors = [] def _mock_handle_style_error(line_number, category, confidence, message): error = (line_number, category, confidence, message) errors.append(error) current_dir = os.path.dirname(__file__) file_path = os.path.join(current_dir, "python_unittest_input.py") checker = PythonChecker(file_path, _mock_handle_style_error) checker.check(lines=[]) # FIXME: https://bugs.webkit.org/show_bug.cgi?id=204133 expected_errors = [(4, "pep8/W291", 5, "trailing whitespace")] if sys.version_info < (3, 0): expected_errors.append((4, "pylint/E0602", 5, "Undefined variable 'error'")) self.assertEqual(errors, expected_errors)
def _create_checker(self, file_type, file_path, handle_style_error, min_confidence, commit_queue): """Instantiate and return a style checker based on file type.""" if file_type == FileType.NONE: checker = None elif file_type == FileType.CHANGELOG: should_line_be_checked = None if handle_style_error: should_line_be_checked = handle_style_error.should_line_be_checked checker = ChangeLogChecker(file_path, handle_style_error, should_line_be_checked) elif file_type == FileType.CPP: file_extension = self._file_extension(file_path) checker = CppChecker(file_path, file_extension, handle_style_error, min_confidence) elif file_type == FileType.JS: basename = os.path.basename(file_path) # Do not attempt to check non-Inspector or 3rd-party JavaScript files as JS. if os.path.join('WebInspectorUI', 'UserInterface') in file_path and (not 'External' in file_path): checker = JSChecker(file_path, handle_style_error) elif basename in _NEVER_SKIPPED_JS_FILES: checker = JSTestChecker(file_path, handle_style_error) else: checker = TextChecker(file_path, handle_style_error) elif file_type == FileType.JSON: basename = os.path.basename(file_path) if basename == 'contributors.json': if commit_queue: checker = JSONContributorsChecker(file_path, handle_style_error) else: checker = ContributorsChecker(file_path, handle_style_error) elif basename == 'features.json': checker = JSONFeaturesChecker(file_path, handle_style_error) elif basename == 'CSSProperties.json': checker = JSONCSSPropertiesChecker(file_path, handle_style_error) else: checker = JSONChecker(file_path, handle_style_error) elif file_type == FileType.PYTHON: python3_paths = ['Tools/resultsdbpy'] for partial in python3_paths: if file_path.startswith(partial): return Python3Checker(file_path, handle_style_error) if apple_additions(): checker = apple_additions().python_checker( file_path, handle_style_error) else: checker = PythonChecker(file_path, handle_style_error) elif file_type == FileType.XML: checker = XMLChecker(file_path, handle_style_error) elif file_type == FileType.XCODEPROJ: checker = XcodeProjectFileChecker(file_path, handle_style_error) elif file_type == FileType.PNG: checker = PNGChecker(file_path, handle_style_error) elif file_type == FileType.CMAKE: checker = CMakeChecker(file_path, handle_style_error) elif file_type == FileType.TEXT: basename = os.path.basename(file_path) if basename == 'TestExpectations': checker = TestExpectationsChecker(file_path, handle_style_error) elif file_path.endswith('.messages.in'): checker = MessagesInChecker(file_path, handle_style_error) else: checker = TextChecker(file_path, handle_style_error) elif file_type == FileType.WATCHLIST: checker = WatchListChecker(file_path, handle_style_error) elif file_type == FileType.FEATUREDEFINES: checker = FeatureDefinesChecker(file_path, handle_style_error) elif file_type == FileType.SDKVARIANT: checker = SDKVariantChecker(file_path, handle_style_error) else: raise ValueError( 'Invalid file type "%(file_type)s": the only valid file types ' "are %(NONE)s, %(CPP)s, and %(TEXT)s." % { "file_type": file_type, "NONE": FileType.NONE, "CPP": FileType.CPP, "TEXT": FileType.TEXT }) return checker