| 1 | // Copyright 2015 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 mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/ |
| 10 | |
| 11 | import ( |
| 12 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 13 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 14 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 15 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 16 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 17 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 18 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 19 | |
| 20 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 21 | ) |
| 22 | |
| 23 | // TODO(adonovan): |
| 24 | // - test introduction of renaming imports. |
| 25 | // - test induced failures of rewriteFile. |
| 26 | |
| 27 | // Guide to the test packages: |
| 28 | // |
| 29 | // new.com/one -- canonical name for old.com/one |
| 30 | // old.com/one -- non-canonical; has import comment "new.com/one" |
| 31 | // old.com/bad -- has a parse error |
| 32 | // fruit.io/orange \ |
| 33 | // fruit.io/banana } orange -> pear -> banana -> titanic.biz/bar |
| 34 | // fruit.io/pear / |
| 35 | // titanic.biz/bar -- domain is sinking; package has jumped ship to new.com/bar |
| 36 | // titanic.biz/foo -- domain is sinking but package has no import comment yet |
| 37 | |
| 38 | var gopathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/gopath. = 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().(cwdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/cwd., "testdata") |
| 39 | |
| 40 | func initscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/init().() { |
| 41 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().("GOPATH", gopathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/gopath.); err != nil { |
| 42 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalscip-go gomod github.com/golang/go/src go1.25.0 log/Fatal().(err) |
| 43 | } |
| 44 | |
| 45 | // This test currently requires GOPATH mode. |
| 46 | // Explicitly disabling module mode should suffix, but |
| 47 | // we'll also turn off GOPROXY just for good measure. |
| 48 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().("GO111MODULE", "off"); err != nil { |
| 49 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalscip-go gomod github.com/golang/go/src go1.25.0 log/Fatal().(err) |
| 50 | } |
| 51 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().("GOPROXY", "off"); err != nil { |
| 52 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalscip-go gomod github.com/golang/go/src go1.25.0 log/Fatal().(err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestFixImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/TestFixImports().(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#) { |
| 57 | if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("GO_BUILDER_NAME") == "plan9-arm" { |
| 58 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("skipping test that times out on plan9-arm; see https://go.dev/issue/50775") |
| 59 | } |
| 60 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go") |
| 61 | |
| 62 | defer func() { |
| 63 | stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr. = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr. |
| 64 | *badDomainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/badDomains. = "code.google.com" |
| 65 | *replaceFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/replaceFlag. = "" |
| 66 | }() |
| 67 | |
| 68 | for i, test := range []struct { |
| 69 | packages []string // packages to rewrite, "go list" syntax |
| 70 | badDomains string // -baddomains flag |
| 71 | replaceFlag string // -replace flag |
| 72 | wantOK bool |
| 73 | wantStderr string |
| 74 | wantRewrite map[string]string |
| 75 | }{ |
| 76 | // #0. No errors. |
| 77 | { |
| 78 | packages: []string{"all"}, |
| 79 | badDomains: "code.google.com", |
| 80 | wantOK: true, |
| 81 | wantStderr: ` |
| 82 | testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF' |
| 83 | fruit.io/banana |
| 84 | fixed: old.com/one -> new.com/one |
| 85 | fixed: titanic.biz/bar -> new.com/bar |
| 86 | `, |
| 87 | wantRewrite: map[string]string{ |
| 88 | "$GOPATH/src/fruit.io/banana/banana.go": `package banana |
| 89 | |
| 90 | import ( |
| 91 | _ "new.com/bar" |
| 92 | _ "new.com/one" |
| 93 | _ "titanic.biz/foo" |
| 94 | )`, |
| 95 | }, |
| 96 | }, |
| 97 | // #1. No packages needed rewriting. |
| 98 | { |
| 99 | packages: []string{"titanic.biz/...", "old.com/...", "new.com/..."}, |
| 100 | badDomains: "code.google.com", |
| 101 | wantOK: true, |
| 102 | wantStderr: ` |
| 103 | testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF' |
| 104 | `, |
| 105 | }, |
| 106 | // #2. Some packages without import comments matched bad domains. |
| 107 | { |
| 108 | packages: []string{"all"}, |
| 109 | badDomains: "titanic.biz", |
| 110 | wantOK: false, |
| 111 | wantStderr: ` |
| 112 | testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF' |
| 113 | fruit.io/banana |
| 114 | testdata/src/fruit.io/banana/banana.go:6: import "titanic.biz/foo" |
| 115 | fixed: old.com/one -> new.com/one |
| 116 | fixed: titanic.biz/bar -> new.com/bar |
| 117 | ERROR: titanic.biz/foo has no import comment |
| 118 | imported directly by: |
| 119 | fruit.io/pear |
| 120 | imported indirectly by: |
| 121 | fruit.io/orange |
| 122 | `, |
| 123 | wantRewrite: map[string]string{ |
| 124 | "$GOPATH/src/fruit.io/banana/banana.go": `package banana |
| 125 | |
| 126 | import ( |
| 127 | _ "new.com/bar" |
| 128 | _ "new.com/one" |
| 129 | _ "titanic.biz/foo" |
| 130 | )`, |
| 131 | }, |
| 132 | }, |
| 133 | // #3. The -replace flag lets user supply missing import comments. |
| 134 | { |
| 135 | packages: []string{"all"}, |
| 136 | replaceFlag: "titanic.biz/foo=new.com/foo", |
| 137 | wantOK: true, |
| 138 | wantStderr: ` |
| 139 | testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF' |
| 140 | fruit.io/banana |
| 141 | fixed: old.com/one -> new.com/one |
| 142 | fixed: titanic.biz/bar -> new.com/bar |
| 143 | fixed: titanic.biz/foo -> new.com/foo |
| 144 | `, |
| 145 | wantRewrite: map[string]string{ |
| 146 | "$GOPATH/src/fruit.io/banana/banana.go": `package banana |
| 147 | |
| 148 | import ( |
| 149 | _ "new.com/bar" |
| 150 | _ "new.com/foo" |
| 151 | _ "new.com/one" |
| 152 | )`, |
| 153 | }, |
| 154 | }, |
| 155 | // #4. The -replace flag supports wildcards. |
| 156 | // An explicit import comment takes precedence. |
| 157 | { |
| 158 | packages: []string{"all"}, |
| 159 | replaceFlag: "titanic.biz/...=new.com/...", |
| 160 | wantOK: true, |
| 161 | wantStderr: ` |
| 162 | testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF' |
| 163 | fruit.io/banana |
| 164 | fixed: old.com/one -> new.com/one |
| 165 | fixed: titanic.biz/bar -> new.com/bar |
| 166 | fixed: titanic.biz/foo -> new.com/foo |
| 167 | `, |
| 168 | wantRewrite: map[string]string{ |
| 169 | "$GOPATH/src/fruit.io/banana/banana.go": `package banana |
| 170 | |
| 171 | import ( |
| 172 | _ "new.com/bar" |
| 173 | _ "new.com/foo" |
| 174 | _ "new.com/one" |
| 175 | )`, |
| 176 | }, |
| 177 | }, |
| 178 | // #5. The -replace flag trumps -baddomains. |
| 179 | { |
| 180 | packages: []string{"all"}, |
| 181 | badDomains: "titanic.biz", |
| 182 | replaceFlag: "titanic.biz/foo=new.com/foo", |
| 183 | wantOK: true, |
| 184 | wantStderr: ` |
| 185 | testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF' |
| 186 | fruit.io/banana |
| 187 | fixed: old.com/one -> new.com/one |
| 188 | fixed: titanic.biz/bar -> new.com/bar |
| 189 | fixed: titanic.biz/foo -> new.com/foo |
| 190 | `, |
| 191 | wantRewrite: map[string]string{ |
| 192 | "$GOPATH/src/fruit.io/banana/banana.go": `package banana |
| 193 | |
| 194 | import ( |
| 195 | _ "new.com/bar" |
| 196 | _ "new.com/foo" |
| 197 | _ "new.com/one" |
| 198 | )`, |
| 199 | }, |
| 200 | }, |
| 201 | } { |
| 202 | *badDomainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/badDomains. = test.badDomains |
| 203 | *replaceFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/replaceFlag. = test.replaceFlag |
| 204 | |
| 205 | stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr. = 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#) |
| 206 | gotRewrite := make(map[string]string) |
| 207 | writeFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/writeFile. = func(filename string, content []byte, mode osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileModescip-go gomod github.com/golang/go/src go1.25.0 os/FileMode#) error { |
| 208 | filename = 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, gopathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/gopath., "$GOPATH", 1) |
| 209 | filename = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(filename) |
| 210 | gotRewrite[filename] = string(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 bytes/TrimSpace().(content)) |
| 211 | return nil |
| 212 | } |
| 213 | |
| 214 | if 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. == "windows" { |
| 215 | test.wantStderr = 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().(test.wantStderr, `testdata/src/old.com/bad/bad.go`, `testdata\src\old.com\bad\bad.go`, -1) |
| 216 | test.wantStderr = 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().(test.wantStderr, `testdata/src/fruit.io/banana/banana.go`, `testdata\src\fruit.io\banana\banana.go`, -1) |
| 217 | } |
| 218 | test.wantStderr = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(test.wantStderr) |
| 219 | |
| 220 | // Check status code. |
| 221 | if fiximportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/fiximports().(test.packages...) != test.wantOK { |
| 222 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("#%d. fiximports() = %t", i, !test.wantOK) |
| 223 | } |
| 224 | |
| 225 | // Compare stderr output. |
| 226 | if got := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr..(*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#).Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().()); got != test.wantStderr { |
| 227 | if 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().(got, "vendor/golang_org/x/text/unicode/norm") { |
| 228 | t.Skipscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skip().("skipping known-broken test; see golang.org/issue/17417") |
| 229 | } |
| 230 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("#%d. stderr: got <<\n%s\n>>, want <<\n%s\n>>", |
| 231 | i, got, test.wantStderr) |
| 232 | } |
| 233 | |
| 234 | // Compare rewrites. |
| 235 | for k, v := range gotRewrite { |
| 236 | if test.wantRewrite[k] != v { |
| 237 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("#%d. rewrite[%s] = <<%s>>, want <<%s>>", |
| 238 | i, k, v, test.wantRewrite[k]) |
| 239 | } |
| 240 | delete(test.wantRewrite, k) |
| 241 | } |
| 242 | for k, v := range test.wantRewrite { |
| 243 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("#%d. rewrite[%s] missing, want <<%s>>", i, k, v) |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // TestDryRun tests that the -n flag suppresses calls to writeFile. |
| 249 | func TestDryRunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/TestDryRun().(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#) { |
| 250 | if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("GO_BUILDER_NAME") == "plan9-arm" { |
| 251 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("skipping test that times out on plan9-arm; see https://go.dev/issue/50775") |
| 252 | } |
| 253 | testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.NeedsToolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/NeedsTool().(t, "go") |
| 254 | |
| 255 | *dryrunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/dryrun. = true |
| 256 | defer func() { *dryrunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/dryrun. = false }() // restore |
| 257 | stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr. = 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#) |
| 258 | writeFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/writeFile. = func(filename string, content []byte, mode osscip-go gomod github.com/golang/go/src go1.25.0 os/.FileModescip-go gomod github.com/golang/go/src go1.25.0 os/FileMode#) error { |
| 259 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("writeFile(%s) called in dryrun mode", filename) |
| 260 | return nil |
| 261 | } |
| 262 | |
| 263 | if !fiximportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/fiximports().("all") { |
| 264 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("fiximports failed: %s", stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr.) |
| 265 | } |
| 266 | } |