Debian Codegraph @

debian/go/src/golang.org/x/telemetry/cmd/gotelemetry/help_test.go

raw symbols SCIP
1 // Copyright 2023 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 package main
6
7 import (
8 "flag"
9 "fmt"
10 "go/format"
11 "go/parser"
12 "go/token"
13 "os"
14 "os/exec"
15 "strings"
16 "testing"
17
18 "golang.org/x/telemetry/internal/testenv"
19 )
20
21 var updateDocs = flag.Bool("update", false, "if set, update docs")
22
23 func TestMain(m *testing.M) {
24 if os.Getenv("GOTELEMETRY_RUN_AS_MAIN") != "" {
25 main()
26 os.Exit(0)
27 }
28 os.Exit(m.Run())
29 }
30
31 func TestDocHelp(t *testing.T) {
32 testenv.MustHaveExec(t)
33
34 exe, err := os.Executable()
35 if err != nil {
36 t.Fatal(err)
37 }
38 cmd := exec.Command(exe, "help")
39 cmd.Env = append(os.Environ(), "GOTELEMETRY_RUN_AS_MAIN=1")
40 help, err := cmd.Output()
41 if err != nil {
42 t.Fatal(err)
43 }
44
45 if *updateDocs {
46 var lines []string
47 for _, line := range strings.Split(strings.TrimSpace(string(help)), "\n") {
48 if len(line) > 0 {
49 lines = append(lines, "// "+line)
50 } else {
51 lines = append(lines, "//")
52 }
53 }
54 contents := fmt.Sprintf(`// Copyright 2023 The Go Authors. All rights reserved.
55 // Use of this source code is governed by a BSD-style
56 // license that can be found in the LICENSE file.
57
58 // Code generated by TestDocHelp; DO NOT EDIT.
59
60 %s
61 package main
62 `, strings.Join(lines, "\n"))
63
64 data, err := format.Source([]byte(contents))
65 if err != nil {
66 t.Fatalf("formatting content: %v", err)
67 }
68
69 if err := os.WriteFile("doc.go", data, 0666); err != nil {
70 t.Fatalf("writing doc.go: %v", err)
71 }
72 }
73
74 f, err := parser.ParseFile(token.NewFileSet(), "doc.go", nil, parser.PackageClauseOnly|parser.ParseComments)
75 if err != nil {
76 t.Fatalf("parsing doc.go: %v", err)
77 }
78 doc := f.Doc.Text()
79 if got, want := doc, string(help); got != want {
80 t.Errorf("doc.go: mismatching content\ngot:\n%s\nwant:\n%s", got, want)
81 }
82 }