| 1 | // Copyright 2021 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:build go1.18 |
| 6 | // +build go1.18 |
| 7 | |
| 8 | package typeparams_test |
| 9 | |
| 10 | import ( |
| 11 | "go/ast" |
| 12 | "go/importer" |
| 13 | "go/parser" |
| 14 | "go/token" |
| 15 | "go/types" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | |
| 19 | "golang.org/x/tools/internal/apidiff" |
| 20 | "golang.org/x/tools/internal/testenv" |
| 21 | ) |
| 22 | |
| 23 | func TestAPIConsistency(t *testing.T) { |
| 24 | testenv.NeedsGoBuild(t) // This is a lie. We actually need the source code. |
| 25 | |
| 26 | // The packages below exclude enabled_*.go, as typeparams.Enabled is |
| 27 | // permitted to change between versions. |
| 28 | old := typeCheck(t, []string{"common.go", "typeparams_go117.go"}) |
| 29 | new := typeCheck(t, []string{"common.go", "typeparams_go118.go"}) |
| 30 | |
| 31 | report := apidiff.Changes(old, new) |
| 32 | |
| 33 | // Temporarily ignore API diff related to Environment, so that we can use a |
| 34 | // transient alias in go/types to allow renaming this type without ever |
| 35 | // breaking the x/tools builder. |
| 36 | // TODO(rfindley): remove this |
| 37 | var filteredChanges []apidiff.Change |
| 38 | for _, change := range report.Changes { |
| 39 | if strings.Contains(change.Message, "Environment") { |
| 40 | continue |
| 41 | } |
| 42 | filteredChanges = append(filteredChanges, change) |
| 43 | } |
| 44 | report.Changes = filteredChanges |
| 45 | if len(report.Changes) > 0 { |
| 46 | t.Errorf("API diff:\n%s", report) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func typeCheck(t *testing.T, filenames []string) *types.Package { |
| 51 | fset := token.NewFileSet() |
| 52 | var files []*ast.File |
| 53 | for _, name := range filenames { |
| 54 | f, err := parser.ParseFile(fset, name, nil, 0) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | files = append(files, f) |
| 59 | } |
| 60 | conf := types.Config{ |
| 61 | Importer: importer.Default(), |
| 62 | } |
| 63 | pkg, err := conf.Check("", fset, files, nil) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | return pkg |
| 68 | } |
| 69 |
Members