| 1 | // Copyright 2024 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 | // go1.23 is required for os.CopyFS. |
| 6 | // !android is required for compatibility with endtoend_test.go. |
| 7 | //go:build go1.23 && !android |
| 8 | |
| 9 | package main |
| 10 | |
| 11 | import ( |
| 12 | "bytes" |
| 13 | "errors" |
| 14 | "fmt" |
| 15 | "os" |
| 16 | "path/filepath" |
| 17 | "testing" |
| 18 | |
| 19 | "golang.org/x/tools/internal/diffp" |
| 20 | "golang.org/x/tools/internal/testenv" |
| 21 | "golang.org/x/tools/txtar" |
| 22 | ) |
| 23 | |
| 24 | // This file contains a test that checks the output files existence |
| 25 | // and content when stringer has types from multiple different input |
| 26 | // files to choose from. |
| 27 | // |
| 28 | // Input is specified in a txtar string. |
| 29 | |
| 30 | // Several tests expect the type Foo generated in some package. |
| 31 | func expectFooString(pkg string) []byte { |
| 32 | return fmt.Appendf(nil, ` |
| 33 | // Header comment ignored. |
| 34 | |
| 35 | package %s |
| 36 | |
| 37 | import "strconv" |
| 38 | |
| 39 | func _() { |
| 40 | // An "invalid array index" compiler error signifies that the constant values have changed. |
| 41 | // Re-run the stringer command to generate them again. |
| 42 | var x [1]struct{} |
| 43 | _ = x[fooX-0] |
| 44 | _ = x[fooY-1] |
| 45 | _ = x[fooZ-2] |
| 46 | } |
| 47 | |
| 48 | const _Foo_name = "fooXfooYfooZ" |
| 49 | |
| 50 | var _Foo_index = [...]uint8{0, 4, 8, 12} |
| 51 | |
| 52 | func (i Foo) String() string { |
| 53 | idx := int(i) - 0 |
| 54 | if i < 0 || idx >= len(_Foo_index)-1 { |
| 55 | return "Foo(" + strconv.FormatInt(int64(i), 10) + ")" |
| 56 | } |
| 57 | return _Foo_name[_Foo_index[idx]:_Foo_index[idx+1]] |
| 58 | }`, pkg) |
| 59 | } |
| 60 | |
| 61 | func TestMultifileStringer(t *testing.T) { |
| 62 | testenv.NeedsTool(t, "go") |
| 63 | stringer := stringerPath(t) |
| 64 | |
| 65 | tests := []struct { |
| 66 | name string |
| 67 | args []string |
| 68 | archive []byte |
| 69 | expectFiles map[string][]byte |
| 70 | }{ |
| 71 | { |
| 72 | name: "package only", |
| 73 | args: []string{"-type=Foo"}, |
| 74 | archive: []byte(` |
| 75 | -- go.mod -- |
| 76 | module foo |
| 77 | |
| 78 | -- main.go -- |
| 79 | package main |
| 80 | |
| 81 | type Foo int |
| 82 | |
| 83 | const ( |
| 84 | fooX Foo = iota |
| 85 | fooY |
| 86 | fooZ |
| 87 | )`), |
| 88 | expectFiles: map[string][]byte{ |
| 89 | "foo_string.go": expectFooString("main"), |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | name: "test package only", |
| 94 | args: []string{"-type=Foo"}, |
| 95 | archive: []byte(` |
| 96 | -- go.mod -- |
| 97 | module foo |
| 98 | |
| 99 | -- main.go -- |
| 100 | package main |
| 101 | |
| 102 | func main() {} |
| 103 | |
| 104 | -- main_test.go -- |
| 105 | package main |
| 106 | |
| 107 | type Foo int |
| 108 | |
| 109 | const ( |
| 110 | fooX Foo = iota |
| 111 | fooY |
| 112 | fooZ |
| 113 | )`), |
| 114 | expectFiles: map[string][]byte{ |
| 115 | "foo_string_test.go": expectFooString("main"), |
| 116 | }, |
| 117 | }, |
| 118 | { |
| 119 | name: "x_test package only", |
| 120 | args: []string{"-type=Foo"}, |
| 121 | archive: []byte(` |
| 122 | -- go.mod -- |
| 123 | module foo |
| 124 | |
| 125 | -- main.go -- |
| 126 | package main |
| 127 | |
| 128 | func main() {} |
| 129 | |
| 130 | -- main_test.go -- |
| 131 | package main_test |
| 132 | |
| 133 | type Foo int |
| 134 | |
| 135 | const ( |
| 136 | fooX Foo = iota |
| 137 | fooY |
| 138 | fooZ |
| 139 | )`), |
| 140 | expectFiles: map[string][]byte{ |
| 141 | "foo_string_test.go": expectFooString("main_test"), |
| 142 | }, |
| 143 | }, |
| 144 | { |
| 145 | // Re-declaring the type in a less prioritized package does not change our output. |
| 146 | name: "package over test package", |
| 147 | args: []string{"-type=Foo"}, |
| 148 | archive: []byte(` |
| 149 | -- go.mod -- |
| 150 | module foo |
| 151 | |
| 152 | -- main.go -- |
| 153 | package main |
| 154 | |
| 155 | type Foo int |
| 156 | |
| 157 | const ( |
| 158 | fooX Foo = iota |
| 159 | fooY |
| 160 | fooZ |
| 161 | ) |
| 162 | |
| 163 | -- main_test.go -- |
| 164 | package main |
| 165 | |
| 166 | type Foo int |
| 167 | |
| 168 | const ( |
| 169 | otherX Foo = iota |
| 170 | otherY |
| 171 | otherZ |
| 172 | ) |
| 173 | `), |
| 174 | expectFiles: map[string][]byte{ |
| 175 | "foo_string.go": expectFooString("main"), |
| 176 | }, |
| 177 | }, |
| 178 | { |
| 179 | // Re-declaring the type in a less prioritized package does not change our output. |
| 180 | name: "package over x_test package", |
| 181 | args: []string{"-type=Foo"}, |
| 182 | archive: []byte(` |
| 183 | -- go.mod -- |
| 184 | module foo |
| 185 | |
| 186 | -- main.go -- |
| 187 | package main |
| 188 | |
| 189 | type Foo int |
| 190 | |
| 191 | const ( |
| 192 | fooX Foo = iota |
| 193 | fooY |
| 194 | fooZ |
| 195 | ) |
| 196 | |
| 197 | -- main_test.go -- |
| 198 | package main_test |
| 199 | |
| 200 | type Foo int |
| 201 | |
| 202 | const ( |
| 203 | otherX Foo = iota |
| 204 | otherY |
| 205 | otherZ |
| 206 | ) |
| 207 | `), |
| 208 | expectFiles: map[string][]byte{ |
| 209 | "foo_string.go": expectFooString("main"), |
| 210 | }, |
| 211 | }, |
| 212 | { |
| 213 | // Re-declaring the type in a less prioritized package does not change our output. |
| 214 | name: "test package over x_test package", |
| 215 | args: []string{"-type=Foo"}, |
| 216 | archive: []byte(` |
| 217 | -- go.mod -- |
| 218 | module foo |
| 219 | |
| 220 | -- main.go -- |
| 221 | package main |
| 222 | |
| 223 | -- main_test.go -- |
| 224 | package main |
| 225 | |
| 226 | type Foo int |
| 227 | |
| 228 | const ( |
| 229 | fooX Foo = iota |
| 230 | fooY |
| 231 | fooZ |
| 232 | ) |
| 233 | |
| 234 | -- main_pkg_test.go -- |
| 235 | package main_test |
| 236 | |
| 237 | type Foo int |
| 238 | |
| 239 | const ( |
| 240 | otherX Foo = iota |
| 241 | otherY |
| 242 | otherZ |
| 243 | )`), |
| 244 | expectFiles: map[string][]byte{ |
| 245 | "foo_string_test.go": expectFooString("main"), |
| 246 | }, |
| 247 | }, |
| 248 | { |
| 249 | name: "unique type in each package variant", |
| 250 | args: []string{"-type=Foo,Bar,Baz"}, |
| 251 | archive: []byte(` |
| 252 | -- go.mod -- |
| 253 | module foo |
| 254 | |
| 255 | -- main.go -- |
| 256 | package main |
| 257 | |
| 258 | type Foo int |
| 259 | |
| 260 | const fooX Foo = 1 |
| 261 | |
| 262 | -- main_test.go -- |
| 263 | package main |
| 264 | |
| 265 | type Bar int |
| 266 | |
| 267 | const barX Bar = 1 |
| 268 | |
| 269 | -- main_pkg_test.go -- |
| 270 | package main_test |
| 271 | |
| 272 | type Baz int |
| 273 | |
| 274 | const bazX Baz = 1 |
| 275 | `), |
| 276 | expectFiles: map[string][]byte{ |
| 277 | "foo_string.go": []byte(` |
| 278 | // Header comment ignored. |
| 279 | |
| 280 | package main |
| 281 | |
| 282 | import "strconv" |
| 283 | |
| 284 | func _() { |
| 285 | // An "invalid array index" compiler error signifies that the constant values have changed. |
| 286 | // Re-run the stringer command to generate them again. |
| 287 | var x [1]struct{} |
| 288 | _ = x[fooX-1] |
| 289 | } |
| 290 | |
| 291 | const _Foo_name = "fooX" |
| 292 | |
| 293 | var _Foo_index = [...]uint8{0, 4} |
| 294 | |
| 295 | func (i Foo) String() string { |
| 296 | idx := int(i) - 1 |
| 297 | if i < 1 || idx >= len(_Foo_index)-1 { |
| 298 | return "Foo(" + strconv.FormatInt(int64(i), 10) + ")" |
| 299 | } |
| 300 | return _Foo_name[_Foo_index[idx]:_Foo_index[idx+1]] |
| 301 | }`), |
| 302 | |
| 303 | "bar_string_test.go": []byte(` |
| 304 | // Header comment ignored. |
| 305 | |
| 306 | package main |
| 307 | |
| 308 | import "strconv" |
| 309 | |
| 310 | func _() { |
| 311 | // An "invalid array index" compiler error signifies that the constant values have changed. |
| 312 | // Re-run the stringer command to generate them again. |
| 313 | var x [1]struct{} |
| 314 | _ = x[barX-1] |
| 315 | } |
| 316 | |
| 317 | const _Bar_name = "barX" |
| 318 | |
| 319 | var _Bar_index = [...]uint8{0, 4} |
| 320 | |
| 321 | func (i Bar) String() string { |
| 322 | idx := int(i) - 1 |
| 323 | if i < 1 || idx >= len(_Bar_index)-1 { |
| 324 | return "Bar(" + strconv.FormatInt(int64(i), 10) + ")" |
| 325 | } |
| 326 | return _Bar_name[_Bar_index[idx]:_Bar_index[idx+1]] |
| 327 | }`), |
| 328 | |
| 329 | "baz_string_test.go": []byte(` |
| 330 | // Header comment ignored. |
| 331 | |
| 332 | package main_test |
| 333 | |
| 334 | import "strconv" |
| 335 | |
| 336 | func _() { |
| 337 | // An "invalid array index" compiler error signifies that the constant values have changed. |
| 338 | // Re-run the stringer command to generate them again. |
| 339 | var x [1]struct{} |
| 340 | _ = x[bazX-1] |
| 341 | } |
| 342 | |
| 343 | const _Baz_name = "bazX" |
| 344 | |
| 345 | var _Baz_index = [...]uint8{0, 4} |
| 346 | |
| 347 | func (i Baz) String() string { |
| 348 | idx := int(i) - 1 |
| 349 | if i < 1 || idx >= len(_Baz_index)-1 { |
| 350 | return "Baz(" + strconv.FormatInt(int64(i), 10) + ")" |
| 351 | } |
| 352 | return _Baz_name[_Baz_index[idx]:_Baz_index[idx+1]] |
| 353 | }`), |
| 354 | }, |
| 355 | }, |
| 356 | |
| 357 | { |
| 358 | name: "package over test package with custom output", |
| 359 | args: []string{"-type=Foo", "-output=custom_output.go"}, |
| 360 | archive: []byte(` |
| 361 | -- go.mod -- |
| 362 | module foo |
| 363 | |
| 364 | -- main.go -- |
| 365 | package main |
| 366 | |
| 367 | type Foo int |
| 368 | |
| 369 | const ( |
| 370 | fooX Foo = iota |
| 371 | fooY |
| 372 | fooZ |
| 373 | ) |
| 374 | |
| 375 | -- main_test.go -- |
| 376 | package main |
| 377 | |
| 378 | type Foo int |
| 379 | |
| 380 | const ( |
| 381 | otherX Foo = iota |
| 382 | otherY |
| 383 | otherZ |
| 384 | )`), |
| 385 | expectFiles: map[string][]byte{ |
| 386 | "custom_output.go": expectFooString("main"), |
| 387 | }, |
| 388 | }, |
| 389 | } |
| 390 | |
| 391 | for _, tt := range tests { |
| 392 | t.Run(tt.name, func(t *testing.T) { |
| 393 | tmpDir := t.TempDir() |
| 394 | |
| 395 | arFS, err := txtar.FS(txtar.Parse(tt.archive)) |
| 396 | if err != nil { |
| 397 | t.Fatalf("txtar.FS: %s", err) |
| 398 | } |
| 399 | err = os.CopyFS(tmpDir, arFS) |
| 400 | if err != nil { |
| 401 | t.Fatalf("copy fs: %s", err) |
| 402 | } |
| 403 | before := dirContent(t, tmpDir) |
| 404 | |
| 405 | // Must run stringer in the temp directory, see TestTags. |
| 406 | args := append(tt.args, tmpDir) |
| 407 | err = runInDir(t, tmpDir, stringer, args...) |
| 408 | if err != nil { |
| 409 | t.Fatalf("run stringer: %s", err) |
| 410 | } |
| 411 | |
| 412 | // Check that all !path files have been created with the expected content. |
| 413 | for f, want := range tt.expectFiles { |
| 414 | got, err := os.ReadFile(filepath.Join(tmpDir, f)) |
| 415 | if errors.Is(err, os.ErrNotExist) { |
| 416 | t.Errorf("expected file not written during test: %s", f) |
| 417 | continue |
| 418 | } |
| 419 | if err != nil { |
| 420 | t.Fatalf("read file %q: %s", f, err) |
| 421 | } |
| 422 | // Trim data for more robust comparison. |
| 423 | got = trimHeader(bytes.TrimSpace(got)) |
| 424 | want = trimHeader(bytes.TrimSpace(want)) |
| 425 | if !bytes.Equal(want, got) { |
| 426 | t.Errorf("file %s does not have the expected content:\n%s", f, diffp.Diff("want", want, "got", got)) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Check that nothing else has been created. |
| 431 | after := dirContent(t, tmpDir) |
| 432 | for f := range after { |
| 433 | if _, expected := tt.expectFiles[f]; !expected && !before[f] { |
| 434 | t.Errorf("found %q in output directory, it is neither input or expected output", f) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | }) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | func dirContent(t *testing.T, dir string) map[string]bool { |
| 443 | entries, err := os.ReadDir(dir) |
| 444 | if err != nil { |
| 445 | t.Fatalf("read dir: %s", err) |
| 446 | } |
| 447 | |
| 448 | out := map[string]bool{} |
| 449 | for _, e := range entries { |
| 450 | out[e.Name()] = true |
| 451 | } |
| 452 | return out |
| 453 | } |
| 454 | |
| 455 | // trimHeader that stringer puts in file. |
| 456 | // It depends on location and interferes with comparing file content. |
| 457 | func trimHeader(s []byte) []byte { |
| 458 | if !bytes.HasPrefix(s, []byte("//")) { |
| 459 | return s |
| 460 | } |
| 461 | _, after, ok := bytes.Cut(s, []byte{'\n'}) |
| 462 | if ok { |
| 463 | return after |
| 464 | } |
| 465 | return s |
| 466 | } |