def test_quality_error(self): # Patch the output stderr/stdout and returncode of `checkstyle` _setup_patch( ( dedent(""" <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="8.0"> <file name="file1.java"> <error line="1" severity="error" message="Missing docstring"/> </file> </checkstyle> """), b"oops", ), status_code=1, ) # Parse the report with patch( "diff_cover.violationsreporters.java_violations_reporter.run_command_for_code" ) as code: code.return_value = 0 quality = QualityReporter(CheckstyleXmlDriver()) # Expect an error self.assertRaises(CommandError, quality.violations, "file1.java")
def test_no_java_file(self): quality = QualityReporter(CheckstyleXmlDriver()) file_paths = ['file1.coffee', 'subdir/file2.js'] # Expect that we get no results because no Java files for path in file_paths: result = quality.violations(path) self.assertEqual(result, [])
def test_quality_error(self, mocker, process_patcher): # Patch the output stderr/stdout and returncode of `checkstyle` process_patcher( ( dedent(""" <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="8.0"> <file name="file1.java"> <error line="1" severity="error" message="Missing docstring"/> </file> </checkstyle> """), b"oops", ), status_code=1, ) # Parse the report code = mocker.patch( "diff_cover.violationsreporters.java_violations_reporter.run_command_for_code" ) code.return_value = 0 quality = QualityReporter(CheckstyleXmlDriver()) with pytest.raises(CommandError): quality.violations("file1.java")
def test_no_java_file(self): quality = QualityReporter(CheckstyleXmlDriver()) file_paths = ["file1.coffee", "subdir/file2.js"] # Expect that we get no results because no Java files for path in file_paths: result = quality.violations(path) assert result == []
def test_quality(self): # Patch the output of `checkstyle` _setup_patch( ( dedent( """ <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="8.0"> <file name="file1.java"> <error line="1" severity="error" message="Missing docstring"/> <error line="2" severity="error" message="Unused variable 'd'"/> <error line="2" severity="warning" message="TODO: Not the real way we'll store usages!"/> <error line="579" severity="error" message="Unable to import 'rooted_paths'"/> <error line="113" severity="error" message="Unused argument 'cls'"/> <error line="150" severity="error" message="error while code parsing ([Errno 2] No such file or directory)"/> <error line="149" severity="error" message="Comma not followed by a space"/> </file> <file name="path/to/file2.java"> <error line="100" severity="error" message="Access to a protected member"/> </file> </checkstyle> """ ) .strip() .encode("ascii"), "", ) ) expected_violations = [ Violation(1, "error: Missing docstring"), Violation(2, "error: Unused variable 'd'"), Violation(2, "warning: TODO: Not the real way we'll store usages!"), Violation(579, "error: Unable to import 'rooted_paths'"), Violation( 150, "error: error while code parsing ([Errno 2] No such file or directory)", ), Violation(149, "error: Comma not followed by a space"), Violation(113, "error: Unused argument 'cls'"), ] # Parse the report quality = QualityReporter(CheckstyleXmlDriver()) # Expect that the name is set self.assertEqual(quality.name(), "checkstyle") # Measured_lines is undefined for a # quality reporter since all lines are measured self.assertIsNone(quality.measured_lines("file1.java")) # Expect that we get violations for file1.java only # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations("file1.java") self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
def test_quality_pregenerated_report(self): # When the user provides us with a pre-generated checkstyle report # then use that instead of calling checkstyle directly. checkstyle_reports = [ BytesIO( dedent(""" <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="8.0"> <file name="path/to/file.java"> <error line="1" severity="error" message="Missing docstring"/> <error line="57" severity="warning" message="TODO the name of this method is a little bit confusing"/> </file> <file name="another/file.java"> <error line="41" severity="error" message="Specify string format arguments as logging function parameters"/> <error line="175" severity="error" message="Operator not preceded by a space"/> <error line="259" severity="error" message="Invalid name '' for type variable (should match [a-z_][a-z0-9_]{2,30}$)"/> </file> </checkstyle> """).strip().encode("utf-8")), BytesIO( dedent(""" <?xml version="1.0" encoding="UTF-8"?> <checkstyle version="8.0"> <file name="path/to/file.java"> <error line="183" severity="error" message="Invalid name '' for type argument (should match [a-z_][a-z0-9_]{2,30}$)"/> </file> <file name="another/file.java"> <error line="183" severity="error" message="Missing docstring"/> </file> </checkstyle> """).strip().encode("utf-8")), ] # Generate the violation report quality = QualityReporter(CheckstyleXmlDriver(), reports=checkstyle_reports) # Expect that we get the right violations expected_violations = [ Violation(1, "error: Missing docstring"), Violation( 57, "warning: TODO the name of this method is a little bit confusing" ), Violation( 183, "error: Invalid name '' for type argument (should match [a-z_][a-z0-9_]{2,30}$)", ), ] # We're not guaranteed that the violations are returned # in any particular order. actual_violations = quality.violations("path/to/file.java") self.assertEqual(len(actual_violations), len(expected_violations)) for expected in expected_violations: self.assertIn(expected, actual_violations)
pycodestyle_driver, pydocstyle_driver, pyflakes_driver, ) QUALITY_DRIVERS = { "cppcheck": CppcheckDriver(), "pycodestyle": pycodestyle_driver, "pyflakes": pyflakes_driver, "pylint": PylintDriver(), "flake8": flake8_driver, "jshint": jshint_driver, "eslint": eslint_driver, "pydocstyle": pydocstyle_driver, "checkstyle": checkstyle_driver, "checkstylexml": CheckstyleXmlDriver(), "findbugs": FindbugsXmlDriver(), "pmd": PmdXmlDriver(), } VIOLATION_CMD_HELP = "Which code quality tool to use (%s)" % "/".join( sorted(QUALITY_DRIVERS)) INPUT_REPORTS_HELP = "Which violations reports to use" OPTIONS_HELP = "Options to be passed to the violations tool" INCLUDE_HELP = "Files to include (glob pattern)" LOGGER = logging.getLogger(__name__) def parse_quality_args(argv): """
pycodestyle_driver, ) from diff_cover.violationsreporters.java_violations_reporter import ( CheckstyleXmlDriver, checkstyle_driver, FindbugsXmlDriver, PmdXmlDriver) QUALITY_DRIVERS = { 'cppcheck' : CppcheckDriver(), 'pycodestyle': pycodestyle_driver, 'pyflakes': pyflakes_driver, 'pylint': PylintDriver(), 'flake8': flake8_driver, 'jshint': jshint_driver, 'eslint': eslint_driver, 'pydocstyle': pydocstyle_driver, 'checkstyle': checkstyle_driver, 'checkstylexml': CheckstyleXmlDriver(), 'findbugs': FindbugsXmlDriver(), 'pmd': PmdXmlDriver() } VIOLATION_CMD_HELP = "Which code quality tool to use (%s)" % "/".join(sorted(QUALITY_DRIVERS)) INPUT_REPORTS_HELP = "Which violations reports to use" OPTIONS_HELP = "Options to be passed to the violations tool" LOGGER = logging.getLogger(__name__) def parse_quality_args(argv): """ Parse command line arguments, returning a dict of
def test_no_such_file(self): quality = QualityReporter(CheckstyleXmlDriver()) # Expect that we get no results result = quality.violations('') self.assertEqual(result, [])