golang.org/x/tools/gopls@v0.15.3/internal/test/integration/codelens/gcdetails_test.go (about)

     1  // Copyright 2020 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 codelens
     6  
     7  import (
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  
    12  	"golang.org/x/tools/gopls/internal/protocol"
    13  	"golang.org/x/tools/gopls/internal/protocol/command"
    14  	"golang.org/x/tools/gopls/internal/server"
    15  	. "golang.org/x/tools/gopls/internal/test/integration"
    16  	"golang.org/x/tools/gopls/internal/test/integration/fake"
    17  	"golang.org/x/tools/gopls/internal/util/bug"
    18  )
    19  
    20  func TestGCDetails_Toggle(t *testing.T) {
    21  	if runtime.GOOS == "android" {
    22  		t.Skipf("the gc details code lens doesn't work on Android")
    23  	}
    24  
    25  	const mod = `
    26  -- go.mod --
    27  module mod.com
    28  
    29  go 1.15
    30  -- main.go --
    31  package main
    32  
    33  import "fmt"
    34  
    35  func main() {
    36  	fmt.Println(42)
    37  }
    38  `
    39  	WithOptions(
    40  		Settings{
    41  			"codelenses": map[string]bool{
    42  				"gc_details": true,
    43  			},
    44  		},
    45  	).Run(t, mod, func(t *testing.T, env *Env) {
    46  		env.OpenFile("main.go")
    47  		env.ExecuteCodeLensCommand("main.go", command.GCDetails, nil)
    48  		d := &protocol.PublishDiagnosticsParams{}
    49  		env.OnceMet(
    50  			CompletedWork(server.DiagnosticWorkTitle(server.FromToggleGCDetails), 1, true),
    51  			ReadDiagnostics("main.go", d),
    52  		)
    53  		// Confirm that the diagnostics come from the gc details code lens.
    54  		var found bool
    55  		for _, d := range d.Diagnostics {
    56  			if d.Severity != protocol.SeverityInformation {
    57  				t.Fatalf("unexpected diagnostic severity %v, wanted Information", d.Severity)
    58  			}
    59  			if strings.Contains(d.Message, "42 escapes") {
    60  				found = true
    61  			}
    62  		}
    63  		if !found {
    64  			t.Fatalf(`expected to find diagnostic with message "escape(42 escapes to heap)", found none`)
    65  		}
    66  
    67  		// Editing a buffer should cause gc_details diagnostics to disappear, since
    68  		// they only apply to saved buffers.
    69  		env.EditBuffer("main.go", fake.NewEdit(0, 0, 0, 0, "\n\n"))
    70  		env.AfterChange(NoDiagnostics(ForFile("main.go")))
    71  
    72  		// Saving a buffer should re-format back to the original state, and
    73  		// re-enable the gc_details diagnostics.
    74  		env.SaveBuffer("main.go")
    75  		env.AfterChange(Diagnostics(AtPosition("main.go", 5, 13)))
    76  
    77  		// Toggle the GC details code lens again so now it should be off.
    78  		env.ExecuteCodeLensCommand("main.go", command.GCDetails, nil)
    79  		env.OnceMet(
    80  			CompletedWork(server.DiagnosticWorkTitle(server.FromToggleGCDetails), 2, true),
    81  			NoDiagnostics(ForFile("main.go")),
    82  		)
    83  	})
    84  }
    85  
    86  // Test for the crasher in golang/go#54199
    87  func TestGCDetails_NewFile(t *testing.T) {
    88  	bug.PanicOnBugs = false
    89  	const src = `
    90  -- go.mod --
    91  module mod.test
    92  
    93  go 1.12
    94  `
    95  
    96  	WithOptions(
    97  		Settings{
    98  			"codelenses": map[string]bool{
    99  				"gc_details": true,
   100  			},
   101  		},
   102  	).Run(t, src, func(t *testing.T, env *Env) {
   103  		env.CreateBuffer("p_test.go", "")
   104  
   105  		hasGCDetails := func() bool {
   106  			lenses := env.CodeLens("p_test.go") // should not crash
   107  			for _, lens := range lenses {
   108  				if lens.Command.Command == command.GCDetails.ID() {
   109  					return true
   110  				}
   111  			}
   112  			return false
   113  		}
   114  
   115  		// With an empty file, we shouldn't get the gc_details codelens because
   116  		// there is nowhere to position it (it needs a package name).
   117  		if hasGCDetails() {
   118  			t.Errorf("got the gc_details codelens for an empty file")
   119  		}
   120  
   121  		// Edit to provide a package name.
   122  		env.EditBuffer("p_test.go", fake.NewEdit(0, 0, 0, 0, "package p"))
   123  
   124  		// Now we should get the gc_details codelens.
   125  		if !hasGCDetails() {
   126  			t.Errorf("didn't get the gc_details codelens for a valid non-empty Go file")
   127  		}
   128  	})
   129  }