| 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 main |
| 10 |
|
| 11 |
import ( |
| 12 |
"bytes" |
| 13 |
"flag" |
| 14 |
"fmt" |
| 15 |
"io" |
| 16 |
"os" |
| 17 |
"path" |
| 18 |
"path/filepath" |
| 19 |
"reflect" |
| 20 |
"slices" |
| 21 |
"strings" |
| 22 |
"sync" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"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 TestMain(m *testing.M) { |
| 34 |
if os.Getenv("STRINGER_TEST_IS_STRINGER") != "" { |
| 35 |
main() |
| 36 |
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 |
os.Setenv("STRINGER_TEST_IS_STRINGER", "1") |
| 43 |
|
| 44 |
flag.Parse() |
| 45 |
if testing.Verbose() { |
| 46 |
os.Setenv("GOPACKAGESDEBUG", "true") |
| 47 |
} |
| 48 |
|
| 49 |
os.Exit(m.Run()) |
| 50 |
} |
| 51 |
|
| 52 |
func TestEndToEnd(t *testing.T) { |
| 53 |
testenv.NeedsTool(t, "go") |
| 54 |
|
| 55 |
stringer := stringerPath(t) |
| 56 |
// Read the testdata directory. |
| 57 |
fd, err := os.Open("testdata") |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
defer fd.Close() |
| 62 |
names, err := fd.Readdirnames(-1) |
| 63 |
if err != nil { |
| 64 |
t.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 !strings.HasSuffix(name, ".go") { |
| 73 |
t.Errorf("%s is not a Go file", name) |
| 74 |
continue |
| 75 |
} |
| 76 |
if strings.HasPrefix(name, "tag_") || strings.HasPrefix(name, "vary_") { |
| 77 |
// This file is used for tag processing in TestTags or TestConstValueChange, below. |
| 78 |
continue |
| 79 |
} |
| 80 |
t.Run(name, func(t *testing.T) { |
| 81 |
if name == "cgo.go" { |
| 82 |
testenv.NeedsTool(t, "cgo") |
| 83 |
} |
| 84 |
stringerCompileAndRun(t, t.TempDir(), 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 typeName(fname string) string { |
| 91 |
// file names are known to be ascii and end .go |
| 92 |
base := path.Base(fname) |
| 93 |
return 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 TestTags(t *testing.T) { |
| 98 |
stringer := stringerPath(t) |
| 99 |
dir := t.TempDir() |
| 100 |
var ( |
| 101 |
protectedConst = []byte("TagProtected") |
| 102 |
output = filepath.Join(dir, "const_string.go") |
| 103 |
) |
| 104 |
for _, file := range []string{"tag_main.go", "tag_tag.go"} { |
| 105 |
err := copy(filepath.Join(dir, file), filepath.Join("testdata", file)) |
| 106 |
if err != nil { |
| 107 |
t.Fatal(err) |
| 108 |
} |
| 109 |
} |
| 110 |
// Run stringer in the directory that contains the module that contains the package files. |
| 111 |
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module test\n"), 0o600); err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
err := runInDir(t, dir, stringer, "-type", "Const", ".") |
| 115 |
if err != nil { |
| 116 |
t.Fatal(err) |
| 117 |
} |
| 118 |
result, err := os.ReadFile(output) |
| 119 |
if err != nil { |
| 120 |
t.Fatal(err) |
| 121 |
} |
| 122 |
if bytes.Contains(result, protectedConst) { |
| 123 |
t.Fatal("tagged variable appears in untagged run") |
| 124 |
} |
| 125 |
err = os.Remove(output) |
| 126 |
if err != nil { |
| 127 |
t.Fatal(err) |
| 128 |
} |
| 129 |
err = runInDir(t, dir, stringer, "-type", "Const", "-tags", "tag", ".") |
| 130 |
if err != nil { |
| 131 |
t.Fatal(err) |
| 132 |
} |
| 133 |
result, err = os.ReadFile(output) |
| 134 |
if err != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
if !bytes.Contains(result, protectedConst) { |
| 138 |
t.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 TestConstValueChange(t *testing.T) { |
| 145 |
testenv.NeedsTool(t, "go") |
| 146 |
|
| 147 |
stringer := stringerPath(t) |
| 148 |
dir := t.TempDir() |
| 149 |
source := filepath.Join(dir, "day.go") |
| 150 |
err := copy(source, filepath.Join("testdata", "day.go")) |
| 151 |
if err != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
stringSource := filepath.Join(dir, "day_string.go") |
| 155 |
// Run stringer in the directory that contains the module that contains the package files. |
| 156 |
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module test\n"), 0o600); err != nil { |
| 157 |
t.Fatal(err) |
| 158 |
} |
| 159 |
err = runInDir(t, dir, stringer, "-type", "Day", "-output", stringSource) |
| 160 |
if err != nil { |
| 161 |
t.Fatal(err) |
| 162 |
} |
| 163 |
// Run the binary in the temporary directory as a sanity check. |
| 164 |
err = run(t, "go", "run", stringSource, source) |
| 165 |
if err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
// Overwrite the source file with a version that has changed constants. |
| 169 |
err = copy(source, filepath.Join("testdata", "vary_day.go")) |
| 170 |
if err != nil { |
| 171 |
t.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.Logf("Note: the following messages should indicate an out-of-bounds compiler error\n") |
| 183 |
err = run(t, "go", "build", stringSource, source) |
| 184 |
if err == nil { |
| 185 |
t.Fatal("unexpected compiler success") |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
var 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 TestTestFiles(t *testing.T) { |
| 232 |
testenv.NeedsTool(t, "go") |
| 233 |
stringer := stringerPath(t) |
| 234 |
|
| 235 |
dir := t.TempDir() |
| 236 |
t.Logf("TestTestFiles in: %s \n", dir) |
| 237 |
for name, src := range testfileSrcs { |
| 238 |
source := filepath.Join(dir, name) |
| 239 |
err := os.WriteFile(source, []byte(src), 0666) |
| 240 |
if err != nil { |
| 241 |
t.Fatalf("write file: %s", err) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// Must run stringer in the temp directory, see TestTags. |
| 246 |
err := runInDir(t, dir, stringer, "-type=Foo,Bar,Baz", dir) |
| 247 |
if err != nil { |
| 248 |
t.Fatalf("run stringer: %s", err) |
| 249 |
} |
| 250 |
|
| 251 |
// Check that stringer has created the expected files. |
| 252 |
content, err := os.ReadDir(dir) |
| 253 |
if err != nil { |
| 254 |
t.Fatalf("read dir: %s", err) |
| 255 |
} |
| 256 |
gotFiles := []string{} |
| 257 |
for _, f := range content { |
| 258 |
if !f.IsDir() { |
| 259 |
gotFiles = append(gotFiles, f.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 |
slices.Sort(gotFiles) |
| 274 |
slices.Sort(wantFiles) |
| 275 |
if !reflect.DeepEqual(gotFiles, wantFiles) { |
| 276 |
t.Errorf("stringer generated files:\n%s\n\nbut want:\n%s", |
| 277 |
strings.Join(gotFiles, "\n"), |
| 278 |
strings.Join(wantFiles, "\n"), |
| 279 |
) |
| 280 |
} |
| 281 |
|
| 282 |
// Run go test as a smoke test. |
| 283 |
err = runInDir(t, dir, "go", "test", "-count=1", ".") |
| 284 |
if err != nil { |
| 285 |
t.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 TestCollidingOutput(t *testing.T) { |
| 291 |
testenv.NeedsTool(t, "go") |
| 292 |
stringer := stringerPath(t) |
| 293 |
|
| 294 |
dir := t.TempDir() |
| 295 |
for name, src := range testfileSrcs { |
| 296 |
source := filepath.Join(dir, name) |
| 297 |
err := os.WriteFile(source, []byte(src), 0666) |
| 298 |
if err != nil { |
| 299 |
t.Fatalf("write file: %s", err) |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// Must run stringer in the temp directory, see TestTags. |
| 304 |
err := runInDir(t, dir, stringer, "-type=Foo,Bar,Baz", "-output=somefile.go", dir) |
| 305 |
if err == nil { |
| 306 |
t.Fatal("unexpected stringer success") |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
var exe struct { |
| 311 |
path string |
| 312 |
err error |
| 313 |
once sync.Once |
| 314 |
} |
| 315 |
|
| 316 |
func stringerPath(t *testing.T) string { |
| 317 |
testenv.NeedsExec(t) |
| 318 |
|
| 319 |
exe.once.Do(func() { |
| 320 |
exe.path, exe.err = os.Executable() |
| 321 |
}) |
| 322 |
if exe.err != nil { |
| 323 |
t.Fatal(exe.err) |
| 324 |
} |
| 325 |
return 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 stringerCompileAndRun(t *testing.T, dir, stringer, typeName, fileName string) { |
| 331 |
t.Logf("run: %s %s\n", fileName, typeName) |
| 332 |
source := filepath.Join(dir, path.Base(fileName)) |
| 333 |
err := copy(source, filepath.Join("testdata", fileName)) |
| 334 |
if err != nil { |
| 335 |
t.Fatalf("copying file to temporary directory: %s", err) |
| 336 |
} |
| 337 |
stringSource := filepath.Join(dir, typeName+"_string.go") |
| 338 |
// Run stringer in temporary directory. |
| 339 |
err = run(t, stringer, "-type", typeName, "-output", stringSource, source) |
| 340 |
if err != nil { |
| 341 |
t.Fatal(err) |
| 342 |
} |
| 343 |
// Run the binary in the temporary directory. |
| 344 |
err = run(t, "go", "run", stringSource, source) |
| 345 |
if err != nil { |
| 346 |
t.Fatal(err) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
// copy copies the from file to the to file. |
| 351 |
func copy(to, from string) error { |
| 352 |
toFd, err := os.Create(to) |
| 353 |
if err != nil { |
| 354 |
return err |
| 355 |
} |
| 356 |
defer toFd.Close() |
| 357 |
fromFd, err := os.Open(from) |
| 358 |
if err != nil { |
| 359 |
return err |
| 360 |
} |
| 361 |
defer fromFd.Close() |
| 362 |
_, err = 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 run(t testing.TB, name string, arg ...string) error { |
| 369 |
t.Helper() |
| 370 |
return 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 runInDir(t testing.TB, dir, name string, arg ...string) error { |
| 376 |
t.Helper() |
| 377 |
cmd := testenv.Command(t, name, arg...) |
| 378 |
cmd.Dir = dir |
| 379 |
out, err := cmd.CombinedOutput() |
| 380 |
if len(out) > 0 { |
| 381 |
t.Logf("%s", out) |
| 382 |
} |
| 383 |
if err != nil { |
| 384 |
return fmt.Errorf("%v: %v", cmd, err) |
| 385 |
} |
| 386 |
return nil |
| 387 |
} |