1212import subprocess
1313import sys
1414from collections import OrderedDict
15- from typing import Dict , List
15+ from typing import Dict , List , Optional
1616
1717import junit_xml
1818
1919from materialize import ROOT , ci_util
2020
21- Coverage = Dict [str , OrderedDict [int , int ]]
21+ # None value indicates that this line is interesting, but we don't know yet if
22+ # it can actually be covered. Int values indicate that the line can be covered
23+ # and how often is has been covered.
24+ Coverage = Dict [str , OrderedDict [int , Optional [int ]]]
2225SOURCE_RE = re .compile (
2326 "^/var/lib/buildkite-agent/builds/buildkite-.*/materialize/coverage/(.*$)"
2427)
@@ -47,12 +50,12 @@ def find_modified_lines() -> Coverage:
4750 line = line_raw .decode ("utf-8" )
4851 # +++ b/src/adapter/src/coord/command_handler.rs
4952 if line .startswith ("+++" ):
53+ file = line .removeprefix ("+++ b/" )
5054 if not line .endswith (".rs" ):
5155 continue
52- file = line .removeprefix ("+++ b/" )
5356 coverage [file ] = OrderedDict ()
5457 # @@ -641,7 +640,6 @@ impl Coordinator {
55- elif line .startswith ("@@ " ) and file :
58+ elif line .startswith ("@@ " ) and file in coverage :
5659 # We only care about the second value ("+640,6" in the example),
5760 # which contains the line number and length of the modified block
5861 # in new code state.
@@ -63,7 +66,7 @@ def find_modified_lines() -> Coverage:
6366 start = int (parts )
6467 length = 1
6568 for line_nr in range (start , start + length ):
66- coverage [file ][line_nr ] = 0
69+ coverage [file ][line_nr ] = None
6770 return coverage
6871
6972
@@ -92,7 +95,7 @@ def mark_covered_lines(lcov_file: str, coverage: Coverage) -> None:
9295 line_nr = int (line_str )
9396 hit = int (hit_str ) if hit_str .isnumeric () else int (float (hit_str ))
9497 if line_nr in coverage [file ]:
95- coverage [file ][line_nr ] += hit
98+ coverage [file ][line_nr ] = ( coverage [ file ][ line_nr ] or 0 ) + hit
9699
97100
98101def get_report (coverage : Coverage ) -> str :
@@ -106,7 +109,7 @@ def get_report(coverage: Coverage) -> str:
106109 content = f .readlines ()
107110 f .seek (0 )
108111 for i , line in enumerate (content ):
109- if i + 1 not in lines or lines [ i + 1 ] > 0 :
112+ if lines . get ( i + 1 ) != 0 :
110113 f .write (line )
111114 f .truncate ()
112115
0 commit comments