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
<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;
--- /dev/null
+// 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)
+ }
+ }
+}