1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright 2019 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.
//go:build ignore
package main
import (
"bytes"
"flag"
"log"
"os"
"os/exec"
"strings"
"testing"
"text/template"
"cmd/go/internal/test/internal/genflags"
)
func main() {
if err := regenerate(); err != nil {
log.Fatal(err)
}
}
func regenerate() error {
vetAnalyzers, err := genflags.VetAnalyzers()
if err != nil {
return err
}
t := template.Must(template.New("fileTemplate").Parse(fileTemplate))
tData := map[string][]string{
"testFlags": testFlags(),
"vetAnalyzers": vetAnalyzers,
}
buf := bytes.NewBuffer(nil)
if err := t.Execute(buf, tData); err != nil {
return err
}
f, err := os.Create("flagdefs.go")
if err != nil {
return err
}
cmd := exec.Command("gofmt")
cmd.Stdin = buf
cmd.Stdout = f
cmd.Stderr = os.Stderr
cmdErr := cmd.Run()
if err := f.Close(); err != nil {
return err
}
if cmdErr != nil {
os.Remove(f.Name())
return cmdErr
}
return nil
}
func testFlags() []string {
testing.Init()
var names []string
flag.VisitAll(func(f *flag.Flag) {
var name string
var found bool
if name, found = strings.CutPrefix(f.Name, "test."); !found {
return
}
switch name {
case "testlogfile", "paniconexit0", "fuzzcachedir", "fuzzworker", "gocoverdir":
// These flags are only for use by cmd/go.
default:
names = append(names, name)
}
})
return names
}
const fileTemplate = `// Copyright 2019 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.
// Code generated by genflags.go — DO NOT EDIT.
package test
// passFlagToTest contains the flags that should be forwarded to
// the test binary with the prefix "test.".
var passFlagToTest = map[string]bool {
{{- range .testFlags}}
"{{.}}": true,
{{- end }}
}
var passAnalyzersToVet = map[string]bool {
{{- range .vetAnalyzers}}
"{{.}}": true,
{{- end }}
}
`
|