Quellcode durchsuchen

Introduce (UI).WantBrowser (#345)

This fixes a bug introduced in #341 which caused the browser
opening code to be disabled for the standard `pprof -http`
invocation.

Introduce a proper getter on the UI which determines whether
the browser ought to be opened.
Tobias Schottdorf vor 7 Jahren
Ursprung
Commit
2e397545a6

+ 3
- 0
driver/driver.go Datei anzeigen

@@ -218,6 +218,9 @@ type UI interface {
218 218
 	// interactive terminal (as opposed to being redirected to a file).
219 219
 	IsTerminal() bool
220 220
 
221
+	// WantBrowser indicates whether browser should be opened with the -http option.
222
+	WantBrowser() bool
223
+
221 224
 	// SetAutoComplete instructs the UI to call complete(cmd) to obtain
222 225
 	// the auto-completion of cmd, if the UI supports auto-completion at all.
223 226
 	SetAutoComplete(complete func(string) string)

+ 4
- 0
internal/driver/options.go Datei anzeigen

@@ -128,6 +128,10 @@ func (ui *stdUI) IsTerminal() bool {
128 128
 	return false
129 129
 }
130 130
 
131
+func (ui *stdUI) WantBrowser() bool {
132
+	return true
133
+}
134
+
131 135
 func (ui *stdUI) SetAutoComplete(func(string) string) {
132 136
 }
133 137
 

+ 1
- 1
internal/driver/webui.go Datei anzeigen

@@ -117,7 +117,7 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) e
117 117
 		},
118 118
 	}
119 119
 
120
-	if o.UI.IsTerminal() {
120
+	if o.UI.WantBrowser() {
121 121
 		go openBrowser("http://"+args.Hostport, o)
122 122
 	}
123 123
 	return server(args)

+ 2
- 7
internal/driver/webui_test.go Datei anzeigen

@@ -28,15 +28,10 @@ import (
28 28
 	"testing"
29 29
 
30 30
 	"github.com/google/pprof/internal/plugin"
31
+	"github.com/google/pprof/internal/proftest"
31 32
 	"github.com/google/pprof/profile"
32 33
 )
33 34
 
34
-type nonTerminalUI struct {
35
-	plugin.UI
36
-}
37
-
38
-func (*nonTerminalUI) IsTerminal() bool { return false }
39
-
40 35
 func TestWebInterface(t *testing.T) {
41 36
 	if runtime.GOOS == "nacl" {
42 37
 		t.Skip("test assumes tcp available")
@@ -61,7 +56,7 @@ func TestWebInterface(t *testing.T) {
61 56
 	// Start server and wait for it to be initialized
62 57
 	go serveWebInterface("unused:1234", prof, &plugin.Options{
63 58
 		Obj:        fakeObjTool{},
64
-		UI:         &nonTerminalUI{&stdUI{}}, // don't open browser
59
+		UI:         &proftest.TestUI{},
65 60
 		HTTPServer: creator,
66 61
 	})
67 62
 	<-serverCreated

+ 3
- 0
internal/plugin/plugin.go Datei anzeigen

@@ -192,6 +192,9 @@ type UI interface {
192 192
 	// interactive terminal (as opposed to being redirected to a file).
193 193
 	IsTerminal() bool
194 194
 
195
+	// WantBrowser indicates whether a browser should be opened with the -http option.
196
+	WantBrowser() bool
197
+
195 198
 	// SetAutoComplete instructs the UI to call complete(cmd) to obtain
196 199
 	// the auto-completion of cmd, if the UI supports auto-completion at all.
197 200
 	SetAutoComplete(complete func(string) string)

+ 5
- 0
internal/proftest/proftest.go Datei anzeigen

@@ -130,6 +130,11 @@ func (ui *TestUI) IsTerminal() bool {
130 130
 	return false
131 131
 }
132 132
 
133
+// WantBrowser indicates whether a browser should be opened with the -http option.
134
+func (ui *TestUI) WantBrowser() bool {
135
+	return false
136
+}
137
+
133 138
 // SetAutoComplete is not supported by the test UI.
134 139
 func (ui *TestUI) SetAutoComplete(_ func(string) string) {
135 140
 }