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.
20 "golang.org/x/tools/internal/testenv"
24 // - test introduction of renaming imports.
25 // - test induced failures of rewriteFile.
27 // Guide to the test packages:
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
33 // fruit.io/banana } orange -> pear -> banana -> titanic.biz/bar
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
38 var gopath = filepath.Join(cwd, "testdata")
41 if err := os.Setenv("GOPATH", gopath); err != nil {
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 {
51 if err := os.Setenv("GOPROXY", "off"); err != nil {
56 func TestFixImports(t *testing.T) {
57 testenv.NeedsTool(t, "go")
61 *badDomains = "code.google.com"
65 for i, test := range []struct {
66 packages []string // packages to rewrite, "go list" syntax
67 badDomains string // -baddomains flag
68 replaceFlag string // -replace flag
71 wantRewrite map[string]string
75 packages: []string{"all"},
76 badDomains: "code.google.com",
79 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
81 fixed: old.com/one -> new.com/one
82 fixed: titanic.biz/bar -> new.com/bar
84 wantRewrite: map[string]string{
85 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
94 // #1. No packages needed rewriting.
96 packages: []string{"titanic.biz/...", "old.com/...", "new.com/..."},
97 badDomains: "code.google.com",
100 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
103 // #2. Some packages without import comments matched bad domains.
105 packages: []string{"all"},
106 badDomains: "titanic.biz",
109 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
111 testdata/src/fruit.io/banana/banana.go:6: import "titanic.biz/foo"
112 fixed: old.com/one -> new.com/one
113 fixed: titanic.biz/bar -> new.com/bar
114 ERROR: titanic.biz/foo has no import comment
115 imported directly by:
117 imported indirectly by:
120 wantRewrite: map[string]string{
121 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
130 // #3. The -replace flag lets user supply missing import comments.
132 packages: []string{"all"},
133 replaceFlag: "titanic.biz/foo=new.com/foo",
136 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
138 fixed: old.com/one -> new.com/one
139 fixed: titanic.biz/bar -> new.com/bar
140 fixed: titanic.biz/foo -> new.com/foo
142 wantRewrite: map[string]string{
143 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
152 // #4. The -replace flag supports wildcards.
153 // An explicit import comment takes precedence.
155 packages: []string{"all"},
156 replaceFlag: "titanic.biz/...=new.com/...",
159 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
161 fixed: old.com/one -> new.com/one
162 fixed: titanic.biz/bar -> new.com/bar
163 fixed: titanic.biz/foo -> new.com/foo
165 wantRewrite: map[string]string{
166 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
175 // #5. The -replace flag trumps -baddomains.
177 packages: []string{"all"},
178 badDomains: "titanic.biz",
179 replaceFlag: "titanic.biz/foo=new.com/foo",
182 testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
184 fixed: old.com/one -> new.com/one
185 fixed: titanic.biz/bar -> new.com/bar
186 fixed: titanic.biz/foo -> new.com/foo
188 wantRewrite: map[string]string{
189 "$GOPATH/src/fruit.io/banana/banana.go": `package banana
199 *badDomains = test.badDomains
200 *replaceFlag = test.replaceFlag
202 stderr = new(bytes.Buffer)
203 gotRewrite := make(map[string]string)
204 writeFile = func(filename string, content []byte, mode os.FileMode) error {
205 filename = strings.Replace(filename, gopath, "$GOPATH", 1)
206 filename = filepath.ToSlash(filename)
207 gotRewrite[filename] = string(bytes.TrimSpace(content))
211 if runtime.GOOS == "windows" {
212 test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/old.com/bad/bad.go`, `testdata\src\old.com\bad\bad.go`, -1)
213 test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/fruit.io/banana/banana.go`, `testdata\src\fruit.io\banana\banana.go`, -1)
215 test.wantStderr = strings.TrimSpace(test.wantStderr)
217 // Check status code.
218 if fiximports(test.packages...) != test.wantOK {
219 t.Errorf("#%d. fiximports() = %t", i, !test.wantOK)
222 // Compare stderr output.
223 if got := strings.TrimSpace(stderr.(*bytes.Buffer).String()); got != test.wantStderr {
224 if strings.Contains(got, "vendor/golang_org/x/text/unicode/norm") {
225 t.Skip("skipping known-broken test; see golang.org/issue/17417")
227 t.Errorf("#%d. stderr: got <<\n%s\n>>, want <<\n%s\n>>",
228 i, got, test.wantStderr)
232 for k, v := range gotRewrite {
233 if test.wantRewrite[k] != v {
234 t.Errorf("#%d. rewrite[%s] = <<%s>>, want <<%s>>",
235 i, k, v, test.wantRewrite[k])
237 delete(test.wantRewrite, k)
239 for k, v := range test.wantRewrite {
240 t.Errorf("#%d. rewrite[%s] missing, want <<%s>>", i, k, v)
245 // TestDryRun tests that the -n flag suppresses calls to writeFile.
246 func TestDryRun(t *testing.T) {
247 testenv.NeedsTool(t, "go")
250 defer func() { *dryrun = false }() // restore
251 stderr = new(bytes.Buffer)
252 writeFile = func(filename string, content []byte, mode os.FileMode) error {
253 t.Fatalf("writeFile(%s) called in dryrun mode", filename)
257 if !fiximports("all") {
258 t.Fatalf("fiximports failed: %s", stderr)