Debian Codegraph @

cmd/stringer/endtoend_test.go

raw symbols SCIP
1 // Copyright 2014 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 // go command is not available on android
6
7 //go:build !android
8
9 package mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/
10
11 import (
12 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
13 "flagscip-go gomod github.com/golang/go/src go1.25.0 flag/"
14 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
15 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
16 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
17 "pathscip-go gomod github.com/golang/go/src go1.25.0 path/"
18 "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/"
19 "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/"
20 "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/"
21 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
22 "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/"
23 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
24
25 "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/"
26 )
27
28 // This file contains a test that compiles and runs each program in testdata
29 // after generating the string method for its type. The rule is that for testdata/x.go
30 // we run stringer -type X and then compile and run the program. The resulting
31 // binary panics if the String method for X is not correct, including for error cases.
32
33 func TestMainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/TestMain().(m *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Mscip-go gomod github.com/golang/go/src go1.25.0 testing/M#) {
34 if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("STRINGER_TEST_IS_STRINGER") != "" {
35 mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/main().()
36 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(0)
37 }
38
39 // Inform subprocesses that they should run the cmd/stringer main instead of
40 // running tests. It's a close approximation to building and running the real
41 // command, and much less complicated and expensive to build and clean up.
42 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().("STRINGER_TEST_IS_STRINGER", "1")
43
44 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Parsescip-go gomod github.com/golang/go/src go1.25.0 flag/Parse().()
45 if testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Verbosescip-go gomod github.com/golang/go/src go1.25.0 testing/Verbose().() {
46 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().("GOPACKAGESDEBUG", "true")
47 }
48
49 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(m.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/M#Run().())
50 }
51
52 func TestEndToEndscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/TestEndToEnd().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
53 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go")
54
55 stringer := stringerPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerPath().(t)
56 // Read the testdata directory.
57 fd, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().("testdata")
58 if err != nil {
59 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
60 }
61 defer fd.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().()
62 names, err := fd.Readdirnamesscip-go gomod github.com/golang/go/src go1.25.0 os/File#Readdirnames().(-1)
63 if err != nil {
64 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Readdirnames: %s", err)
65 }
66 // Generate, compile, and run the test programs.
67 for _, name := range names {
68 if name == "typeparams" {
69 // ignore the directory containing the tests with type params
70 continue
71 }
72 if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(name, ".go") {
73 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s is not a Go file", name)
74 continue
75 }
76 if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(name, "tag_") || stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(name, "vary_") {
77 // This file is used for tag processing in TestTags or TestConstValueChange, below.
78 continue
79 }
80 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
81 if name == "cgo.go" {
82 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "cgo")
83 }
84 stringerCompileAndRunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerCompileAndRun().(t, t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().(), stringer, typeNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/typeName().(name), name)
85 })
86 }
87 }
88
89 // a type name for stringer. use the last component of the file name with the .go
90 func typeNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/typeName().(fname string) string {
91 // file names are known to be ascii and end .go
92 base := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(fname)
93 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%c%s", base[0]+'A'-'a', base[1:len(base)-len(".go")])
94 }
95
96 // TestTags verifies that the -tags flag works as advertised.
97 func TestTagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/TestTags().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
98 stringer := stringerPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerPath().(t)
99 dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().()
100 var (
101 protectedConst = []byte("TagProtected")
102 output = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "const_string.go")
103 )
104 for _, file := range []string{"tag_main.go", "tag_tag.go"} {
105 err := copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/copy().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, file), filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().("testdata", file))
106 if err != nil {
107 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
108 }
109 }
110 // Run stringer in the directory that contains the module that contains the package files.
111 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "go.mod"), []byte("module test\n"), 0o600); err != nil {
112 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
113 }
114 err := runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t, dir, stringer, "-type", "Const", ".")
115 if err != nil {
116 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
117 }
118 result, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(output)
119 if err != nil {
120 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
121 }
122 if bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Containsscip-go gomod github.com/golang/go/src go1.25.0 bytes/Contains().(result, protectedConst) {
123 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("tagged variable appears in untagged run")
124 }
125 err = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(output)
126 if err != nil {
127 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
128 }
129 err = runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t, dir, stringer, "-type", "Const", "-tags", "tag", ".")
130 if err != nil {
131 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
132 }
133 result, err = osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(output)
134 if err != nil {
135 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
136 }
137 if !bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Containsscip-go gomod github.com/golang/go/src go1.25.0 bytes/Contains().(result, protectedConst) {
138 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("tagged variable does not appear in tagged run")
139 }
140 }
141
142 // TestConstValueChange verifies that if a constant value changes and
143 // the stringer code is not regenerated, we'll get a compiler error.
144 func TestConstValueChangescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/TestConstValueChange().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
145 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go")
146
147 stringer := stringerPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerPath().(t)
148 dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().()
149 source := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "day.go")
150 err := copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/copy().(source, filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().("testdata", "day.go"))
151 if err != nil {
152 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
153 }
154 stringSource := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "day_string.go")
155 // Run stringer in the directory that contains the module that contains the package files.
156 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, "go.mod"), []byte("module test\n"), 0o600); err != nil {
157 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
158 }
159 err = runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t, dir, stringer, "-type", "Day", "-output", stringSource)
160 if err != nil {
161 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
162 }
163 // Run the binary in the temporary directory as a sanity check.
164 err = runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/run().(t, "go", "run", stringSource, source)
165 if err != nil {
166 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
167 }
168 // Overwrite the source file with a version that has changed constants.
169 err = copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/copy().(source, filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().("testdata", "vary_day.go"))
170 if err != nil {
171 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
172 }
173 // Unfortunately different compilers may give different error messages,
174 // so there's no easy way to verify that the build failed specifically
175 // because the constants changed rather than because the vary_day.go
176 // file is invalid.
177 //
178 // Instead we'll just rely on manual inspection of the polluted test
179 // output. An alternative might be to check that the error output
180 // matches a set of possible error strings emitted by known
181 // Go compilers.
182 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Note: the following messages should indicate an out-of-bounds compiler error\n")
183 err = runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/run().(t, "go", "build", stringSource, source)
184 if err == nil {
185 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("unexpected compiler success")
186 }
187 }
188
189 var testfileSrcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/testfileSrcs. = map[string]string{
190 "go.mod": "module foo",
191
192 // Normal file in the package.
193 "main.go": `package foo
194
195 type Foo int
196
197 const (
198 fooX Foo = iota
199 fooY
200 fooZ
201 )
202 `,
203
204 // Test file in the package.
205 "main_test.go": `package foo
206
207 type Bar int
208
209 const (
210 barX Bar = iota
211 barY
212 barZ
213 )
214 `,
215
216 // Test file in the test package.
217 "main_pkg_test.go": `package foo_test
218
219 type Baz int
220
221 const (
222 bazX Baz = iota
223 bazY
224 bazZ
225 )
226 `,
227 }
228
229 // Test stringer on types defined in different kinds of tests.
230 // The generated code should not interfere between itself.
231 func TestTestFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/TestTestFiles().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
232 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go")
233 stringer := stringerPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerPath().(t)
234
235 dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().()
236 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("TestTestFiles in: %s \n", dir)
237 for name, src := range testfileSrcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/testfileSrcs. {
238 source := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, name)
239 err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(source, []byte(src), 0666)
240 if err != nil {
241 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("write file: %s", err)
242 }
243 }
244
245 // Must run stringer in the temp directory, see TestTags.
246 err := runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t, dir, stringer, "-type=Foo,Bar,Baz", dir)
247 if err != nil {
248 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("run stringer: %s", err)
249 }
250
251 // Check that stringer has created the expected files.
252 content, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 os/ReadDir().(dir)
253 if err != nil {
254 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("read dir: %s", err)
255 }
256 gotFiles := []string{}
257 for _, f := range content {
258 if !f.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#IsDir().() {
259 gotFiles = append(gotFiles, f.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().())
260 }
261 }
262 wantFiles := []string{
263 // Original.
264 "go.mod",
265 "main.go",
266 "main_test.go",
267 "main_pkg_test.go",
268 // Generated.
269 "foo_string.go",
270 "bar_string_test.go",
271 "baz_string_test.go",
272 }
273 slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Sortscip-go gomod github.com/golang/go/src go1.25.0 slices/Sort().(gotFiles)
274 slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Sortscip-go gomod github.com/golang/go/src go1.25.0 slices/Sort().(wantFiles)
275 if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(gotFiles, wantFiles) {
276 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("stringer generated files:\n%s\n\nbut want:\n%s",
277 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(gotFiles, "\n"),
278 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(wantFiles, "\n"),
279 )
280 }
281
282 // Run go test as a smoke test.
283 err = runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t, dir, "go", "test", "-count=1", ".")
284 if err != nil {
285 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("go test: %s", err)
286 }
287 }
288
289 // The -output flag cannot be used in combination with matching types across multiple packages.
290 func TestCollidingOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/TestCollidingOutput().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
291 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go")
292 stringer := stringerPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerPath().(t)
293
294 dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().()
295 for name, src := range testfileSrcsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/testfileSrcs. {
296 source := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, name)
297 err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(source, []byte(src), 0666)
298 if err != nil {
299 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("write file: %s", err)
300 }
301 }
302
303 // Must run stringer in the temp directory, see TestTags.
304 err := runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t, dir, stringer, "-type=Foo,Bar,Baz", "-output=somefile.go", dir)
305 if err == nil {
306 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("unexpected stringer success")
307 }
308 }
309
310 var exescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe. struct {
311 pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:path. string
312 errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:err. error
313 oncescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:once. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Oncescip-go gomod github.com/golang/go/src go1.25.0 sync/Once#
314 }
315
316 func stringerPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerPath().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) string {
317 testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsExecscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsExec().(t)
318
319 exescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe..oncescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:once..Doscip-go gomod github.com/golang/go/src go1.25.0 sync/Once#Do().(func() {
320 exescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe..pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:path., exescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe..errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:err. = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Executablescip-go gomod github.com/golang/go/src go1.25.0 os/Executable().()
321 })
322 if exescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe..errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:err. != nil {
323 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(exescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe..errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:err.)
324 }
325 return exescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe..pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/exe:path.
326 }
327
328 // stringerCompileAndRun runs stringer for the named file and compiles and
329 // runs the target binary in directory dir. That binary will panic if the String method is incorrect.
330 func stringerCompileAndRunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/stringerCompileAndRun().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, dir, stringer, typeName, fileName string) {
331 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("run: %s %s\n", fileName, typeName)
332 source := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(fileName))
333 err := copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/copy().(source, filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().("testdata", fileName))
334 if err != nil {
335 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("copying file to temporary directory: %s", err)
336 }
337 stringSource := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, typeName+"_string.go")
338 // Run stringer in temporary directory.
339 err = runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/run().(t, stringer, "-type", typeName, "-output", stringSource, source)
340 if err != nil {
341 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
342 }
343 // Run the binary in the temporary directory.
344 err = runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/run().(t, "go", "run", stringSource, source)
345 if err != nil {
346 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
347 }
348 }
349
350 // copy copies the from file to the to file.
351 func copyscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/copy().(to, from string) error {
352 toFd, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Createscip-go gomod github.com/golang/go/src go1.25.0 os/Create().(to)
353 if err != nil {
354 return err
355 }
356 defer toFd.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().()
357 fromFd, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(from)
358 if err != nil {
359 return err
360 }
361 defer fromFd.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().()
362 _, err = ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Copyscip-go gomod github.com/golang/go/src go1.25.0 io/Copy().(toFd, fromFd)
363 return err
364 }
365
366 // run runs a single command and returns an error if it does not succeed.
367 // os/exec should have this function, to be honest.
368 func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/run().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#, name string, arg ...string) error {
369 t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Helper().()
370 return runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t, ".", name, arg...)
371 }
372
373 // runInDir runs a single command in directory dir and returns an error if
374 // it does not succeed.
375 func runInDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/stringer`/runInDir().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#, dir, name string, arg ...string) error {
376 t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Helper().()
377 cmd := testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.Commandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Command().(t, name, arg...)
378 cmd.Dirscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Dir. = dir
379 out, err := cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().()
380 if len(out) > 0 {
381 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Logf().("%s", out)
382 }
383 if err != nil {
384 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%v: %v", cmd, err)
385 }
386 return nil
387 }