github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-tools-src/grumpy_tools/coverparse.py (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  from __future__ import print_function
    20  
    21  import collections
    22  import re
    23  import sys
    24  
    25  try:
    26    xrange          # Python 2
    27  except NameError:
    28    xrange = range  # Python 3
    29  
    30  cover_re = re.compile(r'([^:]+):(\d+)\.\d+,(\d+).\d+ \d+ (\d+)$')
    31  
    32  
    33  def _ParseCover(f):
    34    """Return a dict of sets with uncovered line numbers from a Go cover file."""
    35    uncovered = collections.defaultdict(set)
    36    for line in f:
    37      match = cover_re.match(line.rstrip())
    38      if not match:
    39        raise RuntimeError('invalid coverage line: {!r}'.format(line))
    40      filename, line_start, line_end, count = match.groups()
    41      if not int(count):
    42        for i in xrange(int(line_start), int(line_end) + 1):
    43          uncovered[filename].add(i)
    44    return uncovered
    45  
    46  
    47  def main():
    48    with open(sys.argv[1]) as f:
    49      f.readline()
    50      uncovered = _ParseCover(f)
    51    for filename in sorted(uncovered.keys()):
    52      for lineno in sorted(uncovered[filename]):
    53        print('{}:{}'.format(filename, lineno))
    54  
    55  
    56  if __name__ == '__main__':
    57    main()