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.
5 // No testdata on Android.
21 "golang.org/x/tools/internal/testenv"
25 // - test introduction of renaming imports.
26 // - test induced failures of rewriteFile.
28 // Guide to the test packages:
30 // new.com/one -- canonical name for old.com/one
31 // old.com/one -- non-canonical; has import comment "new.com/one"
32 // old.com/bad -- has a parse error
34 // fruit.io/banana } orange -> pear -> banana -> titanic.biz/bar
36 // titanic.biz/bar -- domain is sinking; package has jumped ship to new.com/bar
37 // titanic.biz/foo -- domain is sinking but package has no import comment yet
39 var gopath = filepath.Join(cwd, "testdata")
42 if err := os.Setenv("GOPATH", gopath); err != nil {
46 // This test currently requires GOPATH mode.
47 // Explicitly disabling module mode should suffix, but
48 // we'll also turn off GOPROXY just for good measure.
49 if err := os.Setenv("GO111MODULE", "off"); err != nil {
52 if err := os.Setenv("GOPROXY", "off"); err != nil {
57 func TestFixImports(t *testing.T) {
58 testenv.NeedsTool(t, "go")
62 *badDomains = "code.google.com"
66 for i, test := range []struct {
67 packages []string // packages to rewrite, "go list" syntax
68 badDomains string // -baddomains flag
69 replaceFlag string // -replace flag
72 wantRewrite map[string]string
76 packages: []string{"all"},
77 badDomains: "code.google.com",
80 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
82 fixed: old.com/one -> new.com/one
83 fixed: titanic.biz/bar -> new.com/bar
85 wantRewrite: map[string]string{
86 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
95 // #1. No packages needed rewriting.
97 packages: []string{"titanic.biz/...", "old.com/...", "new.com/..."},
98 badDomains: "code.google.com",
101 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
104 // #2. Some packages without import comments matched bad domains.
106 packages: []string{"all"},
107 badDomains: "titanic.biz",
110 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
112 testdata/src/fruit.io/banana/banana.go:6: import "titanic.biz/foo"
113 fixed: old.com/one -> new.com/one
114 fixed: titanic.biz/bar -> new.com/bar
115 ERROR: titanic.biz/foo has no import comment
116 imported directly by:
118 imported indirectly by:
121 wantRewrite: map[string]string{
122 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
131 // #3. The -replace flag lets user supply missing import comments.
133 packages: []string{"all"},
134 replaceFlag: "titanic.biz/foo=new.com/foo",
137 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
139 fixed: old.com/one -> new.com/one
140 fixed: titanic.biz/bar -> new.com/bar
141 fixed: titanic.biz/foo -> new.com/foo
143 wantRewrite: map[string]string{
144 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
153 // #4. The -replace flag supports wildcards.
154 // An explicit import comment takes precedence.
156 packages: []string{"all"},
157 replaceFlag: "titanic.biz/...=new.com/...",
160 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
162 fixed: old.com/one -> new.com/one
163 fixed: titanic.biz/bar -> new.com/bar
164 fixed: titanic.biz/foo -> new.com/foo
166 wantRewrite: map[string]string{
167 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
176 // #5. The -replace flag trumps -baddomains.
178 packages: []string{"all"},
179 badDomains: "titanic.biz",
180 replaceFlag: "titanic.biz/foo=new.com/foo",
183 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
185 fixed: old.com/one -> new.com/one
186 fixed: titanic.biz/bar -> new.com/bar
187 fixed: titanic.biz/foo -> new.com/foo
189 wantRewrite: map[string]string{
190 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
200 *badDomains = test.badDomains
201 *replaceFlag = test.replaceFlag
203 stderr = new(bytes.Buffer)
204 gotRewrite := make(map[string]string)
205 writeFile = func(filename string, content []byte, mode os.FileMode) error {
206 filename = strings.Replace(filename, gopath, "$GOPATH", 1)
207 filename = filepath.ToSlash(filename)
208 gotRewrite[filename] = string(bytes.TrimSpace(content))
212 if runtime.GOOS == "windows" {
213 test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/old.com/bad/bad.go`, `testdata\src\old.com\bad\bad.go`, -1)
214 test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/fruit.io/banana/banana.go`, `testdata\src\fruit.io\banana\banana.go`, -1)
216 test.wantStderr = strings.TrimSpace(test.wantStderr)
218 // Check status code.
219 if fiximports(test.packages...) != test.wantOK {
220 t.Errorf("#%d. fiximports() = %t", i, !test.wantOK)
223 // Compare stderr output.
224 if got := strings.TrimSpace(stderr.(*bytes.Buffer).String()); got != test.wantStderr {
225 if strings.Contains(got, "vendor/golang_org/x/text/unicode/norm") {
226 t.Skip("skipping known-broken test; see golang.org/issue/17417")
228 t.Errorf("#%d. stderr: got <<\n%s\n>>, want <<\n%s\n>>",
229 i, got, test.wantStderr)
233 for k, v := range gotRewrite {
234 if test.wantRewrite[k] != v {
235 t.Errorf("#%d. rewrite[%s] = <<%s>>, want <<%s>>",
236 i, k, v, test.wantRewrite[k])
238 delete(test.wantRewrite, k)
240 for k, v := range test.wantRewrite {
241 t.Errorf("#%d. rewrite[%s] missing, want <<%s>>", i, k, v)
246 // TestDryRun tests that the -n flag suppresses calls to writeFile.
247 func TestDryRun(t *testing.T) {
248 testenv.NeedsTool(t, "go")
251 defer func() { *dryrun = false }() // restore
252 stderr = new(bytes.Buffer)
253 writeFile = func(filename string, content []byte, mode os.FileMode) error {
254 t.Fatalf("writeFile(%s) called in dryrun mode", filename)
258 if !fiximports("all") {
259 t.Fatalf("fiximports failed: %s", stderr)