github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/tools/coverparse (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2016 Google Inc. All Rights Reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  """Parse a Go coverage file and prints a message for lines missing coverage."""
    18  
    19  import collections
    20  import re
    21  import sys
    22  
    23  
    24  cover_re = re.compile(r'([^:]+):(\d+)\.\d+,(\d+).\d+ \d+ (\d+)$')
    25  
    26  
    27  def _ParseCover(f):
    28    """Return a dict of sets with uncovered line numbers from a Go cover file."""
    29    uncovered = collections.defaultdict(set)
    30    for line in f:
    31      match = cover_re.match(line.rstrip())
    32      if not match:
    33        raise RuntimeError('invalid coverage line: {!r}'.format(line))
    34      filename, line_start, line_end, count = match.groups()
    35      if not int(count):
    36        for i in xrange(int(line_start), int(line_end) + 1):
    37          uncovered[filename].add(i)
    38    return uncovered
    39  
    40  
    41  def main():
    42    with open(sys.argv[1]) as f:
    43      f.readline()
    44      uncovered = _ParseCover(f)
    45    for filename in sorted(uncovered.keys()):
    46      for lineno in sorted(uncovered[filename]):
    47        print '{}:{}'.format(filename, lineno)
    48  
    49  
    50  if __name__ == '__main__':
    51    main()