From 59964663e58d60719c707d9d59d7424d6f2ea7e0 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Wed, 18 Jan 2023 13:08:49 -0500 Subject: [PATCH] go/types: add a test that generated files match types2 Adjust the go/types file generation to run in a test, so that we can easily reuse the existing logic to verify that the current content of go/types matches the expected result of generating from types2. This test will enforce that we don't forget to regenerate go/types when making changes to types2. Change-Id: Iee14b1402065f7f0ecbcf28000e07a06c08fa42e Reviewed-on: https://go-review.googlesource.com/c/go/+/462758 Run-TryBot: Robert Findley TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer Auto-Submit: Robert Findley --- src/go/types/array.go | 2 +- src/go/types/basic.go | 2 +- src/go/types/chan.go | 2 +- src/go/types/context.go | 2 +- src/go/types/context_test.go | 2 +- src/go/types/gccgosizes.go | 2 +- src/go/types/generate.go | 2 +- .../types/{generator.go => generate_test.go} | 76 +++++++++++-------- src/go/types/hilbert_test.go | 2 +- src/go/types/infer.go | 2 +- src/go/types/instantiate.go | 2 +- src/go/types/instantiate_test.go | 2 +- src/go/types/lookup.go | 2 +- src/go/types/main_test.go | 2 +- src/go/types/map.go | 2 +- src/go/types/named.go | 2 +- src/go/types/object.go | 2 +- src/go/types/object_test.go | 2 +- src/go/types/objset.go | 2 +- src/go/types/package.go | 2 +- src/go/types/pointer.go | 2 +- src/go/types/predicates.go | 2 +- src/go/types/scope.go | 2 +- src/go/types/selection.go | 2 +- src/go/types/sizes.go | 2 +- src/go/types/slice.go | 2 +- src/go/types/subst.go | 2 +- src/go/types/termlist.go | 2 +- src/go/types/termlist_test.go | 2 +- src/go/types/tuple.go | 2 +- src/go/types/typelists.go | 2 +- src/go/types/typeparam.go | 2 +- src/go/types/typeterm.go | 2 +- src/go/types/typeterm_test.go | 2 +- src/go/types/under.go | 2 +- src/go/types/unify.go | 2 +- src/go/types/universe.go | 2 +- src/go/types/util_test.go | 2 +- src/go/types/validtype.go | 2 +- 39 files changed, 82 insertions(+), 70 deletions(-) rename src/go/types/{generator.go => generate_test.go} (83%) diff --git a/src/go/types/array.go b/src/go/types/array.go index 759e2f2e67..f19ce6e528 100644 --- a/src/go/types/array.go +++ b/src/go/types/array.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/basic.go b/src/go/types/basic.go index 7d6decdb1a..d483616413 100644 --- a/src/go/types/basic.go +++ b/src/go/types/basic.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/chan.go b/src/go/types/chan.go index 565e9d2e9d..940620067c 100644 --- a/src/go/types/chan.go +++ b/src/go/types/chan.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/context.go b/src/go/types/context.go index 81b2f139eb..56368e1060 100644 --- a/src/go/types/context.go +++ b/src/go/types/context.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/context_test.go b/src/go/types/context_test.go index 1340bf2053..c9f6dce728 100644 --- a/src/go/types/context_test.go +++ b/src/go/types/context_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/gccgosizes.go b/src/go/types/gccgosizes.go index d98105c69a..9152c811e1 100644 --- a/src/go/types/gccgosizes.go +++ b/src/go/types/gccgosizes.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/generate.go b/src/go/types/generate.go index 6759f117b9..7fec743170 100644 --- a/src/go/types/generate.go +++ b/src/go/types/generate.go @@ -3,6 +3,6 @@ // license that can be found in the LICENSE file. // This file exists only to drive go:generate. -//go:generate go run generator.go +//go:generate go test -run=Generate -write=all package types diff --git a/src/go/types/generator.go b/src/go/types/generate_test.go similarity index 83% rename from src/go/types/generator.go rename to src/go/types/generate_test.go index fc6295d3c8..b6942279a8 100644 --- a/src/go/types/generator.go +++ b/src/go/types/generate_test.go @@ -2,81 +2,93 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build ignore - // This file implements a custom generator to create various go/types // source files from the corresponding types2 files. -package main +package types_test import ( "bytes" "flag" - "fmt" "go/ast" "go/format" "go/parser" "go/token" - "log" + "internal/diff" "os" "path/filepath" "runtime" "strings" + "testing" ) +var filesToWrite = flag.String("write", "", `go/types files to generate, or "all" for all files`) + const ( - srcDir = "cmd/compile/internal/types2" - dstDir = "go/types" + srcDir = "/src/cmd/compile/internal/types2/" + dstDir = "/src/go/types/" ) -var fset = token.NewFileSet() - -func main() { - flag.Parse() - - // process provided filenames, if any - if flag.NArg() > 0 { - for _, filename := range flag.Args() { - generate(filename, filemap[filename]) +// TestGenerate verifies that generated files in go/types match their types2 +// counterpart. If -write is set, this test actually writes the expected +// content to go/types; otherwise, it just compares with the existing content. +func TestGenerate(t *testing.T) { + // If filesToWrite is set, write the generated content to disk. + // In the special case of "all", write all files in filemap. + write := *filesToWrite != "" + var files []string // files to process + if *filesToWrite != "" && *filesToWrite != "all" { + files = strings.Split(*filesToWrite, ",") + } else { + for file := range filemap { + files = append(files, file) } - fmt.Printf("generated %d files\n", flag.NArg()) - return } - // otherwise process per filemap below - for filename, action := range filemap { - generate(filename, action) + for _, filename := range files { + generate(t, filename, write) } - fmt.Printf("generated %d files\n", len(filemap)) } -func generate(filename string, action action) { +func generate(t *testing.T, filename string, write bool) { // parse src - srcFilename := filepath.FromSlash(runtime.GOROOT() + "/src/" + srcDir + "/" + filename) + srcFilename := filepath.FromSlash(runtime.GOROOT() + srcDir + filename) file, err := parser.ParseFile(fset, srcFilename, nil, parser.ParseComments) if err != nil { - log.Fatal(err) + t.Fatal(err) } // fix package name file.Name.Name = strings.ReplaceAll(file.Name.Name, "types2", "types") // rewrite AST as needed - if action != nil { + if action := filemap[filename]; action != nil { action(file) } // format AST var buf bytes.Buffer - buf.WriteString("// Code generated by \"go run generator.go\"; DO NOT EDIT.\n\n") + buf.WriteString("// Code generated by \"go test -run=Generate -write=all\"; DO NOT EDIT.\n\n") if err := format.Node(&buf, fset, file); err != nil { - log.Fatal(err) + t.Fatal(err) } + generatedContent := buf.Bytes() - // write dst - dstFilename := filepath.FromSlash(runtime.GOROOT() + "/src/" + dstDir + "/" + filename) - if err := os.WriteFile(dstFilename, buf.Bytes(), 0o644); err != nil { - log.Fatal(err) + dstFilename := filepath.FromSlash(runtime.GOROOT() + dstDir + filename) + onDiskContent, err := os.ReadFile(dstFilename) + if err != nil { + t.Fatalf("reading %q: %v", filename, err) + } + + if d := diff.Diff(filename+" (on disk)", onDiskContent, filename+" (generated)", generatedContent); d != nil { + if write { + t.Logf("applying change:\n%s", d) + if err := os.WriteFile(dstFilename, generatedContent, 0o644); err != nil { + t.Fatalf("writing %q: %v", filename, err) + } + } else { + t.Errorf("generated file content does not match:\n%s", string(d)) + } } } diff --git a/src/go/types/hilbert_test.go b/src/go/types/hilbert_test.go index 15e5ae3aca..7da2a7ded1 100644 --- a/src/go/types/hilbert_test.go +++ b/src/go/types/hilbert_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/infer.go b/src/go/types/infer.go index ef14353540..5f91b526d9 100644 --- a/src/go/types/infer.go +++ b/src/go/types/infer.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/instantiate.go b/src/go/types/instantiate.go index ab3a9451c0..11afb7a853 100644 --- a/src/go/types/instantiate.go +++ b/src/go/types/instantiate.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/instantiate_test.go b/src/go/types/instantiate_test.go index 91b2a1aaba..574f3aeb86 100644 --- a/src/go/types/instantiate_test.go +++ b/src/go/types/instantiate_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/lookup.go b/src/go/types/lookup.go index fa2b7b2f4b..2d6501b739 100644 --- a/src/go/types/lookup.go +++ b/src/go/types/lookup.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/main_test.go b/src/go/types/main_test.go index b0630947c1..fda9d102a3 100644 --- a/src/go/types/main_test.go +++ b/src/go/types/main_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2022 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/map.go b/src/go/types/map.go index 9827e8c147..febb0d3a0b 100644 --- a/src/go/types/map.go +++ b/src/go/types/map.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/named.go b/src/go/types/named.go index f1b2685ec3..413eaada27 100644 --- a/src/go/types/named.go +++ b/src/go/types/named.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/object.go b/src/go/types/object.go index 74b630af9e..e47ef2ebec 100644 --- a/src/go/types/object.go +++ b/src/go/types/object.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/object_test.go b/src/go/types/object_test.go index b0459260a1..bed8de3637 100644 --- a/src/go/types/object_test.go +++ b/src/go/types/object_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/objset.go b/src/go/types/objset.go index 4e7aaac500..e6ea37566e 100644 --- a/src/go/types/objset.go +++ b/src/go/types/objset.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/package.go b/src/go/types/package.go index 7a64aa9fe4..7aa62fb7a3 100644 --- a/src/go/types/package.go +++ b/src/go/types/package.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/pointer.go b/src/go/types/pointer.go index 091824c55e..5b45ab7755 100644 --- a/src/go/types/pointer.go +++ b/src/go/types/pointer.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/predicates.go b/src/go/types/predicates.go index c80d41d702..1927844732 100644 --- a/src/go/types/predicates.go +++ b/src/go/types/predicates.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2012 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/scope.go b/src/go/types/scope.go index 433f31a478..bf646f6882 100644 --- a/src/go/types/scope.go +++ b/src/go/types/scope.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/selection.go b/src/go/types/selection.go index bc26f39b3f..c79e13c6eb 100644 --- a/src/go/types/selection.go +++ b/src/go/types/selection.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/sizes.go b/src/go/types/sizes.go index fb7a16699a..d32866913e 100644 --- a/src/go/types/sizes.go +++ b/src/go/types/sizes.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/slice.go b/src/go/types/slice.go index 0fb8476d24..934549c2d4 100644 --- a/src/go/types/slice.go +++ b/src/go/types/slice.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/subst.go b/src/go/types/subst.go index 3be5c02be2..30c48e1bad 100644 --- a/src/go/types/subst.go +++ b/src/go/types/subst.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/termlist.go b/src/go/types/termlist.go index 46de92459b..9bc631c0e6 100644 --- a/src/go/types/termlist.go +++ b/src/go/types/termlist.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/termlist_test.go b/src/go/types/termlist_test.go index 7499fc6b9e..cf0c190516 100644 --- a/src/go/types/termlist_test.go +++ b/src/go/types/termlist_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/tuple.go b/src/go/types/tuple.go index 8ccf0289a3..e5e3914bb2 100644 --- a/src/go/types/tuple.go +++ b/src/go/types/tuple.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typelists.go b/src/go/types/typelists.go index 864a34fa54..c000de2272 100644 --- a/src/go/types/typelists.go +++ b/src/go/types/typelists.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typeparam.go b/src/go/types/typeparam.go index c573fa8633..763fcc61f0 100644 --- a/src/go/types/typeparam.go +++ b/src/go/types/typeparam.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typeterm.go b/src/go/types/typeterm.go index d862add06e..c86442c43f 100644 --- a/src/go/types/typeterm.go +++ b/src/go/types/typeterm.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/typeterm_test.go b/src/go/types/typeterm_test.go index bf67f3872d..c6370f44fe 100644 --- a/src/go/types/typeterm_test.go +++ b/src/go/types/typeterm_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/under.go b/src/go/types/under.go index 0c2410e74e..f17d3bcda4 100644 --- a/src/go/types/under.go +++ b/src/go/types/under.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/unify.go b/src/go/types/unify.go index 9db63ca9ec..9fb0c75893 100644 --- a/src/go/types/unify.go +++ b/src/go/types/unify.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2020 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/universe.go b/src/go/types/universe.go index 6984cf849b..d32a8ed4e8 100644 --- a/src/go/types/universe.go +++ b/src/go/types/universe.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/util_test.go b/src/go/types/util_test.go index 4667451a97..205237211f 100644 --- a/src/go/types/util_test.go +++ b/src/go/types/util_test.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2023 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style diff --git a/src/go/types/validtype.go b/src/go/types/validtype.go index 5fd5f73cfa..d915fef825 100644 --- a/src/go/types/validtype.go +++ b/src/go/types/validtype.go @@ -1,4 +1,4 @@ -// Code generated by "go run generator.go"; DO NOT EDIT. +// Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. // Copyright 2022 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style -- 2.50.0