1
0
mirror of https://github.com/golang/go synced 2024-11-11 20:01:37 -07:00

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 <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Findley 2023-01-18 13:08:49 -05:00 committed by Gopher Robot
parent e587a7691b
commit 59964663e5
39 changed files with 82 additions and 70 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()
dstFilename := filepath.FromSlash(runtime.GOROOT() + dstDir + filename)
onDiskContent, err := os.ReadFile(dstFilename)
if err != nil {
t.Fatalf("reading %q: %v", filename, err)
}
// write dst
dstFilename := filepath.FromSlash(runtime.GOROOT() + "/src/" + dstDir + "/" + filename)
if err := os.WriteFile(dstFilename, buf.Bytes(), 0o644); err != nil {
log.Fatal(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))
}
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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