New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: test / tool cover: coverprofile / coverage report incorrect red coverage #27015

Open
MichaelS11 opened this Issue Aug 16, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@MichaelS11

MichaelS11 commented Aug 16, 2018

What version of Go are you using (go version)?

go version go1.10.3 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

set GOHOSTARCH=amd64
set GOHOSTOS=windows

What did you do?

main.go:

package main

import (
	"fmt"
	"time"
)

var vars struct {
	chanStruct    chan struct{}
	chanInt       chan int
	chanOneClosed chan struct{}
	spamNumber    int
}

func main() {
	vars.chanStruct = make(chan struct{})
	vars.chanInt = make(chan int, 1024)
	vars.chanOneClosed = make(chan struct{})
	vars.spamNumber = 20

	go feed()
	go aLoop()

	time.Sleep(time.Second)

	close(vars.chanStruct)

	select {
	case <-vars.chanOneClosed:
	}

	time.Sleep(time.Second)
}

// a comment
func feed() {
	// a comment
	for i := 0; i < 10; i++ {
		// a comment
		vars.chanInt <- i
		// a comment
	}
	// a comment
}

// a comment
func aLoop() {
Loop:
	// a comment
	for {
		// a comment
		select {
		// a comment
		case <-vars.chanStruct:
			// a comment
			close(vars.chanOneClosed)
			// a comment
			break Loop
		// a comment
		case myInt := <-vars.chanInt:
			// a comment
			if myInt < vars.spamNumber {
				// a comment
				vars.chanInt <- myInt + 1
				// a comment
			}
			// a comment
		}
		// a comment
	}
	// a comment
	fmt.Println("closed")
	// a comment
}

main_test.go:

package main

import (
	"testing"
	"time"
)

func TestALoop(t *testing.T) {
	vars.chanStruct = make(chan struct{})
	vars.chanInt = make(chan int, 1024)
	vars.chanOneClosed = make(chan struct{})
	vars.spamNumber = 20

	go feed()
	go aLoop()

	time.Sleep(time.Second)

	close(vars.chanStruct)

	select {
	case <-vars.chanOneClosed:
	}

	time.Sleep(time.Second)
}

Then run:

go test -coverprofile=coverage.out -v test
go tool cover -html=coverage.out

What did you expect to see?

Coverage report with green and black, with only red in main, similar to this:

image

Note, remove the follow code from main to get coverage report to be "correct"

	close(vars.chanStruct)

	select {
	case <-vars.chanOneClosed:
	}

	time.Sleep(time.Second)

What did you see instead?

Coverage report with red marks in comments, function, and case:

image

Note:

This is similar to #22545 but that one is only comments.

@andybons andybons changed the title from test / tool cover: coverprofile / coverage report incorrect red coverage to cmd/go: test / tool cover: coverprofile / coverage report incorrect red coverage Aug 16, 2018

@andybons andybons added this to the Unplanned milestone Aug 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment