Skip to content

Commit 9da4f7b

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/test/integration/completion: make test go1.26-robust
As the API of the testing package evolves, TestFuzzFunc, which uses it to exercise completions, keeps breaking. This CL introduces optional items to make it easier to fix the tests as it breaks each time the API evolves. CL 696399 added (*testing.F).Artifact to go1.26, causing the test to break spuriously. See also golang/go#74987, about the same issue when go1.25 added the Attr method. Updates golang/go#74987 Change-Id: I9653dae29bff8a976f7a4d9eb4b785b4b28b05f5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/702077 Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 4409ea7 commit 9da4f7b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

gopls/internal/test/integration/completion/completion18_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func (s SyncMap[XX,string]) g(v UU) {}
5858
}
5959

6060
func TestFuzzFunc(t *testing.T) {
61+
// The behavior under test is derived from the std module,
62+
// not the x/tools/internal/stdlib linked into gopls.
6163
testenv.NeedsGoCommand1Point(t, 25) // go1.25 added TBF.Attr
6264

6365
// use the example from the package documentation
@@ -103,7 +105,9 @@ func FuzzHex(f *testing.F) {
103105
offset uint32 // UTF16 length from the beginning of pat to what the user just typed
104106
want []string
105107
}{
106-
{"a_test.go", "f.Ad", 3, []string{"Add", "Attr"}},
108+
// To avoid breaking these assertions as the "testing" package evolves,
109+
// use an optional (?) suffix for newer symbols.
110+
{"a_test.go", "f.Ad", 3, []string{"Add", "Attr", "Artifact?"}}, // Attr is 1.25, Artifact is 1.26
107111
{"c_test.go", " f.F", 4, []string{"Failed"}},
108112
{"c_test.go", "f.N", 3, []string{"Name"}},
109113
{"b_test.go", "f.F", 3, []string{"Fuzz(func(t *testing.T, a []byte)", "Fail", "FailNow",

gopls/internal/test/integration/completion/completion_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package completion
77
import (
88
"fmt"
99
"os"
10+
"slices"
1011
"sort"
1112
"strings"
1213
"testing"
@@ -258,6 +259,12 @@ package ma
258259
})
259260
}
260261

262+
// compareCompletionLabels returns a non-empty string reporting the
263+
// difference (if any) between the labels of the actual completion
264+
// items (gotItems) and the expected list (want).
265+
//
266+
// A want item with a "?" suffix is optional.
267+
//
261268
// TODO(rfindley): audit/clean up call sites for this helper, to ensure
262269
// consistent test errors.
263270
func compareCompletionLabels(want []string, gotItems []protocol.CompletionItem) string {
@@ -274,6 +281,19 @@ func compareCompletionLabels(want []string, gotItems []protocol.CompletionItem)
274281
return "" // treat nil and the empty slice as equivalent
275282
}
276283

284+
// A 'want' item with a '?' suffix is optional, to ease
285+
// migration across versions. Remove any that are not present
286+
// in the 'got' set.
287+
out := want[:0] // in-place compaction
288+
for _, item := range want {
289+
item, optional := strings.CutSuffix(item, "?")
290+
if optional && !slices.Contains(got, item) {
291+
continue // optional item is missing
292+
}
293+
out = append(out, item)
294+
}
295+
want = out
296+
277297
if diff := cmp.Diff(want, got); diff != "" {
278298
return fmt.Sprintf("completion item mismatch (-want +got):\n%s", diff)
279299
}

0 commit comments

Comments
 (0)