...
Run Format

Text file src/cmd/go/testdata/script/cover_coverpkg_with_init.txt

Documentation: cmd/go/testdata/script

     1
     2# Testcase inspired by issue #58770, intended to verify that we're
     3# doing the right thing when running "go test -coverpkg=./... ./..."
     4# on a collection of packages where some have init functions and some
     5# do not, some have tests and some do not.
     6
     7[short] skip
     8
     9# Verify correct statements percentages. We have a total of 10
    10# statements in the packages matched by "./..."; package "a" (for
    11# example) has two statements so we expect 20.0% stmts covered. Go
    12# 1.19 would print 50% here (due to force importing of all ./...
    13# packages); prior to the fix for #58770 Go 1.20 would show 100%
    14# coverage. For packages "x" and "f" (which have no tests), check for
    15# 0% stmts covered (as opposed to "no test files").
    16
    17go test -count=1 -coverprofile=cov.dat -coverpkg=./... ./...
    18stdout '^\s*\?\s+M/n\s+\[no test files\]'
    19stdout '^\s*M/x\s+coverage: 0.0% of statements'
    20stdout '^\s*M/f\s+coverage: 0.0% of statements'
    21stdout '^ok\s+M/a\s+\S+\s+coverage: 30.0% of statements in ./...'
    22stdout '^ok\s+M/b\s+\S+\s+coverage: 20.0% of statements in ./...'
    23stdout '^ok\s+M/main\s+\S+\s+coverage: 80.0% of statements in ./...'
    24
    25# Check for selected elements in the collected coverprofile as well.
    26
    27go tool cover -func=cov.dat
    28stdout '^M/x/x.go:3:\s+XFunc\s+0.0%'
    29stdout '^M/b/b.go:7:\s+BFunc\s+100.0%'
    30stdout '^total:\s+\(statements\)\s+80.0%'
    31
    32-- go.mod --
    33module M
    34
    35go 1.21
    36-- a/a.go --
    37package a
    38
    39import "M/f"
    40
    41func init() {
    42	println("package 'a' init: launch the missiles!")
    43}
    44
    45func AFunc() int {
    46	return f.Id()
    47}
    48-- a/a_test.go --
    49package a
    50
    51import "testing"
    52
    53func TestA(t *testing.T) {
    54	if AFunc() != 42 {
    55		t.Fatalf("bad!")
    56	}
    57}
    58-- b/b.go --
    59package b
    60
    61func init() {
    62	println("package 'b' init: release the kraken")
    63}
    64
    65func BFunc() int {
    66	return -42
    67}
    68-- b/b_test.go --
    69package b
    70
    71import "testing"
    72
    73func TestB(t *testing.T) {
    74	if BFunc() != -42 {
    75		t.Fatalf("bad!")
    76	}
    77}
    78-- f/f.go --
    79package f
    80
    81func Id() int {
    82     return 42
    83}
    84-- main/main.go --
    85package main
    86
    87import (
    88	"M/a"
    89	"M/b"
    90)
    91
    92func MFunc() string {
    93	return "42"
    94}
    95
    96func M2Func() int {
    97	return a.AFunc() + b.BFunc()
    98}
    99
   100func init() {
   101	println("package 'main' init")
   102}
   103
   104func main() {
   105	println(a.AFunc() + b.BFunc())
   106}
   107-- main/main_test.go --
   108package main
   109
   110import "testing"
   111
   112func TestMain(t *testing.T) {
   113	if MFunc() != "42" {
   114		t.Fatalf("bad!")
   115	}
   116	if M2Func() != 0 {
   117		t.Fatalf("also bad!")
   118	}
   119}
   120-- n/n.go --
   121package n
   122
   123type N int
   124-- x/x.go --
   125package x
   126
   127func XFunc() int {
   128	return 2 * 2
   129}

View as plain text