]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cover: include a package name in the HTML title
authorRob Pike <r@golang.org>
Tue, 28 Apr 2020 15:45:59 +0000 (01:45 +1000)
committerRob Pike <r@golang.org>
Thu, 30 Apr 2020 03:58:01 +0000 (03:58 +0000)
A recent change added a title to the HTML coverage report but
neglected to include the package name. Add the package name here.
It's a little trickier than you'd think because there may be multiple
packages and we don't want to parse the files, so we just extract
a directory name from the path of the first file.  This will almost
always be right, and has the advantage that it gives a better result
for package main. There are rare cases it will get wrong, but that
will be no hardship.

If this turns out not to be good enough, we can refine it.

Fixes #38609

Change-Id: I2201f6caef906e0b0258b90d7de518879041fe72
Reviewed-on: https://go-review.googlesource.com/c/go/+/230517
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/cover/html.go
src/cmd/cover/pkgname_test.go [new file with mode: 0644]

index 82ef88b79c8a0aeb2dd9e4d421b0364984e1f384..f76ea03cf53e822fc0fa57b893cc760607b74a52 100644 (file)
@@ -172,6 +172,27 @@ type templateData struct {
        Set   bool
 }
 
+// PackageName returns a name for the package being shown.
+// It does this by choosing the penultimate element of the path
+// name, so foo.bar/baz/foo.go chooses 'baz'. This is cheap
+// and easy, avoids parsing the Go file, and gets a better answer
+// for package main. It returns the empty string if there is
+// a problem.
+func (td templateData) PackageName() string {
+       if len(td.Files) == 0 {
+               return ""
+       }
+       fileName := td.Files[0].Name
+       elems := strings.Split(fileName, "/") // Package path is always slash-separated.
+       // Return the penultimate non-empty element.
+       for i := len(elems) - 2; i >= 0; i-- {
+               if elems[i] != "" {
+                       return elems[i]
+               }
+       }
+       return ""
+}
+
 type templateFile struct {
        Name     string
        Body     template.HTML
@@ -183,7 +204,7 @@ const tmplHTML = `
 <html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-               <title>Go Coverage Report</title>
+               <title>{{$pkg := .PackageName}}{{if $pkg}}{{$pkg}}: {{end}}Go Coverage Report</title>
                <style>
                        body {
                                background: black;
diff --git a/src/cmd/cover/pkgname_test.go b/src/cmd/cover/pkgname_test.go
new file mode 100644 (file)
index 0000000..1c731ad
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import "testing"
+
+func TestPackageName(t *testing.T) {
+       var tests = []struct {
+               fileName, pkgName string
+       }{
+               {"", ""},
+               {"///", ""},
+               {"fmt", ""}, // No Go file, improper form.
+               {"fmt/foo.go", "fmt"},
+               {"encoding/binary/foo.go", "binary"},
+               {"encoding/binary/////foo.go", "binary"},
+       }
+       var tf templateFile
+       for _, test := range tests {
+               tf.Name = test.fileName
+               td := templateData{
+                       Files: []*templateFile{&tf},
+               }
+               got := td.PackageName()
+               if got != test.pkgName {
+                       t.Errorf("%s: got %s want %s", test.fileName, got, test.pkgName)
+               }
+       }
+}