github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/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  
    16  func TestPrintVersionInfoJSON(t *testing.T) {
    17  	buf := new(bytes.Buffer)
    18  	if err := PrintVersionInfo(context.Background(), buf, true, JSON); err != nil {
    19  		t.Fatalf("PrintVersionInfo failed: %v", err)
    20  	}
    21  	res := buf.Bytes()
    22  
    23  	var got ServerVersion
    24  	if err := json.Unmarshal(res, &got); err != nil {
    25  		t.Fatalf("unexpected output: %v\n%s", err, res)
    26  	}
    27  	if g, w := got.GoVersion, runtime.Version(); g != w {
    28  		t.Errorf("go version = %v, want %v", g, w)
    29  	}
    30  	if g, w := got.Version, Version; g != w {
    31  		t.Errorf("gopls version = %v, want %v", g, w)
    32  	}
    33  	// Other fields of BuildInfo may not be available during test.
    34  }
    35  
    36  func TestPrintVersionInfoPlainText(t *testing.T) {
    37  	buf := new(bytes.Buffer)
    38  	if err := PrintVersionInfo(context.Background(), buf, true, PlainText); err != nil {
    39  		t.Fatalf("PrintVersionInfo failed: %v", err)
    40  	}
    41  	res := buf.Bytes()
    42  
    43  	// Other fields of BuildInfo may not be available during test.
    44  	if !bytes.Contains(res, []byte(Version)) || !bytes.Contains(res, []byte(runtime.Version())) {
    45  		t.Errorf("plaintext output = %q,\nwant (version: %v, go: %v)", res, Version, runtime.Version())
    46  	}
    47  }