Debian Codegraph @

cmd/fiximports/main_test.go

raw symbols SCIP
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 main
10
11 import (
12 "bytes"
13 "log"
14 "os"
15 "path/filepath"
16 "runtime"
17 "strings"
18 "testing"
19
20 "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 gopath = filepath.Join(cwd, "testdata")
39
40 func init() {
41 if err := os.Setenv("GOPATH", gopath); err != nil {
42 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 := os.Setenv("GO111MODULE", "off"); err != nil {
49 log.Fatal(err)
50 }
51 if err := os.Setenv("GOPROXY", "off"); err != nil {
52 log.Fatal(err)
53 }
54 }
55
56 func TestFixImports(t *testing.T) {
57 if os.Getenv("GO_BUILDER_NAME") == "plan9-arm" {
58 t.Skipf("skipping test that times out on plan9-arm; see https://go.dev/issue/50775")
59 }
60 testenv.NeedsTool(t, "go")
61
62 defer func() {
63 stderr = os.Stderr
64 *badDomains = "code.google.com"
65 *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 *badDomains = test.badDomains
203 *replaceFlag = test.replaceFlag
204
205 stderr = new(bytes.Buffer)
206 gotRewrite := make(map[string]string)
207 writeFile = func(filename string, content []byte, mode os.FileMode) error {
208 filename = strings.Replace(filename, gopath, "$GOPATH", 1)
209 filename = filepath.ToSlash(filename)
210 gotRewrite[filename] = string(bytes.TrimSpace(content))
211 return nil
212 }
213
214 if runtime.GOOS == "windows" {
215 test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/old.com/bad/bad.go`, `testdata\src\old.com\bad\bad.go`, -1)
216 test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/fruit.io/banana/banana.go`, `testdata\src\fruit.io\banana\banana.go`, -1)
217 }
218 test.wantStderr = strings.TrimSpace(test.wantStderr)
219
220 // Check status code.
221 if fiximports(test.packages...) != test.wantOK {
222 t.Errorf("#%d. fiximports() = %t", i, !test.wantOK)
223 }
224
225 // Compare stderr output.
226 if got := strings.TrimSpace(stderr.(*bytes.Buffer).String()); got != test.wantStderr {
227 if strings.Contains(got, "vendor/golang_org/x/text/unicode/norm") {
228 t.Skip("skipping known-broken test; see golang.org/issue/17417")
229 }
230 t.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.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.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 TestDryRun(t *testing.T) {
250 if os.Getenv("GO_BUILDER_NAME") == "plan9-arm" {
251 t.Skipf("skipping test that times out on plan9-arm; see https://go.dev/issue/50775")
252 }
253 testenv.NeedsTool(t, "go")
254
255 *dryrun = true
256 defer func() { *dryrun = false }() // restore
257 stderr = new(bytes.Buffer)
258 writeFile = func(filename string, content []byte, mode os.FileMode) error {
259 t.Fatalf("writeFile(%s) called in dryrun mode", filename)
260 return nil
261 }
262
263 if !fiximports("all") {
264 t.Fatalf("fiximports failed: %s", stderr)
265 }
266 }