| 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 | } |
| 79 | 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/...") |
| 80 | if err != nil { |
| 81 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 82 | } |
| 83 | 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 { |
| 84 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("Load: there were errors") |
| 85 | } |
| 86 | |
| 87 | // Find and compile the template. |
| 88 | 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# |
| 89 | 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# |
| 90 | for _, pkg := range pkgs { |
| 91 | 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" { |
| 92 | template = pkg |
| 93 | } else { |
| 94 | inputs = append(inputs, pkg) |
| 95 | } |
| 96 | } |
| 97 | if template == nil { |
| 98 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("no template package") |
| 99 | } |
| 100 | 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#) |
| 101 | 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.) |
| 102 | if err != nil { |
| 103 | if shouldFail == nil { |
| 104 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("NewTransformer(%s): %s", filename, err) |
| 105 | } 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) { |
| 106 | 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) |
| 107 | } |
| 108 | } else if shouldFail != nil { |
| 109 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("NewTransformer(%s) succeeded unexpectedly; want error %q", |
| 110 | filename, shouldFail.Valscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Const#Val().()) |
| 111 | } |
| 112 | |
| 113 | // Apply template to each input package. |
| 114 | updated := make(map[string][]byte) |
| 115 | for _, pkg := range inputs { |
| 116 | for _, file := range pkg.Syntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Syntax. { |
| 117 | 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().()) |
| 118 | if err != nil { |
| 119 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("can't relativize filename: %v", err) |
| 120 | } |
| 121 | |
| 122 | // Apply the transform and reformat. |
| 123 | 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) |
| 124 | if n == 0 { |
| 125 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("%s: no replacements", filename) |
| 126 | } |
| 127 | var got []byte |
| 128 | { |
| 129 | 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# |
| 130 | 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 |
| 131 | got = out.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().() |
| 132 | } |
| 133 | |
| 134 | // Compare formatted output with out/<filename> |
| 135 | // Errors here are not fatal, so we can proceed to -update. |
| 136 | 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) |
| 137 | updated[outfile] = got |
| 138 | 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)) |
| 139 | if err != nil { |
| 140 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("can't read output file: %v", err) |
| 141 | } 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 != "" { |
| 142 | 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", |
| 143 | diff, |
| 144 | filename, got, outfile, want) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // -update: replace the .txtar. |
| 150 | if *updateFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/updateFlag. { |
| 151 | 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) |
| 152 | if err != nil { |
| 153 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 154 | } |
| 155 | |
| 156 | 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# |
| 157 | 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.) |
| 158 | for _, file := range ar.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/Archive#Files. { |
| 159 | data, ok := updated[file.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Name.] |
| 160 | if !ok { |
| 161 | data = file.Datascip-go gomod golang.org/x/tools . `golang.org/x/tools/txtar`/File#Data. |
| 162 | } |
| 163 | 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) |
| 164 | } |
| 165 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Updating %s...", filename) |
| 166 | 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 |
| 167 | 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 |
| 168 | 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 { |
| 169 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 170 | } |
| 171 | } |
| 172 | }) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // normalizeAny replaces occurrences of interface{} with any, for consistent |
| 177 | // output. |
| 178 | func normalizeAnyscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/eg_test`/normalizeAny().(s string) string { |
| 179 | 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") |
| 180 | } |