github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/lsp/lsprpc/goenv_test.go (about)

     1  // Copyright 2021 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 lsprpc_test
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/powerman/golang-tools/internal/lsp/protocol"
    12  	"github.com/powerman/golang-tools/internal/testenv"
    13  
    14  	. "github.com/powerman/golang-tools/internal/lsp/lsprpc"
    15  )
    16  
    17  type initServer struct {
    18  	protocol.Server
    19  
    20  	params *protocol.ParamInitialize
    21  }
    22  
    23  func (s *initServer) Initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
    24  	s.params = params
    25  	return &protocol.InitializeResult{}, nil
    26  }
    27  
    28  func TestGoEnvMiddleware(t *testing.T) {
    29  	testenv.NeedsGo1Point(t, 13)
    30  
    31  	ctx := context.Background()
    32  
    33  	server := &initServer{}
    34  	env := new(TestEnv)
    35  	defer env.Shutdown(t)
    36  	l, _ := env.serve(ctx, t, staticServerBinder(server))
    37  	mw, err := GoEnvMiddleware()
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	binder := mw(NewForwardBinder(l.Dialer()))
    42  	l, _ = env.serve(ctx, t, binder)
    43  	conn := env.dial(ctx, t, l.Dialer(), noopBinder, true)
    44  	dispatch := protocol.ServerDispatcherV2(conn)
    45  	initParams := &protocol.ParamInitialize{}
    46  	initParams.InitializationOptions = map[string]interface{}{
    47  		"env": map[string]interface{}{
    48  			"GONOPROXY": "example.com",
    49  		},
    50  	}
    51  	if _, err := dispatch.Initialize(ctx, initParams); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	if server.params == nil {
    56  		t.Fatalf("initialize params are unset")
    57  	}
    58  	envOpts := server.params.InitializationOptions.(map[string]interface{})["env"].(map[string]interface{})
    59  
    60  	// Check for an arbitrary Go variable. It should be set.
    61  	if _, ok := envOpts["GOPRIVATE"]; !ok {
    62  		t.Errorf("Go environment variable GOPRIVATE unset in initialization options")
    63  	}
    64  	// Check that the variable present in our user config was not overwritten.
    65  	if got, want := envOpts["GONOPROXY"], "example.com"; got != want {
    66  		t.Errorf("GONOPROXY=%q, want %q", got, want)
    67  	}
    68  }