Skip to content

Commit d169a42

Browse files
committed
cmd/go: test whether alldocs.go is up to date
A common error is to update the help text for a command in cmd/go, but fail to update alldocs.go, which actually prints the help text for the most common commands. Add a test that the long-form documentation help text matches the contents of alldocs.go, which will fail the build if we fail to keep the documentation in sync. We can get fancier with the test output if this is not sufficient. Fixes #26735. Change-Id: I2509765315eeb0f362633d812343d1324a01b73b Reviewed-on: https://go-review.googlesource.com/127920 Run-TryBot: Kevin Burke <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 477b7e5 commit d169a42

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

src/cmd/go/alldocs.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/help_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.
4+
5+
// +build !nacl
6+
7+
package main_test
8+
9+
import (
10+
"bytes"
11+
"io/ioutil"
12+
"testing"
13+
14+
"cmd/go/internal/help"
15+
)
16+
17+
func TestDocsUpToDate(t *testing.T) {
18+
buf := new(bytes.Buffer)
19+
// Match the command in mkalldocs.sh that generates alldocs.go.
20+
help.Help(buf, []string{"documentation"})
21+
data, err := ioutil.ReadFile("alldocs.go")
22+
if err != nil {
23+
t.Fatalf("error reading alldocs.go: %v", err)
24+
}
25+
if !bytes.Equal(data, buf.Bytes()) {
26+
t.Errorf("alldocs.go is not up to date; run mkalldocs.sh to regenerate it")
27+
}
28+
}

src/cmd/go/internal/help/help.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import (
2020
)
2121

2222
// Help implements the 'help' command.
23-
func Help(args []string) {
23+
func Help(w io.Writer, args []string) {
2424
// 'go help documentation' generates doc.go.
2525
if len(args) == 1 && args[0] == "documentation" {
26-
fmt.Println("// Copyright 2011 The Go Authors. All rights reserved.")
27-
fmt.Println("// Use of this source code is governed by a BSD-style")
28-
fmt.Println("// license that can be found in the LICENSE file.")
29-
fmt.Println()
30-
fmt.Println("// Code generated by mkalldocs.sh; DO NOT EDIT.")
31-
fmt.Println("// Edit the documentation in other files and rerun mkalldocs.sh to generate this one.")
32-
fmt.Println()
26+
fmt.Fprintln(w, "// Copyright 2011 The Go Authors. All rights reserved.")
27+
fmt.Fprintln(w, "// Use of this source code is governed by a BSD-style")
28+
fmt.Fprintln(w, "// license that can be found in the LICENSE file.")
29+
fmt.Fprintln(w)
30+
fmt.Fprintln(w, "// Code generated by mkalldocs.sh; DO NOT EDIT.")
31+
fmt.Fprintln(w, "// Edit the documentation in other files and rerun mkalldocs.sh to generate this one.")
32+
fmt.Fprintln(w)
3333
buf := new(bytes.Buffer)
3434
PrintUsage(buf, base.Go)
3535
usage := &base.Command{Long: buf.String()}
@@ -42,8 +42,8 @@ func Help(args []string) {
4242
cmds = append(cmds, cmd)
4343
cmds = append(cmds, cmd.Commands...)
4444
}
45-
tmpl(&commentWriter{W: os.Stdout}, documentationTemplate, cmds)
46-
fmt.Println("package main")
45+
tmpl(&commentWriter{W: w}, documentationTemplate, cmds)
46+
fmt.Fprintln(w, "package main")
4747
return
4848
}
4949

src/cmd/go/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func main() {
9595

9696
cfg.CmdName = args[0] // for error messages
9797
if args[0] == "help" {
98-
help.Help(args[1:])
98+
help.Help(os.Stdout, args[1:])
9999
return
100100
}
101101

@@ -199,7 +199,7 @@ BigCmdLoop:
199199
}
200200
if args[0] == "help" {
201201
// Accept 'go mod help' and 'go mod help foo' for 'go help mod' and 'go help mod foo'.
202-
help.Help(append(strings.Split(cfg.CmdName, " "), args[1:]...))
202+
help.Help(os.Stdout, append(strings.Split(cfg.CmdName, " "), args[1:]...))
203203
return
204204
}
205205
cfg.CmdName += " " + args[0]

src/cmd/go/mkalldocs.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
set -e
77

88
go build -o go.latest
9+
# If the command used to generate alldocs.go changes, update TestDocsUpToDate in
10+
# help_test.go.
911
./go.latest help documentation >alldocs.go
1012
gofmt -w alldocs.go
1113
rm go.latest

0 commit comments

Comments
 (0)