github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/testing/cover.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Support for test coverage.
     6  
     7  package testing
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  )
    13  
    14  // CoverBlock records the coverage data for a single basic block.
    15  // NOTE: This struct is internal to the testing infrastructure and may change.
    16  // It is not covered (yet) by the Go 1 compatibility guidelines.
    17  type CoverBlock struct {
    18  	Line0 uint32
    19  	Col0  uint16
    20  	Line1 uint32
    21  	Col1  uint16
    22  	Stmts uint16
    23  }
    24  
    25  var cover Cover
    26  
    27  // Cover records information about test coverage checking.
    28  // NOTE: This struct is internal to the testing infrastructure and may change.
    29  // It is not covered (yet) by the Go 1 compatibility guidelines.
    30  type Cover struct {
    31  	Mode            string
    32  	Counters        map[string][]uint32
    33  	Blocks          map[string][]CoverBlock
    34  	CoveredPackages string
    35  }
    36  
    37  // RegisterCover records the coverage data accumulators for the tests.
    38  // NOTE: This function is internal to the testing infrastructure and may change.
    39  // It is not covered (yet) by the Go 1 compatibility guidelines.
    40  func RegisterCover(c Cover) {
    41  	cover = c
    42  }
    43  
    44  // mustBeNil checks the error and, if present, reports it and exits.
    45  func mustBeNil(err error) {
    46  	if err != nil {
    47  		fmt.Fprintf(os.Stderr, "testing: %s\n", err)
    48  		os.Exit(2)
    49  	}
    50  }
    51  
    52  // coverReport reports the coverage percentage and writes a coverage profile if requested.
    53  func coverReport() {
    54  	var f *os.File
    55  	var err error
    56  	if *coverProfile != "" {
    57  		f, err = os.Create(toOutputDir(*coverProfile))
    58  		mustBeNil(err)
    59  		fmt.Fprintf(f, "mode: %s\n", cover.Mode)
    60  		defer func() { mustBeNil(f.Close()) }()
    61  	}
    62  
    63  	var active, total int64
    64  	for name, counts := range cover.Counters {
    65  		blocks := cover.Blocks[name]
    66  		for i, count := range counts {
    67  			stmts := int64(blocks[i].Stmts)
    68  			total += stmts
    69  			if count > 0 {
    70  				active += stmts
    71  			}
    72  			if f != nil {
    73  				_, err := fmt.Fprintf(f, "%s:%d.%d,%d.%d %d %d\n", name,
    74  					blocks[i].Line0, blocks[i].Col0,
    75  					blocks[i].Line1, blocks[i].Col1,
    76  					stmts,
    77  					count)
    78  				mustBeNil(err)
    79  			}
    80  		}
    81  	}
    82  	if total == 0 {
    83  		total = 1
    84  	}
    85  	fmt.Printf("coverage: %.1f%% of statements%s\n", 100*float64(active)/float64(total), cover.CoveredPackages)
    86  }