| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package workspace |
| 6 |
|
| 7 |
import ( |
| 8 |
"sort" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/google/go-cmp/cmp" |
| 13 |
"golang.org/x/tools/gopls/internal/protocol" |
| 14 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 15 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 16 |
) |
| 17 |
|
| 18 |
func TestModulesCmd(t *testing.T) { |
| 19 |
const goModView = ` |
| 20 |
-- go.mod -- |
| 21 |
module foo |
| 22 |
|
| 23 |
-- pkg/pkg.go -- |
| 24 |
package pkg |
| 25 |
func Pkg() |
| 26 |
|
| 27 |
-- bar/bar.go -- |
| 28 |
package bar |
| 29 |
func Bar() |
| 30 |
|
| 31 |
-- bar/baz/go.mod -- |
| 32 |
module baz |
| 33 |
|
| 34 |
-- bar/baz/baz.go -- |
| 35 |
package baz |
| 36 |
func Baz() |
| 37 |
` |
| 38 |
|
| 39 |
const goWorkView = ` |
| 40 |
-- go.work -- |
| 41 |
use ./foo |
| 42 |
use ./bar |
| 43 |
|
| 44 |
-- foo/go.mod -- |
| 45 |
module foo |
| 46 |
|
| 47 |
-- foo/foo.go -- |
| 48 |
package foo |
| 49 |
func Foo() |
| 50 |
|
| 51 |
-- bar/go.mod -- |
| 52 |
module bar |
| 53 |
|
| 54 |
-- bar/bar.go -- |
| 55 |
package bar |
| 56 |
func Bar() |
| 57 |
` |
| 58 |
|
| 59 |
t.Run("go.mod view", func(t *testing.T) { |
| 60 |
|
| 61 |
t.Run("unloaded", func(t *testing.T) { |
| 62 |
Run(t, goModView, func(t *testing.T, env *Env) { |
| 63 |
checkModules(t, env, env.Editor.DocumentURI(""), -1, []command.Module{ |
| 64 |
{ |
| 65 |
Path: "foo", |
| 66 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 67 |
}, |
| 68 |
}) |
| 69 |
}) |
| 70 |
}) |
| 71 |
|
| 72 |
|
| 73 |
t.Run("recurse", func(t *testing.T) { |
| 74 |
Run(t, goModView, func(t *testing.T, env *Env) { |
| 75 |
env.OpenFile("bar/baz/baz.go") |
| 76 |
checkModules(t, env, env.Editor.DocumentURI(""), -1, []command.Module{ |
| 77 |
{ |
| 78 |
Path: "baz", |
| 79 |
GoMod: env.Editor.DocumentURI("bar/baz/go.mod"), |
| 80 |
}, |
| 81 |
{ |
| 82 |
Path: "foo", |
| 83 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 84 |
}, |
| 85 |
}) |
| 86 |
}) |
| 87 |
}) |
| 88 |
|
| 89 |
|
| 90 |
t.Run("depth", func(t *testing.T) { |
| 91 |
Run(t, goModView, func(t *testing.T, env *Env) { |
| 92 |
env.OpenFile("bar/baz/baz.go") |
| 93 |
checkModules(t, env, env.Editor.DocumentURI(""), 1, []command.Module{ |
| 94 |
{ |
| 95 |
Path: "foo", |
| 96 |
GoMod: env.Editor.DocumentURI("go.mod"), |
| 97 |
}, |
| 98 |
}) |
| 99 |
}) |
| 100 |
}) |
| 101 |
|
| 102 |
|
| 103 |
t.Run("nested", func(t *testing.T) { |
| 104 |
Run(t, goModView, func(t *testing.T, env *Env) { |
| 105 |
env.OpenFile("bar/baz/baz.go") |
| 106 |
checkModules(t, env, env.Editor.DocumentURI("bar/baz"), 0, []command.Module{ |
| 107 |
{ |
| 108 |
Path: "baz", |
| 109 |
GoMod: env.Editor.DocumentURI("bar/baz/go.mod"), |
| 110 |
}, |
| 111 |
}) |
| 112 |
}) |
| 113 |
}) |
| 114 |
}) |
| 115 |
|
| 116 |
t.Run("go.work view", func(t *testing.T) { |
| 117 |
t.Run("base", func(t *testing.T) { |
| 118 |
Run(t, goWorkView, func(t *testing.T, env *Env) { |
| 119 |
checkModules(t, env, env.Editor.DocumentURI(""), 0, nil) |
| 120 |
}) |
| 121 |
}) |
| 122 |
|
| 123 |
t.Run("recursive", func(t *testing.T) { |
| 124 |
Run(t, goWorkView, func(t *testing.T, env *Env) { |
| 125 |
checkModules(t, env, env.Editor.DocumentURI(""), -1, []command.Module{ |
| 126 |
{ |
| 127 |
Path: "bar", |
| 128 |
GoMod: env.Editor.DocumentURI("bar/go.mod"), |
| 129 |
}, |
| 130 |
{ |
| 131 |
Path: "foo", |
| 132 |
GoMod: env.Editor.DocumentURI("foo/go.mod"), |
| 133 |
}, |
| 134 |
}) |
| 135 |
}) |
| 136 |
}) |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
func checkModules(t testing.TB, env *Env, dir protocol.DocumentURI, maxDepth int, want []command.Module) { |
| 141 |
t.Helper() |
| 142 |
|
| 143 |
cmd := command.NewModulesCommand("Modules", command.ModulesArgs{Dir: dir, MaxDepth: maxDepth}) |
| 144 |
var result command.ModulesResult |
| 145 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 146 |
Command: command.Modules.String(), |
| 147 |
Arguments: cmd.Arguments, |
| 148 |
}, &result) |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
sort.Slice(result.Modules, func(i, j int) bool { |
| 153 |
a, b := result.Modules[i], result.Modules[j] |
| 154 |
return strings.Compare(a.Path, b.Path) < 0 |
| 155 |
}) |
| 156 |
|
| 157 |
diff := cmp.Diff(want, result.Modules) |
| 158 |
if diff != "" { |
| 159 |
t.Errorf("Modules(%v) returned unexpected diff (-want +got):\n%s", dir, diff) |
| 160 |
} |
| 161 |
} |