Browse Source

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 7 years ago
parent
commit
2e397545a6

+ 3
- 0
driver/driver.go View File

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

+ 4
- 0
internal/driver/options.go View File

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

+ 1
- 1
internal/driver/webui.go View File

117
 		},
117
 		},
118
 	}
118
 	}
119
 
119
 
120
-	if o.UI.IsTerminal() {
120
+	if o.UI.WantBrowser() {
121
 		go openBrowser("http://"+args.Hostport, o)
121
 		go openBrowser("http://"+args.Hostport, o)
122
 	}
122
 	}
123
 	return server(args)
123
 	return server(args)

+ 2
- 7
internal/driver/webui_test.go View File

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

+ 3
- 0
internal/plugin/plugin.go View File

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

+ 5
- 0
internal/proftest/proftest.go View File

130
 	return false
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
 // SetAutoComplete is not supported by the test UI.
138
 // SetAutoComplete is not supported by the test UI.
134
 func (ui *TestUI) SetAutoComplete(_ func(string) string) {
139
 func (ui *TestUI) SetAutoComplete(_ func(string) string) {
135
 }
140
 }