|
| 1 | +// Copyright 2025 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 web_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "runtime" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "golang.org/x/tools/gopls/internal/protocol" |
| 12 | + "golang.org/x/tools/gopls/internal/protocol/command" |
| 13 | + "golang.org/x/tools/gopls/internal/settings" |
| 14 | + . "golang.org/x/tools/gopls/internal/test/integration" |
| 15 | + "golang.org/x/tools/internal/testenv" |
| 16 | +) |
| 17 | + |
| 18 | +// TestAssembly is a basic test of the web-based assembly listing. |
| 19 | +func TestAssembly(t *testing.T) { |
| 20 | + testenv.NeedsGoCommand1Point(t, 22) // for up-to-date assembly listing |
| 21 | + |
| 22 | + const files = ` |
| 23 | +-- go.mod -- |
| 24 | +module example.com |
| 25 | +
|
| 26 | +-- a/a.go -- |
| 27 | +package a |
| 28 | +
|
| 29 | +func f(x int) int { |
| 30 | + println("hello") |
| 31 | + defer println("world") |
| 32 | + return x |
| 33 | +} |
| 34 | +
|
| 35 | +func g() { |
| 36 | + println("goodbye") |
| 37 | +} |
| 38 | +
|
| 39 | +var v = [...]int{ |
| 40 | + f(123), |
| 41 | + f(456), |
| 42 | +} |
| 43 | +
|
| 44 | +func init() { |
| 45 | + f(789) |
| 46 | +} |
| 47 | +` |
| 48 | + Run(t, files, func(t *testing.T, env *Env) { |
| 49 | + env.OpenFile("a/a.go") |
| 50 | + |
| 51 | + asmFor := func(pattern string) []byte { |
| 52 | + // Invoke the "Browse assembly" code action to start the server. |
| 53 | + loc := env.RegexpSearch("a/a.go", pattern) |
| 54 | + actions, err := env.Editor.CodeAction(env.Ctx, loc, nil, protocol.CodeActionUnknownTrigger) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("CodeAction: %v", err) |
| 57 | + } |
| 58 | + action, err := codeActionByKind(actions, settings.GoAssembly) |
| 59 | + if err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + |
| 63 | + // Execute the command. |
| 64 | + // Its side effect should be a single showDocument request. |
| 65 | + params := &protocol.ExecuteCommandParams{ |
| 66 | + Command: action.Command.Command, |
| 67 | + Arguments: action.Command.Arguments, |
| 68 | + } |
| 69 | + var result command.DebuggingResult |
| 70 | + collectDocs := env.Awaiter.ListenToShownDocuments() |
| 71 | + env.ExecuteCommand(params, &result) |
| 72 | + doc := shownDocument(t, collectDocs(), "http:") |
| 73 | + if doc == nil { |
| 74 | + t.Fatalf("no showDocument call had 'file:' prefix") |
| 75 | + } |
| 76 | + t.Log("showDocument(package doc) URL:", doc.URI) |
| 77 | + |
| 78 | + return get(t, doc.URI) |
| 79 | + } |
| 80 | + |
| 81 | + // Get the report and do some minimal checks for sensible results. |
| 82 | + // |
| 83 | + // Use only portable instructions below! Remember that |
| 84 | + // this is a test of plumbing, not compilation, so |
| 85 | + // it's better to skip the tests, rather than refine |
| 86 | + // them, on any architecture that gives us trouble |
| 87 | + // (e.g. uses JAL for CALL, or BL<cc> for RET). |
| 88 | + // We conservatively test only on the two most popular |
| 89 | + // architectures. |
| 90 | + { |
| 91 | + report := asmFor("println") |
| 92 | + checkMatch(t, true, report, `TEXT.*example.com/a.f`) |
| 93 | + switch runtime.GOARCH { |
| 94 | + case "amd64", "arm64": |
| 95 | + checkMatch(t, true, report, `CALL runtime.printlock`) |
| 96 | + checkMatch(t, true, report, `CALL runtime.printstring`) |
| 97 | + checkMatch(t, true, report, `CALL runtime.printunlock`) |
| 98 | + checkMatch(t, true, report, `CALL example.com/a.f.deferwrap`) |
| 99 | + checkMatch(t, true, report, `RET`) |
| 100 | + checkMatch(t, true, report, `CALL runtime.morestack_noctxt`) |
| 101 | + } |
| 102 | + |
| 103 | + // Nested functions are also shown. |
| 104 | + // |
| 105 | + // The condition here was relaxed to unblock go.dev/cl/639515. |
| 106 | + checkMatch(t, true, report, `example.com/a.f.deferwrap`) |
| 107 | + |
| 108 | + // But other functions are not. |
| 109 | + checkMatch(t, false, report, `TEXT.*example.com/a.g`) |
| 110 | + } |
| 111 | + |
| 112 | + // Check that code in a package-level var initializer is found too. |
| 113 | + { |
| 114 | + report := asmFor(`f\(123\)`) |
| 115 | + switch runtime.GOARCH { |
| 116 | + case "amd64", "arm64": |
| 117 | + checkMatch(t, true, report, `TEXT.*example.com/a.init`) |
| 118 | + checkMatch(t, true, report, `MOV.? \$123`) |
| 119 | + checkMatch(t, true, report, `MOV.? \$456`) |
| 120 | + checkMatch(t, true, report, `CALL example.com/a.f`) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // And code in a source-level init function. |
| 125 | + { |
| 126 | + report := asmFor(`f\(789\)`) |
| 127 | + switch runtime.GOARCH { |
| 128 | + case "amd64", "arm64": |
| 129 | + checkMatch(t, true, report, `TEXT.*example.com/a.init`) |
| 130 | + checkMatch(t, true, report, `MOV.? \$789`) |
| 131 | + checkMatch(t, true, report, `CALL example.com/a.f`) |
| 132 | + } |
| 133 | + } |
| 134 | + }) |
| 135 | +} |
0 commit comments