1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 "golang.org/x/tools/godoc/vfs/mapfs"
18 // TestIgnoredGoFiles tests the scenario where a folder has no .go or .c files,
19 // but has an ignored go file.
20 func TestIgnoredGoFiles(t *testing.T) {
21 packagePath := "github.com/package"
22 packageComment := "main is documented in an ignored .go file"
24 c := NewCorpus(mapfs.New(map[string]string{
25 "src/" + packagePath + "/ignored.go": `// +build ignore
27 // ` + packageComment + `
29 srv := &handlerServer{
35 pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, NoFiltering, "linux", "amd64")
37 if pInfo.PDoc == nil {
38 t.Error("pInfo.PDoc = nil; want non-nil.")
40 if got, want := pInfo.PDoc.Doc, packageComment+"\n"; got != want {
41 t.Errorf("pInfo.PDoc.Doc = %q; want %q.", got, want)
43 if got, want := pInfo.PDoc.Name, "main"; got != want {
44 t.Errorf("pInfo.PDoc.Name = %q; want %q.", got, want)
46 if got, want := pInfo.PDoc.ImportPath, packagePath; got != want {
47 t.Errorf("pInfo.PDoc.ImportPath = %q; want %q.", got, want)
50 if pInfo.FSet == nil {
51 t.Error("pInfo.FSet = nil; want non-nil.")
55 func TestIssue5247(t *testing.T) {
56 const packagePath = "example.com/p"
57 c := NewCorpus(mapfs.New(map[string]string{
58 "src/" + packagePath + "/p.go": `package p
61 // F doc //line 1 should appear
62 // line 2 should appear
64 //line foo.go:100`})) // No newline at end to check corner cases.
66 srv := &handlerServer{
67 p: &Presentation{Corpus: c},
70 pInfo := srv.GetPageInfo("/src/"+packagePath, packagePath, 0, "linux", "amd64")
71 if got, want := pInfo.PDoc.Funcs[0].Doc, "F doc //line 1 should appear\nline 2 should appear\n"; got != want {
72 t.Errorf("pInfo.PDoc.Funcs[0].Doc = %q; want %q", got, want)
76 func TestRedirectAndMetadata(t *testing.T) {
77 c := NewCorpus(mapfs.New(map[string]string{
78 "doc/y/index.html": "Hello, y.",
79 "doc/x/index.html": `<!--{
88 GodocHTML: template.Must(template.New("").Parse(`{{printf "%s" .Body}}`)),
90 r := &http.Request{URL: &url.URL{}}
92 // Test that redirect is sent back correctly.
93 // Used to panic. See golang.org/issue/40665.
94 for _, elem := range []string{"x", "y"} {
95 dir := "/doc/" + elem + "/"
96 r.URL.Path = dir + "index.html"
97 rw := httptest.NewRecorder()
99 loc := rw.Result().Header.Get("Location")
100 if rw.Code != 301 || loc != dir {
101 t.Errorf("GET %s: expected 301 -> %q, got %d -> %q", r.URL.Path, dir, rw.Code, loc)
105 rw = httptest.NewRecorder()
107 if rw.Code != 200 || !strings.Contains(rw.Body.String(), "Hello, "+elem) {
108 t.Fatalf("GET %s: expected 200 w/ Hello, %s: got %d w/ body:\n%s",
109 r.URL.Path, elem, rw.Code, rw.Body)