Debian Codegraph @

gopls/internal/util/goversion/goversion_test.go

raw symbols SCIP
1 // Copyright 2022 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 goversion_test
6
7 import (
8 "fmt"
9 "strings"
10 "testing"
11
12 "golang.org/x/tools/gopls/internal/util/goversion"
13 )
14
15 func TestMessage(t *testing.T) {
16 // Note(rfindley): this test is a change detector, as it must be updated
17 // whenever we deprecate a version.
18 //
19 // However, I chose to leave it as is since it gives us confidence in error
20 // messages served for Go versions that we no longer support (and therefore
21 // no longer run in CI).
22 type test struct {
23 goVersion int
24 fromBuild bool
25 wantContains []string // string fragments that we expect to see
26 wantIsError bool // an error, not a mere warning
27 }
28
29 deprecated := func(goVersion int, lastVersion string) test {
30 return test{
31 goVersion: goVersion,
32 fromBuild: false,
33 wantContains: []string{
34 fmt.Sprintf("Found Go version 1.%d", goVersion),
35 "not supported",
36 fmt.Sprintf("upgrade to Go 1.%d", goversion.OldestSupported()),
37 fmt.Sprintf("install gopls %s", lastVersion),
38 },
39 wantIsError: true,
40 }
41 }
42
43 tests := []test{
44 {-1, false, nil, false},
45 deprecated(12, "v0.7.5"),
46 deprecated(13, "v0.9.5"),
47 deprecated(15, "v0.9.5"),
48 deprecated(16, "v0.11.0"),
49 deprecated(17, "v0.11.0"),
50 deprecated(18, "v0.14.2"),
51 {19, false, []string{"Found Go version 1.19", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false},
52 {19, true, []string{"Gopls was built with Go version 1.19", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false},
53 {20, false, []string{"Found Go version 1.20", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false},
54 {20, true, []string{"Gopls was built with Go version 1.20", "unsupported by gopls v0.17.0", "upgrade to Go 1.21", "install gopls v0.15.3"}, false},
55 }
56
57 for _, test := range tests {
58 gotMsg, gotIsError := goversion.Message(test.goVersion, test.fromBuild)
59
60 if len(test.wantContains) == 0 && gotMsg != "" {
61 t.Errorf("versionMessage(%d) = %q, want \"\"", test.goVersion, gotMsg)
62 }
63
64 for _, want := range test.wantContains {
65 if !strings.Contains(gotMsg, want) {
66 t.Errorf("versionMessage(%d) = %q, want containing %q", test.goVersion, gotMsg, want)
67 }
68 }
69
70 if gotIsError != test.wantIsError {
71 t.Errorf("versionMessage(%d) isError = %v, want %v", test.goVersion, gotIsError, test.wantIsError)
72 }
73 }
74 }