| 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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/ |
| 10 |
|
| 11 |
import ( |
| 12 |
"bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 13 |
"flagscip-go gomod github.com/golang/go/src go1.25.0 flag/" |
| 14 |
"fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 15 |
"go/constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/" |
| 16 |
"go/formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/" |
| 17 |
"go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 18 |
"osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 19 |
"path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 20 |
"runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 21 |
"stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 22 |
"testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/" |
| 25 |
"golang.org/x/tools/go/packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/" |
| 26 |
"golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 27 |
"golang.org/x/tools/internal/testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/" |
| 28 |
"golang.org/x/tools/refactor/egscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg`/" |
| 29 |
"golang.org/x/tools/txtarscip-go gomod golang.org/x/tools . `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 |
updateFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/updateFlag. = flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Boolscip-go gomod github.com/golang/go/src go1.25.0 flag/Bool().("update", false, "update the golden files") |
| 40 |
verboseFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/verboseFlag. = flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Boolscip-go gomod github.com/golang/go/src go1.25.0 flag/Bool().("verbose", false, "show matcher information") |
| 41 |
) |
| 42 |
|
| 43 |
func Testscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/Test().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 44 |
testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsGoPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsGoPackages().(t) |
| 45 |
|
| 46 |
switch runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 runtime/GOOS. { |
| 47 |
case "windows": |
| 48 |
t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("skipping test on %q (no /usr/bin/diff)", runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.25.0 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.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(filename, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 73 |
// Extract and load packages from test archive. |
| 74 |
dir := testfilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/.ExtractTxtarFileToTmpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testfiles`/ExtractTxtarFileToTmp().(t, filename) |
| 75 |
cfg := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#{ |
| 76 |
Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Mode.: packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.LoadAllSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/LoadAllSyntax., |
| 77 |
Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir.: dir, |
| •78 |
Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env.: []string{"GO111MODULE=on", "GOCACHE=/tmp"}, |
| 79 |
} |
| 80 |
pkgs, err := packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Load().(&cfg, "example.com/template", "example.com/in/...") |
| 81 |
if err != nil { |
| 82 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 83 |
} |
| 84 |
if packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.PrintErrorsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/PrintErrors().(pkgs) > 0 { |
| 85 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("Load: there were errors") |
| 86 |
} |
| 87 |
|
| 88 |
// Find and compile the template. |
| 89 |
var template *packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package# |
| 90 |
var inputs []*packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package# |
| 91 |
for _, pkg := range pkgs { |
| 92 |
if pkg.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().() == "template" { |
| 93 |
template = pkg |
| 94 |
} else { |
| 95 |
inputs = append(inputs, pkg) |
| 96 |
} |
| 97 |
} |
| 98 |
if template == nil { |
| 99 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("no template package") |
| 100 |
} |
| 101 |
shouldFail, _ := template.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types..Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("shouldFail").(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Constscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Const#) |
| 102 |
xform, err := egscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg`/.NewTransformerscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg`/NewTransformer().(template.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset., template.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types., template.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax.[0], template.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#TypesInfo., *verboseFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/verboseFlag.) |
| 103 |
if err != nil { |
| 104 |
if shouldFail == nil { |
| 105 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("NewTransformer(%s): %s", filename, err) |
| 106 |
} else if want := constantscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/.StringValscip-go gomod github.com/golang/go/src go1.25.0 `go/constant`/StringVal().(shouldFail.Valscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Const#Val().()); !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(normalizeAnyscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/normalizeAny().(err.Error()), want) { |
| 107 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("NewTransformer(%s): got error %q, want error %q", filename, err, want) |
| 108 |
} |
| 109 |
} else if shouldFail != nil { |
| 110 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("NewTransformer(%s) succeeded unexpectedly; want error %q", |
| 111 |
filename, shouldFail.Valscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Const#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.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax. { |
| 118 |
filename, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(dir, pkg.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(file.FileStartscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileStart.).Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().()) |
| 119 |
if err != nil { |
| 120 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("can't relativize filename: %v", err) |
| 121 |
} |
| 122 |
|
| 123 |
// Apply the transform and reformat. |
| 124 |
n := xform.Transformscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg`/Transformer#Transform().(pkg.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#TypesInfo., pkg.Typesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Types., file) |
| 125 |
if n == 0 { |
| 126 |
t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s: no replacements", filename) |
| 127 |
} |
| 128 |
var got []byte |
| 129 |
{ |
| 130 |
var out bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 131 |
formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/format`/Node().(&out, pkg.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset., file) // ignore error |
| 132 |
got = out.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().() |
| 133 |
} |
| 134 |
|
| 135 |
// Compare formatted output with out/<filename> |
| 136 |
// Errors here are not fatal, so we can proceed to -update. |
| 137 |
outfile := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 strings/Replace().(filename, "in", "out", 1) |
| 138 |
updated[outfile] = got |
| 139 |
want, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadFilescip-go gomod github.com/golang/go/src go1.25.0 os/ReadFile().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, outfile)) |
| 140 |
if err != nil { |
| 141 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("can't read output file: %v", err) |
| 142 |
} else if diff := cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(want, got); diff != "" { |
| 143 |
t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 *updateFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/updateFlag. { |
| 152 |
ar, err := txtarscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/.ParseFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/ParseFile().(filename) |
| 153 |
if err != nil { |
| 154 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 155 |
} |
| 156 |
|
| 157 |
var new bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 158 |
new.Writescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Write().(ar.Commentscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Comment.) |
| 159 |
for _, file := range ar.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files. { |
| 160 |
data, ok := updated[file.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name.] |
| 161 |
if !ok { |
| 162 |
data = file.Datascip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Data. |
| 163 |
} |
| 164 |
fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&new, "-- %s --\n%s", file.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name., data) |
| 165 |
} |
| 166 |
t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Updating %s...", filename) |
| 167 |
osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(filename + ".bak") // ignore error |
| 168 |
osscip-go gomod github.com/golang/go/src go1.25.0 os/.Renamescip-go gomod github.com/golang/go/src go1.25.0 os/Rename().(filename, filename+".bak") // ignore error |
| 169 |
if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().(filename, new.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), 0666); err != nil { |
| 170 |
t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 171 |
} |
| 172 |
} |
| 173 |
}) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// normalizeAny replaces occurrences of interface{} with any, for consistent |
| 178 |
// output. |
| 179 |
func normalizeAnyscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/normalizeAny().(s string) string { |
| 180 |
return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(s, "interface{}", "any") |
| 181 |
} |