| 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 |
// No testdata on Android. |
| 6 |
|
| 7 |
//go:build !android |
| 8 |
|
| 9 |
package eg_test |
| 10 |
|
| 11 |
import ( |
| 12 |
"bytes" |
| 13 |
"flag" |
| 14 |
"fmt" |
| 15 |
"go/constant" |
| 16 |
"go/format" |
| 17 |
"go/types" |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"runtime" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
"golang.org/x/tools/go/packages" |
| 26 |
"golang.org/x/tools/internal/testenv" |
| 27 |
"golang.org/x/tools/internal/testfiles" |
| 28 |
"golang.org/x/tools/refactor/eg" |
| 29 |
"golang.org/x/tools/txtar" |
| 30 |
) |
| 31 |
|
| 32 |
// TODO(adonovan): more tests: |
| 33 |
// - of command-line tool |
| 34 |
// - of all parts of syntax |
| 35 |
// - of applying a template to a package it imports: |
| 36 |
// the replacement syntax should use unqualified names for its objects. |
| 37 |
|
| 38 |
var ( |
| 39 |
updateFlag = flag.Bool("update", false, "update the golden files") |
| 40 |
verboseFlag = flag.Bool("verbose", false, "show matcher information") |
| 41 |
) |
| 42 |
|
| 43 |
func Test(t *testing.T) { |
| 44 |
testenv.NeedsGoPackages(t) |
| 45 |
|
| 46 |
switch runtime.GOOS { |
| 47 |
case "windows": |
| 48 |
t.Skipf("skipping test on %q (no /usr/bin/diff)", runtime.GOOS) |
| 49 |
} |
| 50 |
|
| 51 |
// Each txtar defines a package example.com/template and zero |
| 52 |
// or more input packages example.com/in/... on which to apply |
| 53 |
// it. The outputs are compared with the corresponding files |
| 54 |
// in example.com/out/... |
| 55 |
for _, filename := range []string{ |
| 56 |
"testdata/a.txtar", |
| 57 |
"testdata/b.txtar", |
| 58 |
"testdata/c.txtar", |
| 59 |
"testdata/d.txtar", |
| 60 |
"testdata/e.txtar", |
| 61 |
"testdata/f.txtar", |
| 62 |
"testdata/g.txtar", |
| 63 |
"testdata/h.txtar", |
| 64 |
"testdata/i.txtar", |
| 65 |
"testdata/j.txtar", |
| 66 |
"testdata/bad_type.txtar", |
| 67 |
"testdata/no_before.txtar", |
| 68 |
"testdata/no_after_return.txtar", |
| 69 |
"testdata/type_mismatch.txtar", |
| 70 |
"testdata/expr_type_mismatch.txtar", |
| 71 |
} { |
| 72 |
t.Run(filename, func(t *testing.T) { |
| 73 |
// Extract and load packages from test archive. |
| 74 |
dir := testfiles.ExtractTxtarFileToTmp(t, filename) |
| 75 |
cfg := packages.Config{ |
| 76 |
Mode: packages.LoadAllSyntax, |
| 77 |
Dir: dir, |
| •78 |
Env: []string{"GO111MODULE=on", "GOCACHE=/tmp"}, |
| 79 |
} |
| 80 |
pkgs, err := packages.Load(&cfg, "example.com/template", "example.com/in/...") |
| 81 |
if err != nil { |
| 82 |
t.Fatal(err) |
| 83 |
} |
| 84 |
if packages.PrintErrors(pkgs) > 0 { |
| 85 |
t.Fatal("Load: there were errors") |
| 86 |
} |
| 87 |
|
| 88 |
// Find and compile the template. |
| 89 |
var template *packages.Package |
| 90 |
var inputs []*packages.Package |
| 91 |
for _, pkg := range pkgs { |
| 92 |
if pkg.Types.Name() == "template" { |
| 93 |
template = pkg |
| 94 |
} else { |
| 95 |
inputs = append(inputs, pkg) |
| 96 |
} |
| 97 |
} |
| 98 |
if template == nil { |
| 99 |
t.Fatal("no template package") |
| 100 |
} |
| 101 |
shouldFail, _ := template.Types.Scope().Lookup("shouldFail").(*types.Const) |
| 102 |
xform, err := eg.NewTransformer(template.Fset, template.Types, template.Syntax[0], template.TypesInfo, *verboseFlag) |
| 103 |
if err != nil { |
| 104 |
if shouldFail == nil { |
| 105 |
t.Errorf("NewTransformer(%s): %s", filename, err) |
| 106 |
} else if want := constant.StringVal(shouldFail.Val()); !strings.Contains(normalizeAny(err.Error()), want) { |
| 107 |
t.Errorf("NewTransformer(%s): got error %q, want error %q", filename, err, want) |
| 108 |
} |
| 109 |
} else if shouldFail != nil { |
| 110 |
t.Errorf("NewTransformer(%s) succeeded unexpectedly; want error %q", |
| 111 |
filename, shouldFail.Val()) |
| 112 |
} |
| 113 |
|
| 114 |
// Apply template to each input package. |
| 115 |
updated := make(map[string][]byte) |
| 116 |
for _, pkg := range inputs { |
| 117 |
for _, file := range pkg.Syntax { |
| 118 |
filename, err := filepath.Rel(dir, pkg.Fset.File(file.FileStart).Name()) |
| 119 |
if err != nil { |
| 120 |
t.Fatalf("can't relativize filename: %v", err) |
| 121 |
} |
| 122 |
|
| 123 |
// Apply the transform and reformat. |
| 124 |
n := xform.Transform(pkg.TypesInfo, pkg.Types, file) |
| 125 |
if n == 0 { |
| 126 |
t.Fatalf("%s: no replacements", filename) |
| 127 |
} |
| 128 |
var got []byte |
| 129 |
{ |
| 130 |
var out bytes.Buffer |
| 131 |
format.Node(&out, pkg.Fset, file) // ignore error |
| 132 |
got = out.Bytes() |
| 133 |
} |
| 134 |
|
| 135 |
// Compare formatted output with out/<filename> |
| 136 |
// Errors here are not fatal, so we can proceed to -update. |
| 137 |
outfile := strings.Replace(filename, "in", "out", 1) |
| 138 |
updated[outfile] = got |
| 139 |
want, err := os.ReadFile(filepath.Join(dir, outfile)) |
| 140 |
if err != nil { |
| 141 |
t.Errorf("can't read output file: %v", err) |
| 142 |
} else if diff := cmp.Diff(want, got); diff != "" { |
| 143 |
t.Errorf("Unexpected output:\n%s\n\ngot %s:\n%s\n\nwant %s:\n%s", |
| 144 |
diff, |
| 145 |
filename, got, outfile, want) |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// -update: replace the .txtar. |
| 151 |
if *updateFlag { |
| 152 |
ar, err := txtar.ParseFile(filename) |
| 153 |
if err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
|
| 157 |
var new bytes.Buffer |
| 158 |
new.Write(ar.Comment) |
| 159 |
for _, file := range ar.Files { |
| 160 |
data, ok := updated[file.Name] |
| 161 |
if !ok { |
| 162 |
data = file.Data |
| 163 |
} |
| 164 |
fmt.Fprintf(&new, "-- %s --\n%s", file.Name, data) |
| 165 |
} |
| 166 |
t.Logf("Updating %s...", filename) |
| 167 |
os.Remove(filename + ".bak") // ignore error |
| 168 |
os.Rename(filename, filename+".bak") // ignore error |
| 169 |
if err := os.WriteFile(filename, new.Bytes(), 0666); err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
} |
| 173 |
}) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// normalizeAny replaces occurrences of interface{} with any, for consistent |
| 178 |
// output. |
| 179 |
func normalizeAny(s string) string { |
| 180 |
return strings.ReplaceAll(s, "interface{}", "any") |
| 181 |
} |