| 1 | // Copyright 2019 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 | package imports_test |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/google/go-cmp/cmp" |
| 15 | "golang.org/x/tools/internal/imports" |
| 16 | "golang.org/x/tools/internal/modindex" |
| 17 | ) |
| 18 | |
| 19 | // There are two cached packages, both resolving foo.Foo, |
| 20 | // but only one resolving foo.Bar |
| 21 | var ( |
| 22 | foo = tpkg{ |
| 23 | repo: "foo.com", |
| 24 | dir: "[email protected]", |
| 25 | syms: []string{"Foo"}, |
| 26 | } |
| 27 | foobar = tpkg{ |
| 28 | repo: "bar.com", |
| 29 | dir: "[email protected]", |
| 30 | syms: []string{"Foo", "Bar"}, |
| 31 | } |
| 32 | |
| 33 | fx = `package main |
| 34 | var _ = foo.Foo |
| 35 | var _ = foo.Bar |
| 36 | ` |
| 37 | ) |
| 38 | |
| 39 | type tpkg struct { |
| 40 | // all packages are named foo |
| 41 | repo string // e.g. foo.com |
| 42 | dir string // e.g., [email protected] |
| 43 | syms []string // exported syms |
| 44 | } |
| 45 | |
| 46 | func newpkgs(cachedir string, pks ...*tpkg) error { |
| 47 | for _, p := range pks { |
| 48 | fname := filepath.Join(cachedir, p.repo, p.dir, "foo.go") |
| 49 | if err := os.MkdirAll(filepath.Dir(fname), 0755); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | fd, err := os.Create(fname) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | fmt.Fprintf(fd, "package foo\n") |
| 57 | for _, s := range p.syms { |
| 58 | fmt.Fprintf(fd, "func %s() {}\n", s) |
| 59 | } |
| 60 | if err := fd.Close(); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func TestSource(t *testing.T) { |
| 68 | dirs := testDirs(t) |
| 69 | if err := newpkgs(dirs.cachedir, &foo, &foobar); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | source := imports.NewIndexSource(dirs.cachedir) |
| 73 | ctx := context.Background() |
| 74 | fixes, err := imports.FixImports(ctx, "tfile.go", []byte(fx), "unused", nil, source) |
| 75 | if err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | opts := imports.Options{} |
| 79 | // ApplyFixes needs a non-nil opts |
| 80 | got, err := imports.ApplyFixes(fixes, "tfile.go", []byte(fx), &opts, 0) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | |
| 85 | fxwant := "package main\n\nimport \"bar.com/foo\"\n\nvar _ = foo.Foo\nvar _ = foo.Bar\n" |
| 86 | if diff := cmp.Diff(string(got), fxwant); diff != "" { |
| 87 | t.Errorf("FixImports got\n%q, wanted\n%q\ndiff is\n%s", string(got), fxwant, diff) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | type dirs struct { |
| 92 | tmpdir string |
| 93 | cachedir string |
| 94 | rootdir string // goroot if we need it, which we don't |
| 95 | } |
| 96 | |
| 97 | func testDirs(t *testing.T) dirs { |
| 98 | t.Helper() |
| 99 | dir := t.TempDir() |
| 100 | modindex.IndexDir = dir |
| 101 | x := dirs{ |
| 102 | tmpdir: dir, |
| 103 | cachedir: filepath.Join(dir, "pkg", "mod"), |
| 104 | rootdir: filepath.Join(dir, "root"), |
| 105 | } |
| 106 | if err := os.MkdirAll(x.cachedir, 0755); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | if err := os.MkdirAll(x.rootdir, 0755); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | return x |
| 113 | } |