golang.org/x/tools/gopls@v0.15.3/internal/debug/info_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 debug exports debug information for gopls.
     6  package debug
     7  
     8  import (
     9  	"bytes"
    10  	"context"
    11  	"encoding/json"
    12  	"runtime"
    13  	"testing"
    14  
    15  	"golang.org/x/tools/gopls/internal/version"
    16  )
    17  
    18  func TestPrintVersionInfoJSON(t *testing.T) {
    19  	buf := new(bytes.Buffer)
    20  	if err := PrintVersionInfo(context.Background(), buf, true, JSON); err != nil {
    21  		t.Fatalf("PrintVersionInfo failed: %v", err)
    22  	}
    23  	res := buf.Bytes()
    24  
    25  	var got ServerVersion
    26  	if err := json.Unmarshal(res, &got); err != nil {
    27  		t.Fatalf("unexpected output: %v\n%s", err, res)
    28  	}
    29  	if g, w := got.GoVersion, runtime.Version(); g != w {
    30  		t.Errorf("go version = %v, want %v", g, w)
    31  	}
    32  	if g, w := got.Version, version.Version(); g != w {
    33  		t.Errorf("gopls version = %v, want %v", g, w)
    34  	}
    35  	// Other fields of BuildInfo may not be available during test.
    36  }
    37  
    38  func TestPrintVersionInfoPlainText(t *testing.T) {
    39  	buf := new(bytes.Buffer)
    40  	if err := PrintVersionInfo(context.Background(), buf, true, PlainText); err != nil {
    41  		t.Fatalf("PrintVersionInfo failed: %v", err)
    42  	}
    43  	res := buf.Bytes()
    44  
    45  	// Other fields of BuildInfo may not be available during test.
    46  	wantGoplsVersion, wantGoVersion := version.Version(), runtime.Version()
    47  	if !bytes.Contains(res, []byte(wantGoplsVersion)) || !bytes.Contains(res, []byte(wantGoVersion)) {
    48  		t.Errorf("plaintext output = %q,\nwant (version: %v, go: %v)", res, wantGoplsVersion, wantGoVersion)
    49  	}
    50  }