def file_source(self, filename): """ Return a list of namedtuple `Line` for each line of code found in the given file `filename`. """ if self.cobertura1.has_file( filename) and self.cobertura1.filesystem.has_file(filename): lines1 = self.cobertura1.source_lines(filename) line_statuses1 = dict(self.cobertura1.line_statuses(filename)) else: lines1 = [] line_statuses1 = {} lines2 = self.cobertura2.source_lines(filename) line_statuses2 = dict(self.cobertura2.line_statuses(filename)) # Build a dict of lineno2 -> lineno1 lineno_map = reconcile_lines(lines2, lines1) # when we are using the same coverage file for both sides, we need to # translate the coverage of lines1 so that it corresponds to its # real lines. if self.cobertura1 == self.cobertura2: line_statuses1 = {} for l2, l1 in lineno_map.items(): line_statuses1[l1] = line_statuses2.get(l2) lines = [] for lineno, source in enumerate(lines2, start=1): status = None reason = None if lineno not in lineno_map: # line was added or removed, just use whatever coverage status # is available as there is nothing to compare against. status = line_statuses2.get(lineno) reason = "line-edit" else: other_lineno = lineno_map[lineno] line_status1 = line_statuses1.get(other_lineno) line_status2 = line_statuses2.get(lineno) if line_status1 is line_status2: status = None # unchanged reason = None elif line_status1 is True and line_status2 is False: status = False # decreased reason = "cov-down" elif line_status1 is False and line_status2 is True: status = True # increased reason = "cov-up" line = Line(lineno, source, status, reason) lines.append(line) return lines
def test_reconcile_lines__identical(): from pycobertura.utils import reconcile_lines lines1 = [ 'hello', # 1 'world', # 2 ] lines2 = [ 'hello', # 1 'world', # 2 ] assert reconcile_lines(lines1, lines2) == {1: 1, 2: 2}
def file_source(self, filename): """ Return a list of namedtuple `Line` for each line of code found in the given file `filename`. """ if self.cobertura1.has_file(filename) and \ self.cobertura1.filesystem.has_file(filename): lines1 = self.cobertura1.source_lines(filename) line_statuses1 = dict(self.cobertura1.line_statuses( filename)) else: lines1 = [] line_statuses1 = {} lines2 = self.cobertura2.source_lines(filename) line_statuses2 = dict(self.cobertura2.line_statuses(filename)) # Build a dict of lineno2 -> lineno1 lineno_map = reconcile_lines(lines2, lines1) lines = [] for lineno, source in enumerate(lines2, start=1): status = None reason = None if lineno not in lineno_map: # line was added or removed, just use whatever coverage status # is available as there is nothing to compare against. status = line_statuses2.get(lineno) reason = 'line-edit' else: other_lineno = lineno_map[lineno] line_status1 = line_statuses1.get(other_lineno) line_status2 = line_statuses2.get(lineno) if line_status1 is line_status2: status = None # unchanged reason = None elif line_status1 is True and line_status2 is False: status = False # decreased reason = 'cov-down' elif line_status1 is False and line_status2 is True: status = True # increased reason = 'cov-up' line = Line(lineno, source, status, reason) lines.append(line) return lines
def file_source(self, filename): """ Return a list of namedtuple `Line` for each line of code found in the given file `filename`. """ if self.cobertura1.has_file(filename) and \ self.cobertura1.filesystem.has_file(filename): lines1 = self.cobertura1.source_lines(filename) line_statuses1 = dict(self.cobertura1.line_statuses( filename)) else: lines1 = [] line_statuses1 = {} lines2 = self.cobertura2.source_lines(filename) line_statuses2 = dict(self.cobertura2.line_statuses(filename)) # Build a dict of lineno2 -> lineno1 lineno_map = reconcile_lines(lines2, lines1) lines = [] for lineno, source in enumerate(lines2, start=1): if lineno not in lineno_map: # line was added or removed, just use whatever coverage status # is available as there is nothing to compare against. status = line_statuses2.get(lineno) reason = 'line-edit' else: other_lineno = lineno_map[lineno] line_status1 = line_statuses1.get(other_lineno) line_status2 = line_statuses2.get(lineno) if line_status1 is line_status2: status = None # unchanged reason = None elif line_status1 is True and line_status2 is False: status = False # decreased reason = 'cov-down' elif line_status1 is False and line_status2 is True: status = True # increased reason = 'cov-up' line = Line(lineno, source, status, reason) lines.append(line) return lines
def test_reconcile_lines__less_to_more(): from pycobertura.utils import reconcile_lines lines1 = [ 'hello', # 1 'world', # 2 ] lines2 = [ 'dear all', # 1 'hello', # 2 'beautiful', # 3 'world', # 4 'of', # 5 'pain', # 6 ] assert reconcile_lines(lines1, lines2) == {1: 2, 2: 4}