// Copyright 2025 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 web_test
import (
"fmt"
"html"
"regexp"
"strings"
"testing"
"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/gopls/internal/settings"
. "golang.org/x/tools/gopls/internal/test/integration"
)
// TODO(adonovan): define marker test verbs for checking package docs.
// TestBrowsePkgDoc provides basic coverage of the "Browse package
// documentation", which creates a web server on demand.
func TestBrowsePkgDoc(t *testing.T) {
const files = `
-- go.mod --
module example.com
-- a/a.go --
package a
const A = 1
type G[T any] int
func (G[T]) F(int, int, int, int, int, int, int, ...int) {}
// EOF
`
Run(t, files, func(t *testing.T, env *Env) {
// Assert that the HTML page contains the expected const declaration.
// (We may need to make allowances for HTML markup.)
env.OpenFile("a/a.go")
uri1 := viewPkgDoc(t, env, env.Sandbox.Workdir.EntireFile("a/a.go"))
doc1 := get(t, uri1)
checkMatch(t, true, doc1, "const A =.*1")
// Regression test for signature truncation (#67287, #67294).
checkMatch(t, true, doc1, regexp.QuoteMeta("func (G[T]) F(int, int, int, ...)"))
// Check that edits to the buffer (even unsaved) are
// reflected in the HTML document.
env.RegexpReplace("a/a.go", "// EOF", "func NewFunc() {}")
env.Await(env.DoneDiagnosingChanges())
doc2 := get(t, uri1)
checkMatch(t, true, doc2, "func NewFunc")
// TODO(adonovan): assert some basic properties of the
// HTML document using something like
// golang.org/x/pkgsite/internal/testing/htmlcheck.
// Grab the URL in the HTML source link for NewFunc.
// (We don't have a DOM or JS interpreter so we have
// to know something of the document internals here.)
rx := regexp.MustCompile(`
`))
})
}
// TestPkgDocContext tests that the gopls.doc command title and /pkg
// URL are appropriate for the current selection. It is effectively a
// test of golang.DocFragment.
func TestPkgDocContext(t *testing.T) {
const files = `
-- go.mod --
module example.com
-- a/a.go --
package a
import "fmt"
import "bytes"
func A() {
fmt.Println()
new(bytes.Buffer).Write(nil)
}
const K = 123
type T int
func (*T) M() { /*in T.M*/}
`
viewRE := regexp.MustCompile("view=[0-9]*")
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
for _, test := range []struct {
re string // regexp indicating selected portion of input file
want string // suffix of expected URL after /pkg/
}{
// current package
{"package a", "example.com/a?view=1"}, // outside any decl
{"in T.M", "example.com/a?view=1#T.M"}, // inside method (*T).M
{"123", "example.com/a?view=1#K"}, // inside const/var decl
{"T int", "example.com/a?view=1#T"}, // inside type decl
// imported
{"\"fmt\"", "fmt?view=1"}, // in import spec
{"fmt()[.]", "fmt?view=1"}, // use of PkgName
{"Println", "fmt?view=1#Println"}, // use of imported pkg-level symbol
{"fmt.Println", "fmt?view=1#Println"}, // qualified identifier
{"Write", "bytes?view=1#Buffer.Write"}, // use of imported method
// TODO(adonovan):
// - xtest package -> ForTest
// - field of imported struct -> nope
// - exported method of nonexported type from another package
// (e.g. types.Named.Obj) -> nope
// Also: assert that Command.Title looks nice.
} {
uri := viewPkgDoc(t, env, env.RegexpSearch("a/a.go", test.re))
_, got, ok := strings.Cut(uri, "/pkg/")
if !ok {
t.Errorf("pattern %q => %s (invalid /pkg URL)", test.re, uri)
continue
}
// Normalize the view ID, which varies by integration test mode.
got = viewRE.ReplaceAllString(got, "view=1")
if got != test.want {
t.Errorf("pattern %q => %s; want %s", test.re, got, test.want)
}
}
})
}
// TestPkgDocFileImports tests that the doc links are rendered
// as URLs based on the correct import mapping for the file in
// which they appear.
func TestPkgDocFileImports(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.20
-- a/a1.go --
// Package a refers to [b.T] [b.U] [alias.D] [d.D] [c.T] [c.U] [nope.Nope]
package a
import "mod.com/b"
import alias "mod.com/d"
// [b.T] indeed refers to b.T.
//
// [alias.D] refers to d.D
// but [d.D] also refers to d.D.
type A1 int
-- a/a2.go --
package a
import b "mod.com/c"
// [b.U] actually refers to c.U.
type A2 int
-- b/b.go --
package b
type T int
type U int
-- c/c.go --
package c
type T int
type U int
-- d/d.go --
package d
type D int
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a/a1.go")
uri1 := viewPkgDoc(t, env, env.Sandbox.Workdir.EntireFile("a/a1.go"))
doc := get(t, uri1)
// Check that the doc links are resolved using the
// appropriate import mapping for the file in which
// they appear.
checkMatch(t, true, doc, `pkg/mod.com/b\?.*#T">b.T indeed refers to b.T`)
checkMatch(t, true, doc, `pkg/mod.com/c\?.*#U">b.U actually refers to c.U`)
// Check that doc links can be resolved using either
// the original or the local name when they refer to a
// renaming import. (Local names are preferred.)
checkMatch(t, true, doc, `pkg/mod.com/d\?.*#D">alias.D refers to d.D`)
checkMatch(t, true, doc, `pkg/mod.com/d\?.*#D">d.D also refers to d.D`)
// Check that links in the package doc comment are
// resolved, and relative to the correct file (a1.go).
checkMatch(t, true, doc, `Package a refers to.*pkg/mod.com/b\?.*#T">b.T`)
checkMatch(t, true, doc, `Package a refers to.*pkg/mod.com/b\?.*#U">b.U`)
checkMatch(t, true, doc, `Package a refers to.*pkg/mod.com/d\?.*#D">alias.D`)
checkMatch(t, true, doc, `Package a refers to.*pkg/mod.com/d\?.*#D">d.D`)
checkMatch(t, true, doc, `Package a refers to.*pkg/mod.com/c\?.*#T">c.T`)
checkMatch(t, true, doc, `Package a refers to.*pkg/mod.com/c\?.*#U">c.U`)
checkMatch(t, true, doc, `Package a refers to.* \[nope.Nope\]`)
})
}
// TestPkgDocConstructorOfUnexported tests that exported constructor
// functions (NewT) whose result type (t) is unexported are not
// discarded but are presented as ordinary top-level functions (#69553).
func TestPkgDocConstructorOfUnexported(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.20
-- a/a.go --
package a
func A() {}
func Z() {}
type unexported int
func NewUnexported() unexported // exported constructor of unexported type
type Exported int
func NewExported() Exported // exported constructor of exported type
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
uri1 := viewPkgDoc(t, env, env.Sandbox.Workdir.EntireFile("a/a.go"))
doc := get(t, uri1)
want := regexp.QuoteMeta(`
`)
checkMatch(t, true, doc, want)
})
}
// viewPkgDoc invokes the "Browse package documentation" code action
// at the specified location. It returns the URI of the document, or
// fails the test.
func viewPkgDoc(t *testing.T, env *Env, loc protocol.Location) protocol.URI {
// Invoke the "Browse package documentation" code
// action to start the server.
actions := env.CodeAction(loc, nil, 0)
docAction, err := CodeActionByKind(actions, settings.GoDoc)
if err != nil {
t.Fatal(err)
}
// Execute the command.
// Its side effect should be a single showDocument request.
params := &protocol.ExecuteCommandParams{
Command: docAction.Command.Command,
Arguments: docAction.Command.Arguments,
}
var result any
collectDocs := env.Awaiter.ListenToShownDocuments()
env.ExecuteCommand(params, &result)
doc := shownDocument(t, collectDocs(), "http:")
if doc == nil {
t.Fatalf("no showDocument call had 'http:' prefix")
}
if false {
t.Log("showDocument(package doc) URL:", doc.URI)
}
return doc.URI
}