cmd/go: fix a couple of bugs in coverage tooling
Merging a couple of CLs into one, since they collided in my client
and I'm lazy.

1) Fix up output in "go test -cover" case.
We need to tell the testing package the name of the package being tested
and the name of the package being covered. It can then sort out the report.

2) Filter out the _test.go files from coverage processing. We want to measure
what the tests cover, not what's covered in the tests,
The coverage for encoding/gob goes from 82.2% to 88.4%.
There may be a cleaner way to do this - suggestions welcome - but ça suffit.

Fixes #5810.

R=rsc
CC=golang-dev
https://golang.org/cl/10868047
diff --git a/src/pkg/testing/cover.go b/src/pkg/testing/cover.go
index 4136bec..4f66a0b 100644
--- a/src/pkg/testing/cover.go
+++ b/src/pkg/testing/cover.go
@@ -27,6 +27,11 @@
 	coverBlocks   map[string][]CoverBlock
 )
 
+var (
+	testedPackage  string // The package being tested.
+	coveredPackage string // List of the package[s] being covered, if distinct from the tested package.
+)
+
 // RegisterCover records the coverage data accumulators for the tests.
 // NOTE: This struct is internal to the testing infrastructure and may change.
 // It is not covered (yet) by the Go 1 compatibility guidelines.
@@ -35,6 +40,14 @@
 	coverBlocks = b
 }
 
+// CoveredPackage records the names of the packages being tested and covered.
+// NOTE: This function is internal to the testing infrastructure and may change.
+// It is not covered (yet) by the Go 1 compatibility guidelines.
+func CoveredPackage(tested, covered string) {
+	testedPackage = tested
+	coveredPackage = covered
+}
+
 // mustBeNil checks the error and, if present, reports it and exits.
 func mustBeNil(err error) {
 	if err != nil {
@@ -55,16 +68,7 @@
 	}
 
 	var active, total int64
-	packageName := ""
 	for name, counts := range coverCounters {
-		if packageName == "" {
-			// Package name ends at last slash.
-			for i, c := range name {
-				if c == '/' {
-					packageName = name[:i]
-				}
-			}
-		}
 		blocks := coverBlocks[name]
 		for i, count := range counts {
 			stmts := int64(blocks[i].Stmts)
@@ -85,8 +89,5 @@
 	if total == 0 {
 		total = 1
 	}
-	if packageName == "" {
-		packageName = "package"
-	}
-	fmt.Printf("test coverage for %s: %.1f%% of statements\n", packageName, 100*float64(active)/float64(total))
+	fmt.Printf("coverage for %s: %.1f%% of statements%s\n", testedPackage, 100*float64(active)/float64(total), coveredPackage)
 }