| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package web_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"net/http" |
| 13 |
"net/url" |
| 14 |
"slices" |
| 15 |
"strings" |
| 16 |
"testing" |
| 17 |
|
| 18 |
"github.com/google/go-cmp/cmp" |
| 19 |
"golang.org/x/tools/gopls/internal/golang/splitpkg" |
| 20 |
"golang.org/x/tools/gopls/internal/settings" |
| 21 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
func TestSplitPackage(t *testing.T) { |
| 26 |
const files = ` |
| 27 |
-- go.mod -- |
| 28 |
module example.com |
| 29 |
|
| 30 |
-- a/a.go -- |
| 31 |
package a |
| 32 |
|
| 33 |
func a() { b1() } |
| 34 |
|
| 35 |
func b1() { b2() } |
| 36 |
func b2() { b1(); c() } |
| 37 |
|
| 38 |
// EOF |
| 39 |
-- a/b.go -- |
| 40 |
package a |
| 41 |
|
| 42 |
func c() { d() } |
| 43 |
|
| 44 |
func d() {} |
| 45 |
` |
| 46 |
Run(t, files, func(t *testing.T, env *Env) { |
| 47 |
env.OpenFile("a/a.go") |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
loc := env.RegexpSearch("a/a.go", "package") |
| 53 |
uri, page := codeActionWebPage(t, env, settings.GoSplitPackage, loc) |
| 54 |
|
| 55 |
checkMatch(t, true, page, `<h1>Split package example.com/a</h1>`) |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
jsonHTTP := func(method, path string, in, out any) { |
| 62 |
|
| 63 |
|
| 64 |
u, err := url.Parse(uri) |
| 65 |
if err != nil { |
| 66 |
t.Fatalf("parsing URL: %v", err) |
| 67 |
} |
| 68 |
u.Path = strings.ReplaceAll(u.Path, "/splitpkg", path) |
| 69 |
|
| 70 |
|
| 71 |
inJSON, err := json.Marshal(in) |
| 72 |
if err != nil { |
| 73 |
t.Fatalf("encoding input: %v", err) |
| 74 |
} |
| 75 |
t.Logf("%s: in=%s", path, inJSON) |
| 76 |
req, err := http.NewRequest(method, u.String(), bytes.NewReader(inJSON)) |
| 77 |
if err != nil { |
| 78 |
t.Fatalf("NewRequest: %v", err) |
| 79 |
} |
| 80 |
req.Header.Set("Content-Type", "application/json") |
| 81 |
resp, err := http.DefaultClient.Do(req) |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("HTTP request: %v", err) |
| 84 |
} |
| 85 |
defer resp.Body.Close() |
| 86 |
outJSON, err := io.ReadAll(resp.Body) |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("reading output: %v", err) |
| 89 |
} |
| 90 |
t.Logf("%s: out=%s", path, outJSON) |
| 91 |
if out != nil { |
| 92 |
if err := json.Unmarshal(outJSON, out); err != nil { |
| 93 |
t.Fatalf("decoding output: %v", err) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
checkFileDecls := func(want string) { |
| 101 |
var res splitpkg.ResultJSON |
| 102 |
jsonHTTP("GET", "/splitpkg-json", nil, &res) |
| 103 |
|
| 104 |
var lines []string |
| 105 |
for _, file := range res.Files { |
| 106 |
var buf strings.Builder |
| 107 |
fmt.Fprintf(&buf, "file %s:", file.Base) |
| 108 |
for _, decl := range file.Decls { |
| 109 |
for _, spec := range decl.Specs { |
| 110 |
fmt.Fprintf(&buf, " %s %s;", decl.Kind, spec.Name) |
| 111 |
} |
| 112 |
} |
| 113 |
lines = append(lines, buf.String()) |
| 114 |
} |
| 115 |
slices.Sort(lines) |
| 116 |
got := strings.Join(lines, "\n") |
| 117 |
|
| 118 |
if diff := cmp.Diff(want, got); diff != "" { |
| 119 |
t.Errorf("unexpected file decls:\ngot:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
checkEdges := func(want string) { |
| 126 |
var res splitpkg.ResultJSON |
| 127 |
jsonHTTP("GET", "/splitpkg-json", nil, &res) |
| 128 |
|
| 129 |
var lines []string |
| 130 |
for _, edge := range res.Edges { |
| 131 |
var buf strings.Builder |
| 132 |
fmt.Fprintf(&buf, "edge %s -> %s:", res.Components.Names[edge.From], res.Components.Names[edge.To]) |
| 133 |
if edge.Cyclic { |
| 134 |
buf.WriteString(" ⚠") |
| 135 |
} |
| 136 |
for _, ref := range edge.Refs { |
| 137 |
fmt.Fprintf(&buf, " %s -> %s;", ref.From, ref.To) |
| 138 |
} |
| 139 |
lines = append(lines, buf.String()) |
| 140 |
} |
| 141 |
slices.Sort(lines) |
| 142 |
got := strings.Join(lines, "\n") |
| 143 |
if diff := cmp.Diff(want, got); diff != "" { |
| 144 |
t.Errorf("unexpected component edges:\ngot:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
checkFileDecls(` |
| 150 |
file a.go: func a; func b1; func b2; |
| 151 |
file b.go: func c; func d;`[1:]) |
| 152 |
|
| 153 |
|
| 154 |
env.RegexpReplace("a/a.go", "// EOF", "func b3() {}") |
| 155 |
env.Await(env.DoneDiagnosingChanges()) |
| 156 |
checkFileDecls(` |
| 157 |
file a.go: func a; func b1; func b2; func b3; |
| 158 |
file b.go: func c; func d;`[1:]) |
| 159 |
|
| 160 |
|
| 161 |
jsonHTTP("POST", "/splitpkg-components", splitpkg.ComponentsJSON{ |
| 162 |
Names: []string{"zero", "one", "two", "three"}, |
| 163 |
Assignments: map[string]int{"a": 0, "b1": 1, "b2": 2, "c": 3, "d": 3}, |
| 164 |
}, nil) |
| 165 |
checkEdges(` |
| 166 |
edge one -> two: ⚠ b1 -> b2; |
| 167 |
edge two -> one: ⚠ b2 -> b1; |
| 168 |
edge two -> three: b2 -> c; |
| 169 |
edge zero -> one: a -> b1;`[1:]) |
| 170 |
|
| 171 |
|
| 172 |
jsonHTTP("POST", "/splitpkg-components", splitpkg.ComponentsJSON{ |
| 173 |
Names: []string{"zero", "one", "two", "three"}, |
| 174 |
Assignments: map[string]int{"a": 0, "b1": 1, "b2": 1, "c": 2, "d": 3}, |
| 175 |
}, nil) |
| 176 |
checkEdges(` |
| 177 |
edge one -> two: b2 -> c; |
| 178 |
edge two -> three: c -> d; |
| 179 |
edge zero -> one: a -> b1;`[1:]) |
| 180 |
}) |
| 181 |
} |