Skip to content

Commit

Permalink
Fix map_function for invalid/unmatched "Defined" (#74)
Browse files Browse the repository at this point in the history
Fixes #73.
  • Loading branch information
blueyed authored Jun 1, 2019
1 parent 05ea7b6 commit 766f918
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
17 changes: 15 additions & 2 deletions covimerage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,14 @@ def map_function(self, f):
# Assign counts from function to script.
for [f_lnum, f_line] in f.lines.items():
s_lnum = script_lnum + f_lnum
s_line = script.lines[s_lnum]
try:
s_line = script.lines[s_lnum]
except KeyError:
logger.warning(
"Could not find script line for function %s (%d, %d)",
f.name, script_lnum, f_lnum,
)
return False

# XXX: might not be the same, since function lines
# are joined, while script lines might be spread
Expand All @@ -485,7 +492,13 @@ def map_function(self, f):
if script_source == f_line.line:
break

assert 0, 'Script line matches function line.'
logger.warning(
"Script line does not match function line, "
"ignoring: %r != %r.",
script_source,
f_line.line,
)
return False

if f_line.count is not None:
script.parse_function(script_lnum + f_lnum,
Expand Down
63 changes: 63 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,66 @@ def test_duplicate_s_function(caplog):
(N, 'endfunction')]

assert not caplog.records


@pytest.mark.parametrize("defined_at", (-1, 1))
def test_handles_unmatched_defined(defined_at, caplog):
from covimerage import Profile

file_object = StringIO(textwrap.dedent(
"""
SCRIPT invalid_defined.vim
Sourced 1 time
Total time: 0.000037
Self time: 0.000032
count total (s) self (s)
1 0.000015 execute "function! F_via_execute_1()\\nreturn 0\\nendfunction"
1 0.000011 0.000007 call F_via_execute_1()
1 0.000006 0.000005 call F_via_execute_1()
FUNCTION F_via_execute_1()
Defined: invalid_defined.vim line {defined_at}
Called 2 times
Total time: 0.000005
Self time: 0.000005
count total (s) self (s)
2 0.000003 return 0
FUNCTIONS SORTED ON TOTAL TIME
count total (s) self (s) function
2 0.000005 F_via_execute_1()
FUNCTIONS SORTED ON SELF TIME
count total (s) self (s) function
2 0.000005 F_via_execute_1()
""".format(
defined_at=defined_at
)))

p = Profile(file_object)
p.parse()

assert len(p.scripts) == 1
s = p.scripts[0]

assert [(l.count, l.line) for l in s.lines.values()
if not l.line.startswith('"')] == [
(1, 'execute "function! F_via_execute_1()\\nreturn 0\\nendfunction"'),
(1, 'call F_via_execute_1()'),
(1, 'call F_via_execute_1()'),
]

logmsgs = [x[1:] for x in caplog.record_tuples]
if defined_at == -1:
assert logmsgs == [
(30, "Could not find script line for function F_via_execute_1 (-1, 1)"),
(40, "Could not find source for function: F_via_execute_1"),
]
else:
assert defined_at == 1
assert logmsgs == [
(30, "Script line does not match function line, ignoring: 'call F_via_execute_1()' != 'return 0'."),
(40, "Could not find source for function: F_via_execute_1"),
]

0 comments on commit 766f918

Please sign in to comment.