golang.org/x/tools/gopls@v0.15.3/internal/util/goversion/goversion_test.go (about) 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 := []struct { 44 goVersion int 45 fromBuild bool 46 wantContains []string // string fragments that we expect to see 47 wantIsError bool // an error, not a mere warning 48 }{ 49 {-1, false, nil, false}, 50 deprecated(12, "v0.7.5"), 51 deprecated(13, "v0.9.5"), 52 deprecated(15, "v0.9.5"), 53 deprecated(16, "v0.11.0"), 54 deprecated(17, "v0.11.0"), 55 {18, false, []string{"Found Go version 1.18", "unsupported by gopls v0.16.0", "upgrade to Go 1.19", "install gopls v0.14.2"}, false}, 56 {18, true, []string{"Gopls was built with Go version 1.18", "unsupported by gopls v0.16.0", "upgrade to Go 1.19", "install gopls v0.14.2"}, false}, 57 } 58 59 for _, test := range tests { 60 gotMsg, gotIsError := goversion.Message(test.goVersion, test.fromBuild) 61 62 if len(test.wantContains) == 0 && gotMsg != "" { 63 t.Errorf("versionMessage(%d) = %q, want \"\"", test.goVersion, gotMsg) 64 } 65 66 for _, want := range test.wantContains { 67 if !strings.Contains(gotMsg, want) { 68 t.Errorf("versionMessage(%d) = %q, want containing %q", test.goVersion, gotMsg, want) 69 } 70 } 71 72 if gotIsError != test.wantIsError { 73 t.Errorf("versionMessage(%d) isError = %v, want %v", test.goVersion, gotIsError, test.wantIsError) 74 } 75 } 76 }