cmd/doc: fix pretty printing of paths

The code to strip GOROOT and GOPATH had a bug: it assumed there
were bytes after the GOROOT prefix but there might not be.
Fix this and other issues by taking care the prefix is really a
file name prefix for the path, not just a string prefix, and
handle the case where GOROOT==path.

Change-Id: I8066865fd05f938bb6dbf3bb8ab1fc58e5cf6bb5
Reviewed-on: https://go-review.googlesource.com/15112
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
Rob Pike 2015-09-28 13:45:57 -07:00
parent 3b9e8bb7f2
commit 0722a5e718
3 changed files with 54 additions and 7 deletions

View file

@ -401,3 +401,33 @@ func TestMultiplePackages(t *testing.T) {
}
}
}
type trimTest struct {
path string
prefix string
result string
ok bool
}
var trimTests = []trimTest{
{"", "", "", true},
{"/usr/gopher", "/usr/gopher", "/usr/gopher", true},
{"/usr/gopher/bar", "/usr/gopher", "bar", true},
{"/usr/gopher", "/usr/gopher", "/usr/gopher", true},
{"/usr/gopherflakes", "/usr/gopher", "/usr/gopherflakes", false},
{"/usr/gopher/bar", "/usr/zot", "/usr/gopher/bar", false},
}
func TestTrim(t *testing.T) {
for _, test := range trimTests {
result, ok := trim(test.path, test.prefix)
if ok != test.ok {
t.Errorf("%s %s expected %t got %t", test.path, test.prefix, test.ok, ok)
continue
}
if result != test.result {
t.Errorf("%s %s expected %q got %q", test.path, test.prefix, test.result, result)
continue
}
}
}