Skip to content

Commit 766f918

Browse files
authored
Fix map_function for invalid/unmatched "Defined" (#74)
Fixes #73.
1 parent 05ea7b6 commit 766f918

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

covimerage/__init__.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,14 @@ def map_function(self, f):
464464
# Assign counts from function to script.
465465
for [f_lnum, f_line] in f.lines.items():
466466
s_lnum = script_lnum + f_lnum
467-
s_line = script.lines[s_lnum]
467+
try:
468+
s_line = script.lines[s_lnum]
469+
except KeyError:
470+
logger.warning(
471+
"Could not find script line for function %s (%d, %d)",
472+
f.name, script_lnum, f_lnum,
473+
)
474+
return False
468475

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

488-
assert 0, 'Script line matches function line.'
495+
logger.warning(
496+
"Script line does not match function line, "
497+
"ignoring: %r != %r.",
498+
script_source,
499+
f_line.line,
500+
)
501+
return False
489502

490503
if f_line.count is not None:
491504
script.parse_function(script_lnum + f_lnum,

tests/test_main.py

+63
Original file line numberDiff line numberDiff line change
@@ -571,3 +571,66 @@ def test_duplicate_s_function(caplog):
571571
(N, 'endfunction')]
572572

573573
assert not caplog.records
574+
575+
576+
@pytest.mark.parametrize("defined_at", (-1, 1))
577+
def test_handles_unmatched_defined(defined_at, caplog):
578+
from covimerage import Profile
579+
580+
file_object = StringIO(textwrap.dedent(
581+
"""
582+
SCRIPT invalid_defined.vim
583+
Sourced 1 time
584+
Total time: 0.000037
585+
Self time: 0.000032
586+
587+
count total (s) self (s)
588+
1 0.000015 execute "function! F_via_execute_1()\\nreturn 0\\nendfunction"
589+
1 0.000011 0.000007 call F_via_execute_1()
590+
1 0.000006 0.000005 call F_via_execute_1()
591+
592+
FUNCTION F_via_execute_1()
593+
Defined: invalid_defined.vim line {defined_at}
594+
Called 2 times
595+
Total time: 0.000005
596+
Self time: 0.000005
597+
598+
count total (s) self (s)
599+
2 0.000003 return 0
600+
601+
FUNCTIONS SORTED ON TOTAL TIME
602+
count total (s) self (s) function
603+
2 0.000005 F_via_execute_1()
604+
605+
FUNCTIONS SORTED ON SELF TIME
606+
count total (s) self (s) function
607+
2 0.000005 F_via_execute_1()
608+
""".format(
609+
defined_at=defined_at
610+
)))
611+
612+
p = Profile(file_object)
613+
p.parse()
614+
615+
assert len(p.scripts) == 1
616+
s = p.scripts[0]
617+
618+
assert [(l.count, l.line) for l in s.lines.values()
619+
if not l.line.startswith('"')] == [
620+
(1, 'execute "function! F_via_execute_1()\\nreturn 0\\nendfunction"'),
621+
(1, 'call F_via_execute_1()'),
622+
(1, 'call F_via_execute_1()'),
623+
]
624+
625+
logmsgs = [x[1:] for x in caplog.record_tuples]
626+
if defined_at == -1:
627+
assert logmsgs == [
628+
(30, "Could not find script line for function F_via_execute_1 (-1, 1)"),
629+
(40, "Could not find source for function: F_via_execute_1"),
630+
]
631+
else:
632+
assert defined_at == 1
633+
assert logmsgs == [
634+
(30, "Script line does not match function line, ignoring: 'call F_via_execute_1()' != 'return 0'."),
635+
(40, "Could not find source for function: F_via_execute_1"),
636+
]

0 commit comments

Comments
 (0)