| 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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 11 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 12 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 13 | |
| 14 | "github.com/google/go-cmp/cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/" |
| 15 | "golang.org/x/tools/internal/importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/" |
| 16 | "golang.org/x/tools/internal/modindexscip-go gomod golang.org/x/tools . `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 | fooscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/foo. = tpkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#{ |
| 23 | reposcip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#repo.: "foo.com", |
| 24 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#dir.: "[email protected]", |
| 25 | symsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#syms.: []string{"Foo"}, |
| 26 | } |
| 27 | foobarscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/foobar. = tpkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#{ |
| 28 | reposcip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#repo.: "bar.com", |
| 29 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#dir.: "[email protected]", |
| 30 | symsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#syms.: []string{"Foo", "Bar"}, |
| 31 | } |
| 32 | |
| 33 | fxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/fx. = `package main |
| 34 | var _ = foo.Foo |
| 35 | var _ = foo.Bar |
| 36 | ` |
| 37 | ) |
| 38 | |
| 39 | type tpkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg# struct { |
| 40 | // all packages are named foo |
| 41 | reposcip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#repo. string // e.g. foo.com |
| 42 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#dir. string // e.g., [email protected] |
| 43 | symsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#syms. []string // exported syms |
| 44 | } |
| 45 | |
| 46 | func newpkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/newpkgs().(cachedir string, pks ...*tpkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#) error { |
| 47 | for _, p := range pks { |
| 48 | fname := 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().(cachedir, p.reposcip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#repo., p.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#dir., "foo.go") |
| 49 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirAll().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(fname), 0755); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | fd, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Createscip-go gomod github.com/golang/go/src go1.25.0 os/Create().(fname) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(fd, "package foo\n") |
| 57 | for _, s := range p.symsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/tpkg#syms. { |
| 58 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(fd, "func %s() {}\n", s) |
| 59 | } |
| 60 | if err := fd.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().(); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func TestSourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/TestSource().(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#) { |
| 68 | dirs := testDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/testDirs().(t) |
| 69 | if err := newpkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/newpkgs().(dirs.cachedirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#cachedir., &fooscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/foo., &foobarscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/foobar.); err != nil { |
| 70 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 71 | } |
| 72 | source := importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/.NewIndexSourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/NewIndexSource().(dirs.cachedirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#cachedir.) |
| 73 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().() |
| 74 | fixes, err := importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/.FixImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/FixImports().(ctx, "tfile.go", []byte(fxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/fx.), "unused", nil, source) |
| 75 | if err != nil { |
| 76 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 77 | } |
| 78 | opts := importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/.Optionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Options#{} |
| 79 | // ApplyFixes needs a non-nil opts |
| 80 | got, err := importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/.ApplyFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ApplyFixes().(fixes, "tfile.go", []byte(fxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/fx.), &opts, 0) |
| 81 | if err != nil { |
| 82 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 83 | } |
| 84 | |
| 85 | fxwant := "package main\n\nimport \"bar.com/foo\"\n\nvar _ = foo.Foo\nvar _ = foo.Bar\n" |
| 86 | if diff := cmpscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/.Diffscip-go gomod github.com/google/go-cmp v0.6.0 `github.com/google/go-cmp/cmp`/Diff().(string(got), fxwant); diff != "" { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("FixImports got\n%q, wanted\n%q\ndiff is\n%s", string(got), fxwant, diff) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | type dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs# struct { |
| 92 | tmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#tmpdir. string |
| 93 | cachedirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#cachedir. string |
| 94 | rootdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#rootdir. string // goroot if we need it, which we don't |
| 95 | } |
| 96 | |
| 97 | func testDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/testDirs().(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#) dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs# { |
| 98 | t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().() |
| 99 | dir := t.TempDirscip-go gomod github.com/golang/go/src go1.25.0 testing/common#TempDir().() |
| 100 | modindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/.IndexDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/IndexDir. = dir |
| 101 | x := dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#{ |
| 102 | tmpdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#tmpdir.: dir, |
| 103 | cachedirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#cachedir.: 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().(dir, "pkg", "mod"), |
| 104 | rootdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#rootdir.: 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().(dir, "root"), |
| 105 | } |
| 106 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirAll().(x.cachedirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#cachedir., 0755); err != nil { |
| 107 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 108 | } |
| 109 | if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirAll().(x.rootdirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports_test`/dirs#rootdir., 0755); err != nil { |
| 110 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 111 | } |
| 112 | return x |
| 113 | } |