Commit b2f964b5 authored by Stefan Behnel's avatar Stefan Behnel

coverage: do not mark non-code lines as 'excluded' to make them show up normally in the report

parent be5bea91
...@@ -74,13 +74,13 @@ class Plugin(CoveragePlugin): ...@@ -74,13 +74,13 @@ class Plugin(CoveragePlugin):
filename = os.path.abspath(filename) filename = os.path.abspath(filename)
if self._c_files_map and filename in self._c_files_map: if self._c_files_map and filename in self._c_files_map:
c_file, rel_file_path, code, excluded = self._c_files_map[filename] c_file, rel_file_path, code = self._c_files_map[filename]
else: else:
c_file, _ = self._find_source_files(filename) c_file, _ = self._find_source_files(filename)
if not c_file: if not c_file:
return None # unknown file return None # unknown file
rel_file_path, code, excluded = self._parse_lines(c_file, filename) rel_file_path, code = self._parse_lines(c_file, filename)
return CythonModuleReporter(c_file, filename, rel_file_path, code, excluded) return CythonModuleReporter(c_file, filename, rel_file_path, code)
def _find_source_files(self, filename): def _find_source_files(self, filename):
basename, ext = os.path.splitext(filename) basename, ext = os.path.splitext(filename)
...@@ -155,7 +155,6 @@ class Plugin(CoveragePlugin): ...@@ -155,7 +155,6 @@ class Plugin(CoveragePlugin):
match_comment_end = re.compile(r' *[*]/$').match match_comment_end = re.compile(r' *[*]/$').match
code_lines = defaultdict(dict) code_lines = defaultdict(dict)
max_line = defaultdict(int)
filenames = set() filenames = set()
with open(c_file) as lines: with open(c_file) as lines:
lines = iter(lines) lines = iter(lines)
...@@ -166,7 +165,6 @@ class Plugin(CoveragePlugin): ...@@ -166,7 +165,6 @@ class Plugin(CoveragePlugin):
filename, lineno = match.groups() filename, lineno = match.groups()
filenames.add(filename) filenames.add(filename)
lineno = int(lineno) lineno = int(lineno)
max_line[filename] = max(max_line[filename], lineno)
for comment_line in lines: for comment_line in lines:
match = match_current_code_line(comment_line) match = match_current_code_line(comment_line)
if match: if match:
...@@ -176,21 +174,15 @@ class Plugin(CoveragePlugin): ...@@ -176,21 +174,15 @@ class Plugin(CoveragePlugin):
# unexpected comment format - false positive? # unexpected comment format - false positive?
break break
excluded_lines = dict(
(filename, set(range(1, max_line[filename] + 1)) - set(lines))
for filename, lines in code_lines.iteritems()
)
if self._c_files_map is None: if self._c_files_map is None:
self._c_files_map = {} self._c_files_map = {}
for filename in filenames: for filename in filenames:
abs_path = _find_dep_file_path(c_file, filename) abs_path = _find_dep_file_path(c_file, filename)
self._c_files_map[abs_path] = ( self._c_files_map[abs_path] = (c_file, filename, code_lines[filename])
c_file, filename, code_lines[filename], excluded_lines[filename])
if sourcefile not in self._c_files_map: if sourcefile not in self._c_files_map:
return (None,) * 3 # shouldn't happen ... return (None,) * 2 # shouldn't happen ...
return self._c_files_map[sourcefile][1:] return self._c_files_map[sourcefile][1:]
...@@ -218,7 +210,7 @@ class CythonModuleTracer(FileTracer): ...@@ -218,7 +210,7 @@ class CythonModuleTracer(FileTracer):
assert self._c_files_map is not None assert self._c_files_map is not None
if abs_path not in self._c_files_map: if abs_path not in self._c_files_map:
self._c_files_map[abs_path] = (self.c_file, source_file, None, None) self._c_files_map[abs_path] = (self.c_file, source_file, None)
return abs_path return abs_path
...@@ -226,26 +218,22 @@ class CythonModuleReporter(FileReporter): ...@@ -226,26 +218,22 @@ class CythonModuleReporter(FileReporter):
""" """
Provide detailed trace information for one source file to coverage.py. Provide detailed trace information for one source file to coverage.py.
""" """
def __init__(self, c_file, source_file, rel_file_path, code, excluded): def __init__(self, c_file, source_file, rel_file_path, code):
super(CythonModuleReporter, self).__init__(source_file) super(CythonModuleReporter, self).__init__(source_file)
self.name = rel_file_path self.name = rel_file_path
self.c_file = c_file self.c_file = c_file
self._code = code self._code = code
self._excluded = excluded
def statements(self): def statements(self):
return self._code.viewkeys() return self._code.viewkeys()
def excluded_statements(self): def _iter_source_tokens(self):
return self._excluded
def _iter_source_lines(self):
current_line = 1 current_line = 1
for line_no, code_line in sorted(self._code.iteritems()): for line_no, code_line in sorted(self._code.iteritems()):
while line_no > current_line: while line_no > current_line:
yield '' yield []
current_line += 1 current_line += 1
yield code_line yield [('txt', code_line)]
current_line += 1 current_line += 1
def source(self): def source(self):
...@@ -253,13 +241,15 @@ class CythonModuleReporter(FileReporter): ...@@ -253,13 +241,15 @@ class CythonModuleReporter(FileReporter):
with open(self.filename) as f: with open(self.filename) as f:
return f.read() return f.read()
else: else:
return '\n'.join(self._iter_source_lines()) return '\n'.join(
(tokens[0][1] if tokens else '')
for tokens in self._iter_source_tokens())
def source_token_lines(self): def source_token_lines(self):
if os.path.exists(self.filename): if os.path.exists(self.filename):
with open(self.filename) as f: with open(self.filename) as f:
for line in f: for line in f:
yield [('txt', line)] yield [('txt', line.rstrip('\n'))]
else: else:
for line in self._iter_source_lines(): for line in self._iter_source_tokens():
yield [('txt', line)] yield [('txt', line)]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment