зеркало из https://github.com/golang/pkgsite.git
internal/godoc/internal/doc: use txtar format for example tests
Switch from my custom section-file format to the txtar format, which is used internally by the Go tool and is now publicly available in golang/x/tools. Less code for us to maintain. Change-Id: I8330072de2e70ed535d9104dff9948810af97bcc Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/289951 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Julie Qiu <julie@golang.org>
This commit is contained in:
Родитель
b3177424ae
Коммит
9057dcb572
7
go.mod
7
go.mod
|
@ -39,10 +39,11 @@ require (
|
|||
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da // indirect
|
||||
go.opencensus.io v0.22.4
|
||||
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
|
||||
golang.org/x/net v0.0.0-20200904194848-62affa334b73
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
|
||||
golang.org/x/sys v0.0.0-20200922070232-aee5d888a860 // indirect
|
||||
golang.org/x/text v0.3.4 // indirect
|
||||
golang.org/x/tools v0.1.0 // indirect
|
||||
golang.org/x/tools v0.0.0-20200915173823-2db8f0ff891c
|
||||
google.golang.org/api v0.32.0
|
||||
google.golang.org/genproto v0.0.0-20200923140941-5646d36feee1
|
||||
google.golang.org/grpc v1.32.0
|
||||
|
|
14
go.sum
14
go.sum
|
@ -565,9 +565,8 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/
|
|||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA=
|
||||
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
|
@ -584,9 +583,8 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ
|
|||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
|
@ -633,9 +631,8 @@ golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200828194041-157a740278f4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200922070232-aee5d888a860 h1:YEu4SMq7D0cmT7CBbXfcH0NZeuChAXwsHe/9XueUO6o=
|
||||
golang.org/x/sys v0.0.0-20200922070232-aee5d888a860/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
@ -698,9 +695,8 @@ golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc
|
|||
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||
golang.org/x/tools v0.0.0-20200828161849-5deb26317202/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||
golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE=
|
||||
golang.org/x/tools v0.0.0-20200915173823-2db8f0ff891c h1:AQsh/7arPVFDBraQa8x7GoVnwnGg1kM7J2ySI0kF5WU=
|
||||
golang.org/x/tools v0.0.0-20200915173823-2db8f0ff891c/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
|
||||
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
|
||||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
|
||||
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
|
|
@ -5,15 +5,12 @@
|
|||
package doc_test
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
@ -21,6 +18,7 @@ import (
|
|||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"golang.org/x/pkgsite/internal/godoc/internal/doc"
|
||||
"golang.org/x/tools/txtar"
|
||||
)
|
||||
|
||||
func TestExamples(t *testing.T) {
|
||||
|
@ -311,81 +309,16 @@ func mustParse(fset *token.FileSet, filename, src string) *ast.File {
|
|||
// readSectionFile reads a file that is divided into sections, and returns
|
||||
// a map from section name to contents.
|
||||
//
|
||||
// A section begins with a line starting with at least four hyphens. Any number
|
||||
// of additional hyphens may follow. After the last hyphen is the section name,
|
||||
// which may be followed by more hyphens. The contents of the section are the
|
||||
// lines that follow, until the next section start or EOF. For example, here is
|
||||
// a section with name Foo and contents "hello":
|
||||
//
|
||||
// ---- Foo
|
||||
// hello
|
||||
//
|
||||
// Lines before the first section are ignored.
|
||||
//
|
||||
// There is no way to represent a section that contains a line beginning with
|
||||
// four or more hyphens.
|
||||
// We use the txtar format for the file. See https://pkg.go.dev/golang.org/x/tools/txtar.
|
||||
// Although the format talks about filenames as the keys, they can be arbitrary strings.
|
||||
func readSectionFile(filename string) (map[string]string, error) {
|
||||
f, err := os.Open(filename)
|
||||
archive, err := txtar.ParseFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return readSections(filename, f)
|
||||
}
|
||||
|
||||
func readSections(filename string, r io.Reader) (map[string]string, error) {
|
||||
scan := bufio.NewScanner(r)
|
||||
sections := map[string]string{}
|
||||
var name string
|
||||
var lines []string
|
||||
for scan.Scan() {
|
||||
line := scan.Text()
|
||||
if strings.HasPrefix(line, "----") {
|
||||
if name != "" {
|
||||
sections[name] = strings.Join(lines, "\n")
|
||||
lines = nil
|
||||
}
|
||||
name = strings.Trim(line, "- \t")
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("%s: empty name on line: %q", filename, line)
|
||||
}
|
||||
if _, ok := sections[name]; ok {
|
||||
return nil, fmt.Errorf("%s: duplicate section name %q", filename, name)
|
||||
}
|
||||
} else if name != "" {
|
||||
lines = append(lines, line)
|
||||
}
|
||||
}
|
||||
if err := scan.Err(); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", filename, err)
|
||||
}
|
||||
if name != "" {
|
||||
sections[name] = strings.Join(lines, "\n")
|
||||
}
|
||||
return sections, nil
|
||||
}
|
||||
|
||||
const sectionFileContents = `
|
||||
---- S1 ----
|
||||
hello, world
|
||||
---------------- empty ----------------
|
||||
---- S2
|
||||
two
|
||||
lines
|
||||
`
|
||||
|
||||
func TestReadSections(t *testing.T) {
|
||||
r := strings.NewReader(sectionFileContents)
|
||||
got, err := readSections("test", r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]string{
|
||||
"S1": "hello, world",
|
||||
"S2": "two\nlines",
|
||||
"empty": "",
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
m := map[string]string{}
|
||||
for _, f := range archive.Files {
|
||||
m[f.Name] = strings.TrimSpace(string(f.Data))
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
---------------- .Play
|
||||
-- .Play --
|
||||
package main
|
||||
|
||||
func main() {}
|
||||
func main()
|
||||
---------------- .Output
|
||||
-- .Output --
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
---------------- .Play
|
||||
-- .Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -14,8 +14,8 @@ func main() {
|
|||
getReader()
|
||||
do()
|
||||
}
|
||||
---------------- .Output
|
||||
---------------- Ignored.Play
|
||||
-- .Output --
|
||||
-- Ignored.Play --
|
||||
package main
|
||||
|
||||
import ()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
---------------- .Play
|
||||
-- .Play --
|
||||
package main
|
||||
|
||||
import ()
|
||||
|
@ -14,7 +14,7 @@ func main() {
|
|||
_ = b
|
||||
_ = d
|
||||
}
|
||||
---------------- 2.Play
|
||||
-- 2.Play --
|
||||
package main
|
||||
|
||||
import ()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
---------------- Profile_simple.Play
|
||||
-- Profile_simple.Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -79,7 +79,9 @@ var smallDumbell = []intset{
|
|||
4: linksTo(5),
|
||||
5: nil,
|
||||
}
|
||||
---------------- Profile_multiplex.Play
|
||||
|
||||
-- Profile_multiplex.Play --
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
---------------- Hello.Play
|
||||
-- Hello.Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -8,9 +8,9 @@ import (
|
|||
func main() {
|
||||
fmt.Println("Hello, world!")
|
||||
}
|
||||
---------------- Hello.Output
|
||||
-- Hello.Output --
|
||||
Hello, world!
|
||||
---------------- Import.Play
|
||||
-- Import.Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -26,7 +26,7 @@ func main() {
|
|||
}
|
||||
fmt.Printf("The date is %s\n", out)
|
||||
}
|
||||
---------------- KeyValue.Play
|
||||
-- KeyValue.Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -43,7 +43,7 @@ func main() {
|
|||
}
|
||||
fmt.Print(v)
|
||||
}
|
||||
---------------- KeyValueImport.Play
|
||||
-- KeyValueImport.Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -57,11 +57,11 @@ func main() {
|
|||
}
|
||||
fmt.Print(f)
|
||||
}
|
||||
---------------- KeyValue.Output
|
||||
-- KeyValue.Output --
|
||||
a: "A", b: 1
|
||||
---------------- KeyValueImport.Output
|
||||
-- KeyValueImport.Output --
|
||||
Name: "play"
|
||||
---------------- KeyValueTopDecl.Play
|
||||
-- KeyValueTopDecl.Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -79,9 +79,9 @@ var keyValueTopDecl = struct {
|
|||
func main() {
|
||||
fmt.Print(keyValueTopDecl)
|
||||
}
|
||||
---------------- KeyValueTopDecl.Output
|
||||
-- KeyValueTopDecl.Output --
|
||||
a: "B", b: 2
|
||||
---------------- Sort.Play
|
||||
-- Sort.Play --
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -124,6 +124,6 @@ func main() {
|
|||
sort.Sort(ByAge(people))
|
||||
fmt.Println(people)
|
||||
}
|
||||
---------------- Sort.Output
|
||||
-- Sort.Output --
|
||||
[Bob: 31 John: 42 Michael: 17 Jenny: 26]
|
||||
[Michael: 17 Jenny: 26 Bob: 31 John: 42]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
---------------- A.Play
|
||||
-- A.Play --
|
||||
package main
|
||||
|
||||
import ()
|
||||
|
@ -12,7 +12,7 @@ var a, b = f()
|
|||
func main() {
|
||||
_ = a
|
||||
}
|
||||
---------------- B.Play
|
||||
-- B.Play --
|
||||
package main
|
||||
|
||||
import ()
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
---------------- .Play
|
||||
-- .Play --
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -17,5 +17,5 @@ func (X) BenchmarkFoo() {
|
|||
func main() {
|
||||
fmt.Println("Hello, world!")
|
||||
}
|
||||
---------------- .Output
|
||||
-- .Output --
|
||||
Hello, world!
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
---------------- .Play
|
||||
-- .Play --
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
@ -9,5 +9,5 @@ func Foo(x int) {
|
|||
func main() {
|
||||
fmt.Println("Hello, world!")
|
||||
}
|
||||
---------------- .Output
|
||||
-- .Output --
|
||||
Hello, world!
|
||||
|
|
Загрузка…
Ссылка в новой задаче