def _src_path_stats(self, src_path): """ Return a dict of statistics for the source file at `src_path`. """ # Find violation lines violation_lines = self.violation_lines(src_path) violations = sorted(self._diff_violations()[src_path].violations) # Load source snippets (if the report will display them) # If we cannot load the file, then fail gracefully if self.INCLUDE_SNIPPETS: try: snippets = Snippet.load_snippets_html(src_path, violation_lines) except IOError: snippets = [] else: snippets = [] return { 'percent_covered': self.percent_covered(src_path), 'violation_lines': TemplateReportGenerator.combine_adjacent_lines(violation_lines), 'violations': violations, 'snippets_html': snippets }
def test_load_snippets_html(self): # Need to be in the fixture directory # so the source path is displayed correctly old_cwd = os.getcwd() self.addCleanup(lambda: os.chdir(old_cwd)) os.chdir(fixture_path('')) src_path = fixture_path('snippet_src.py') self._init_src_file(100, src_path) # One higher-level test to make sure # the snippets are being rendered correctly violations = [10, 12, 13, 50, 51, 54, 55, 57] snippets_html = '\n\n'.join( Snippet.load_snippets_html('snippet_src.py', violations) ) # Load the fixture for the expected contents expected_path = fixture_path('snippet_list.html') with open(expected_path) as fixture_file: expected = fixture_file.read() # Check that we got what we expected assert_long_str_equal(expected, snippets_html, strip=True)
def test_load_snippets_non_ascii(self): # Need to be in the fixture directory # so the source path is displayed correctly old_cwd = os.getcwd() self.addCleanup(lambda: os.chdir(old_cwd)) os.chdir(fixture_path('')) src_path = fixture_path('snippet_src.py') self._init_src_file(100, src_path) # One higher-level test to make sure # the snippets are being rendered correctly violations = [10, 12, 13, 50, 51, 54, 55, 57] snippets_html = '\n\n'.join( Snippet.load_snippets_html('snippet_non_ascii.py', violations) )
def _compare_snippets_html_output(self, filename, violations, expected_out_filename): # Need to be in the fixture directory # so the source path is displayed correctly old_cwd = os.getcwd() self.addCleanup(lambda: os.chdir(old_cwd)) os.chdir(fixture_path("")) # One higher-level test to make sure # the snippets are being rendered correctly snippets_html = "\n\n".join(Snippet.load_snippets_html(filename, violations)) # Load the fixture for the expected contents expected_path = fixture_path(expected_out_filename) with open(expected_path) as fixture_file: expected = fixture_file.read() if isinstance(expected, six.binary_type): expected = expected.decode("utf-8") # Check that we got what we expected assert_long_str_equal(expected, snippets_html, strip=True)
def _compare_snippets_html_output(self, filename, violations, expected_out_filename): # Need to be in the fixture directory # so the source path is displayed correctly old_cwd = os.getcwd() self.addCleanup(lambda: os.chdir(old_cwd)) os.chdir(fixture_path('')) # One higher-level test to make sure # the snippets are being rendered correctly snippets_html = '\n\n'.join( Snippet.load_snippets_html(filename, violations) ) # Load the fixture for the expected contents expected_path = fixture_path(expected_out_filename) with open(expected_path) as fixture_file: expected = fixture_file.read() if isinstance(expected, six.binary_type): expected = expected.decode('utf-8') # Check that we got what we expected assert_long_str_equal(expected, snippets_html, strip=True)
def _src_path_stats(self, src_path): stats = super()._src_path_stats(src_path) # Load source snippets (if the report will display them) # If we cannot load the file, then fail gracefully if self.INCLUDE_SNIPPETS: try: snippets = Snippet.load_snippets_html(src_path, stats["violation_lines"]) except OSError: snippets = [] else: snippets = [] stats.update({ "snippets_html": snippets, "violation_lines": TemplateReportGenerator.combine_adjacent_lines( stats["violation_lines"]), }) return stats