def test_two_inputs_first_violate(self): # Construct the XML report file_paths = ['file1.java'] violations1 = self.MANY_VIOLATIONS violations2 = self.FEW_VIOLATIONS measured1 = self.FEW_MEASURED measured2 = self.MANY_MEASURED xml = self._coverage_xml(file_paths, violations1, measured1) xml2 = self._coverage_xml(file_paths, violations2, measured2) # Parse the report coverage = CloverXmlCoverageReporter([xml, xml2]) # By construction, each file has the same set # of covered/uncovered lines self.assertEqual( violations1 & violations2, coverage.violations('file1.java') ) self.assertEqual( measured1 | measured2, coverage.measured_lines('file1.java') )
def test_empty_violations(self): """ Test that an empty violations report is handled properly """ # Construct the XML report file_paths = ['file1.java'] violations1 = self.MANY_VIOLATIONS violations2 = set() measured1 = self.FEW_MEASURED measured2 = self.MANY_MEASURED xml = self._coverage_xml(file_paths, violations1, measured1) xml2 = self._coverage_xml(file_paths, violations2, measured2) # Parse the report coverage = CloverXmlCoverageReporter([xml2, xml]) # By construction, each file has the same set # of covered/uncovered lines self.assertEqual(violations1 & violations2, coverage.violations('file1.java')) self.assertEqual(measured1 | measured2, coverage.measured_lines('file1.java'))
def test_empty_violations(self): """ Test that an empty violations report is handled properly """ # Construct the XML report file_paths = ['file1.java'] violations1 = self.MANY_VIOLATIONS violations2 = set() measured1 = self.FEW_MEASURED measured2 = self.MANY_MEASURED xml = self._coverage_xml(file_paths, violations1, measured1) xml2 = self._coverage_xml(file_paths, violations2, measured2) # Parse the report coverage = CloverXmlCoverageReporter([xml2, xml]) # By construction, each file has the same set # of covered/uncovered lines self.assertEqual( violations1 & violations2, coverage.violations('file1.java') ) self.assertEqual( measured1 | measured2, coverage.measured_lines('file1.java') )
def test_three_inputs(self): # Construct the XML report file_paths = ['file1.java'] violations1 = self.MANY_VIOLATIONS violations2 = self.FEW_VIOLATIONS violations3 = self.ONE_VIOLATION measured1 = self.FEW_MEASURED measured2 = self.MANY_MEASURED measured3 = self.VERY_MANY_MEASURED xml = self._coverage_xml(file_paths, violations1, measured1) xml2 = self._coverage_xml(file_paths, violations2, measured2) xml3 = self._coverage_xml(file_paths, violations3, measured3) # Parse the report coverage = CloverXmlCoverageReporter([xml2, xml, xml3]) # By construction, each file has the same set # of covered/uncovered lines self.assertEqual(violations1 & violations2 & violations3, coverage.violations('file1.java')) self.assertEqual(measured1 | measured2 | measured3, coverage.measured_lines('file1.java'))
def test_no_such_file(self): # Construct the XML report with no source files xml = self._coverage_xml([], [], []) # Parse the report coverage = CloverXmlCoverageReporter(xml) # Expect that we get no results result = coverage.violations('file.java') self.assertEqual(result, set([]))
def test_different_files_in_inputs(self): # Construct the XML report xml_roots = [ self._coverage_xml(['file.java'], self.MANY_VIOLATIONS, self.FEW_MEASURED), self._coverage_xml(['other_file.java'], self.FEW_VIOLATIONS, self.MANY_MEASURED) ] # Parse the report coverage = CloverXmlCoverageReporter(xml_roots) self.assertEqual(self.MANY_VIOLATIONS, coverage.violations('file.java')) self.assertEqual(self.FEW_VIOLATIONS, coverage.violations('other_file.java'))
def test_violations(self): # Construct the XML report file_paths = ['file1.java', 'subdir/file2.java'] violations = self.MANY_VIOLATIONS measured = self.FEW_MEASURED xml = self._coverage_xml(file_paths, violations, measured) # Parse the report coverage = CloverXmlCoverageReporter(xml) # Expect that the name is set self.assertEqual(coverage.name(), "XML") # By construction, each file has the same set # of covered/uncovered lines self.assertEqual(violations, coverage.violations('file1.java')) self.assertEqual(measured, coverage.measured_lines('file1.java')) # Try getting a smaller range result = coverage.violations('subdir/file2.java') self.assertEqual(result, violations) # Once more on the first file (for caching) result = coverage.violations('file1.java') self.assertEqual(result, violations)
def generate_coverage_report(coverage_xml, compare_branch, html_report=None, css_file=None, ignore_staged=False, ignore_unstaged=False, exclude=None): """ Generate the diff coverage report, using kwargs from `parse_args()`. """ diff = GitDiffReporter(compare_branch, git_diff=GitDiffTool(), ignore_staged=ignore_staged, ignore_unstaged=ignore_unstaged, exclude=exclude) xml_roots = [cElementTree.parse(xml_root) for xml_root in coverage_xml] clover_xml_roots = [ clover_xml for clover_xml in xml_roots if clover_xml.findall('.[@clover]') ] cobertura_xml_roots = [ cobertura_xml for cobertura_xml in xml_roots if cobertura_xml.findall('.[@line-rate]') ] if clover_xml_roots and cobertura_xml_roots: raise TypeError("Can't handle mixed coverage reports") elif clover_xml_roots: coverage = CloverXmlCoverageReporter(clover_xml_roots) elif cobertura_xml_roots: coverage = XmlCoverageReporter(cobertura_xml_roots) # Build a report generator if html_report is not None: css_url = css_file if css_url is not None: css_url = os.path.relpath(css_file, os.path.dirname(html_report)) reporter = HtmlReportGenerator(coverage, diff, css_url=css_url) with open(html_report, "wb") as output_file: reporter.generate_report(output_file) if css_file is not None: with open(css_file, "wb") as output_file: reporter.generate_css(output_file) reporter = StringReportGenerator(coverage, diff) output_file = sys.stdout if six.PY2 else sys.stdout.buffer # Generate the report reporter.generate_report(output_file) return reporter.total_percent_covered()