Skip to content

Commit bbf8380

Browse files
committed
gopls/internal/regtest/marker: use golden diffs for suggested fixes
Following the model of codeactionerr, use diffs to reduce the verbosity and redundancy of golden content, for the suggestedfix marker. Also, since all suggested fixes should be of kind 'quickfix', remove the unnecessary kind parameter. Finally, clean up some stale comments. For golang/go#54845 Change-Id: I2eb08e4415dcff4acba604bf97b16c0a82c0a658 Reviewed-on: https://go-review.googlesource.com/c/tools/+/539656 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 51df92b commit bbf8380

File tree

9 files changed

+92
-123
lines changed

9 files changed

+92
-123
lines changed

gopls/internal/lsp/regtest/marker.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,6 @@ var update = flag.Bool("update", false, "if set, update test data during marker
249249
// to have exactly one associated code action of the specified kind.
250250
// This action is executed for its editing effects on the source files.
251251
// Like rename, the golden directory contains the expected transformed files.
252-
// TODO(rfindley): we probably only need 'suggestedfix' for quick-fixes. All
253-
// other actions should use codeaction markers. In that case, we can remove
254-
// the 'kind' parameter.
255252
//
256253
// - rank(location, ...completionItem): executes a textDocument/completion
257254
// request at the given location, and verifies that each expected
@@ -387,15 +384,11 @@ var update = flag.Bool("update", false, "if set, update test data during marker
387384
//
388385
// Existing marker tests (in ../testdata) to port:
389386
// - CallHierarchy
390-
// - Completions
391-
// - CompletionSnippets
392-
// - CaseSensitiveCompletions
393-
// - RankCompletions
394387
// - SemanticTokens
395-
// - FunctionExtractions
388+
// - SuggestedFixes
396389
// - MethodExtractions
397-
// - Renames
398390
// - InlayHints
391+
// - Renames
399392
// - SelectionRanges
400393
func RunMarkerTests(t *testing.T, dir string) {
401394
// The marker tests must be able to run go/packages.Load.
@@ -407,7 +400,6 @@ func RunMarkerTests(t *testing.T, dir string) {
407400
}
408401

409402
// Opt: use a shared cache.
410-
// TODO(rfindley): opt: use a memoize store with no eviction.
411403
cache := cache.New(nil)
412404

413405
for _, test := range tests {
@@ -2035,7 +2027,7 @@ func (mark marker) consumeExtraNotes(name string, f func(marker)) {
20352027
// kind, golden) marker. It acts like @diag(location, regexp), to set
20362028
// the expectation of a diagnostic, but then it applies the first code
20372029
// action of the specified kind suggested by the matched diagnostic.
2038-
func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, actionKind string, golden *Golden) {
2030+
func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, golden *Golden) {
20392031
loc.Range.End = loc.Range.Start // diagnostics ignore end position.
20402032
// Find and remove the matching diagnostic.
20412033
diag, ok := removeDiagnostic(mark, loc, re)
@@ -2045,14 +2037,14 @@ func suggestedfixMarker(mark marker, loc protocol.Location, re *regexp.Regexp, a
20452037
}
20462038

20472039
// Apply the fix it suggests.
2048-
changed, err := codeAction(mark.run.env, loc.URI, diag.Range, actionKind, &diag)
2040+
changed, err := codeAction(mark.run.env, loc.URI, diag.Range, "quickfix", &diag)
20492041
if err != nil {
20502042
mark.errorf("suggestedfix failed: %v. (Use @suggestedfixerr for expected errors.)", err)
20512043
return
20522044
}
20532045

20542046
// Check the file state.
2055-
checkChangedFiles(mark, changed, golden)
2047+
checkDiffs(mark, changed, golden)
20562048
}
20572049

20582050
// codeAction executes a textDocument/codeAction request for the specified

gopls/internal/regtest/marker/testdata/diagnostics/typeerr.txt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ package a
1919
func f(x int) {
2020
append("") //@diag(re`""`, re"a slice")
2121

22-
x := 123 //@diag(re"x := 123", re"no new variables"), suggestedfix(re"():", re"no new variables", "quickfix", fix)
22+
x := 123 //@diag(re"x := 123", re"no new variables"), suggestedfix(re"():", re"no new variables", fix)
2323
}
2424

2525
-- @fix/typeerr.go --
26-
package a
27-
28-
func f(x int) {
29-
append("") //@diag(re`""`, re"a slice")
30-
31-
x = 123 //@diag(re"x := 123", re"no new variables"), suggestedfix(re"():", re"no new variables", "quickfix", fix)
32-
}
33-
26+
--- before
27+
+++ after
28+
@@ -6 +6 @@
29+
- x := 123 //@diag(re"x := 123", re"no new variables"), suggestedfix(re"():", re"no new variables", fix)
30+
+ x = 123 //@diag(re"x := 123", re"no new variables"), suggestedfix(re"():", re"no new variables", fix)

gopls/internal/regtest/marker/testdata/stubmethods/basic.txt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ package a
99

1010
type C int
1111

12-
var _ error = C(0) //@suggestedfix(re"C.0.", re"missing method Error", "quickfix", stub)
13-
12+
var _ error = C(0) //@suggestedfix(re"C.0.", re"missing method Error", stub)
1413
-- @stub/a/a.go --
15-
package a
16-
17-
type C int
18-
19-
// Error implements error.
20-
func (C) Error() string {
21-
panic("unimplemented")
22-
}
23-
24-
var _ error = C(0) //@suggestedfix(re"C.0.", re"missing method Error", "quickfix", stub)
14+
--- before
15+
+++ after
16+
@@ -3 +3,6 @@
17+
-type C int
18+
+type C int
19+
+
20+
+// Error implements error.
21+
+func (C) Error() string {
22+
+ panic("unimplemented")
23+
+}

gopls/internal/regtest/marker/testdata/stubmethods/issue61693.txt

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,16 @@ func F(err ...error) {}
1515

1616
func _() {
1717
var x error
18-
F(x, C(0)) //@suggestedfix(re"C.0.", re"missing method Error", "quickfix", stub)
18+
F(x, C(0)) //@suggestedfix(re"C.0.", re"missing method Error", stub)
1919
}
2020
-- @stub/main.go --
21-
package main
22-
23-
type C int
24-
25-
// Error implements error.
26-
func (C) Error() string {
27-
panic("unimplemented")
28-
}
29-
30-
func F(err ...error) {}
31-
32-
func _() {
33-
var x error
34-
F(x, C(0)) //@suggestedfix(re"C.0.", re"missing method Error", "quickfix", stub)
35-
}
21+
--- before
22+
+++ after
23+
@@ -3 +3,6 @@
24+
-type C int
25+
+type C int
26+
+
27+
+// Error implements error.
28+
+func (C) Error() string {
29+
+ panic("unimplemented")
30+
+}

gopls/internal/regtest/marker/testdata/stubmethods/issue61830.txt

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,15 @@ type I interface {
1414

1515
type A struct{}
1616

17-
var _ I = &A{} //@suggestedfix(re"&A..", re"missing method M", "quickfix", stub)
17+
var _ I = &A{} //@suggestedfix(re"&A..", re"missing method M", stub)
1818
-- @stub/p.go --
19-
package p
20-
21-
import "io"
22-
23-
type B struct{}
24-
25-
type I interface {
26-
M(io.Reader, B)
27-
}
28-
29-
type A struct{}
30-
31-
// M implements I.
32-
func (*A) M(io.Reader, B) {
33-
panic("unimplemented")
34-
}
35-
36-
var _ I = &A{} //@suggestedfix(re"&A..", re"missing method M", "quickfix", stub)
19+
--- before
20+
+++ after
21+
@@ -11 +11,6 @@
22+
-type A struct{}
23+
+type A struct{}
24+
+
25+
+// M implements I.
26+
+func (*A) M(io.Reader, B) {
27+
+ panic("unimplemented")
28+
+}

gopls/internal/regtest/marker/testdata/suggestedfix/self_assignment.txt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@ import (
99

1010
func goodbye() {
1111
s := "hiiiiiii"
12-
s = s //@suggestedfix("s = s", re"self-assignment", "quickfix", fix)
12+
s = s //@suggestedfix("s = s", re"self-assignment", fix)
1313
log.Print(s)
1414
}
1515

1616
-- @fix/a.go --
17-
package suggestedfix
18-
19-
import (
20-
"log"
21-
)
22-
23-
func goodbye() {
24-
s := "hiiiiiii"
25-
//@suggestedfix("s = s", re"self-assignment", "quickfix", fix)
26-
log.Print(s)
27-
}
28-
17+
--- before
18+
+++ after
19+
@@ -9 +9 @@
20+
- s = s //@suggestedfix("s = s", re"self-assignment", fix)
21+
+ //@suggestedfix("s = s", re"self-assignment", fix)

gopls/internal/regtest/marker/testdata/suggestedfix/undeclared.txt

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,46 @@ go 1.12
99
package p
1010

1111
func a() {
12-
z, _ := 1+y, 11 //@suggestedfix("y", re"(undeclared name|undefined): y", "quickfix", a)
12+
z, _ := 1+y, 11 //@suggestedfix("y", re"(undeclared name|undefined): y", a)
1313
_ = z
1414
}
1515

1616
-- @a/a.go --
17-
package p
18-
19-
func a() {
20-
y :=
21-
z, _ := 1+y, 11 //@suggestedfix("y", re"(undeclared name|undefined): y", "quickfix", a)
22-
_ = z
23-
}
24-
17+
--- before
18+
+++ after
19+
@@ -3 +3,2 @@
20+
-func a() {
21+
+func a() {
22+
+ y :=
2523
-- b.go --
2624
package p
2725

2826
func b() {
2927
if 100 < 90 {
30-
} else if 100 > n+2 { //@suggestedfix("n", re"(undeclared name|undefined): n", "quickfix", b)
28+
} else if 100 > n+2 { //@suggestedfix("n", re"(undeclared name|undefined): n", b)
3129
}
3230
}
3331

3432
-- @b/b.go --
35-
package p
36-
37-
func b() {
38-
n :=
39-
if 100 < 90 {
40-
} else if 100 > n+2 { //@suggestedfix("n", re"(undeclared name|undefined): n", "quickfix", b)
41-
}
42-
}
43-
33+
--- before
34+
+++ after
35+
@@ -3 +3,2 @@
36+
-func b() {
37+
+func b() {
38+
+ n :=
4439
-- c.go --
4540
package p
4641

4742
func c() {
48-
for i < 200 { //@suggestedfix("i", re"(undeclared name|undefined): i", "quickfix", c)
43+
for i < 200 { //@suggestedfix("i", re"(undeclared name|undefined): i", c)
4944
}
5045
r() //@diag("r", re"(undeclared name|undefined): r")
5146
}
5247

5348
-- @c/c.go --
54-
package p
55-
56-
func c() {
57-
i :=
58-
for i < 200 { //@suggestedfix("i", re"(undeclared name|undefined): i", "quickfix", c)
59-
}
60-
r() //@diag("r", re"(undeclared name|undefined): r")
61-
}
62-
49+
--- before
50+
+++ after
51+
@@ -3 +3,2 @@
52+
-func c() {
53+
+func c() {
54+
+ i :=

gopls/internal/regtest/marker/testdata/suggestedfix/unusedrequire.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ module mod.com
1313

1414
go 1.14
1515

16-
require example.com v1.0.0 //@suggestedfix("require", re"not used", "quickfix", a)
16+
require example.com v1.0.0 //@suggestedfix("require", re"not used", a)
1717

1818
-- @a/a/go.mod --
19-
module mod.com
20-
21-
go 1.14
19+
--- before
20+
+++ after
21+
@@ -4,3 +4 @@
22+
-
23+
-require example.com v1.0.0 //@suggestedfix("require", re"not used", a)
24+
-
2225
-- a/main.go --
2326
package main
2427
func main() {}

gopls/internal/regtest/marker/testdata/suggestedfix/unusedrequire_gowork.txt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ module mod.com/a
2323

2424
go 1.14
2525

26-
require example.com v1.0.0 //@suggestedfix("require", re"not used", "quickfix", a)
26+
require example.com v1.0.0 //@suggestedfix("require", re"not used", a)
2727

2828
-- @a/a/go.mod --
29-
module mod.com/a
30-
31-
go 1.14
29+
--- before
30+
+++ after
31+
@@ -4,3 +4 @@
32+
-
33+
-require example.com v1.0.0 //@suggestedfix("require", re"not used", a)
34+
-
3235
-- a/main.go --
3336
package main
3437
func main() {}
@@ -38,12 +41,15 @@ module mod.com/b
3841

3942
go 1.14
4043

41-
require example.com v1.0.0 //@suggestedfix("require", re"not used", "quickfix", b)
44+
require example.com v1.0.0 //@suggestedfix("require", re"not used", b)
4245

4346
-- @b/b/go.mod --
44-
module mod.com/b
45-
46-
go 1.14
47+
--- before
48+
+++ after
49+
@@ -4,3 +4 @@
50+
-
51+
-require example.com v1.0.0 //@suggestedfix("require", re"not used", b)
52+
-
4753
-- b/main.go --
4854
package main
4955
func main() {}

0 commit comments

Comments
 (0)