diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-02 08:46:56 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-02 08:46:56 +0200 |
| commit | 717cf6a47cd19cc284b023825a3bac3272ebe171 (patch) | |
| tree | 5be553754917157311d107538a99e34b2f8aa839 /internal/flamegraph | |
| parent | b3327d17f9f5c7080b05d6d5457cf25550d40ad9 (diff) | |
Make --open a command template with no defaults
Diffstat (limited to 'internal/flamegraph')
| -rw-r--r-- | internal/flamegraph/liveserver.go | 30 | ||||
| -rw-r--r-- | internal/flamegraph/liveserver_open_test.go | 54 |
2 files changed, 19 insertions, 65 deletions
diff --git a/internal/flamegraph/liveserver.go b/internal/flamegraph/liveserver.go index dda1af1..de65ee3 100644 --- a/internal/flamegraph/liveserver.go +++ b/internal/flamegraph/liveserver.go @@ -7,7 +7,6 @@ import ( "fmt" "net/http" "os/exec" - "runtime" "strings" "time" ) @@ -19,8 +18,7 @@ var liveServerTimeouts = serverTimeouts{ } type LiveServerOptions struct { - AutoOpenBrowser bool - OpenCommand string + OpenCommand string } var openBrowserURLFn = openBrowserURL @@ -47,14 +45,14 @@ func ServeLiveWithOptions(ctx context.Context, lt *LiveTrie, interval time.Durat } func maybeOpenLiveBrowser(url string, options LiveServerOptions) error { - if !options.AutoOpenBrowser { + if strings.TrimSpace(options.OpenCommand) == "" { return nil } return openBrowserURLFn(url, options.OpenCommand) } func openBrowserURL(url, openCommand string) error { - parts, err := browserOpenCommandParts(runtime.GOOS, openCommand, url) + parts, err := browserOpenCommandParts(openCommand, url) if err != nil { return err } @@ -66,13 +64,8 @@ func openBrowserURL(url, openCommand string) error { return nil } -func browserOpenCommandParts(goos, openCommand, url string) ([]string, error) { - var parts []string - if trimmed := strings.TrimSpace(openCommand); trimmed != "" { - parts = strings.Fields(trimmed) - } else { - parts = defaultBrowserCommand(goos) - } +func browserOpenCommandParts(openCommand, url string) ([]string, error) { + parts := strings.Fields(strings.TrimSpace(openCommand)) if len(parts) == 0 { return nil, errors.New("empty browser open command") } @@ -90,19 +83,6 @@ func browserOpenCommandParts(goos, openCommand, url string) ([]string, error) { return parts, nil } -func defaultBrowserCommand(goos string) []string { - switch goos { - case "darwin": - return []string{"open", "-a", "Firefox"} - case "linux": - return []string{"firefox"} - case "windows": - return []string{"cmd", "/c", "start"} - default: - return []string{"firefox"} - } -} - func handleLivePage() http.HandlerFunc { return func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") diff --git a/internal/flamegraph/liveserver_open_test.go b/internal/flamegraph/liveserver_open_test.go index 9d280b9..0a42a61 100644 --- a/internal/flamegraph/liveserver_open_test.go +++ b/internal/flamegraph/liveserver_open_test.go @@ -5,40 +5,15 @@ import ( "testing" ) -func TestBrowserOpenCommandPartsUsesLinuxDefault(t *testing.T) { - got, err := browserOpenCommandParts("linux", "", "http://localhost:1234/") - if err != nil { - t.Fatalf("browserOpenCommandParts returned error: %v", err) - } - want := []string{"firefox", "http://localhost:1234/"} - if len(got) != len(want) { - t.Fatalf("len(parts) = %d, want %d", len(got), len(want)) - } - for i := range want { - if got[i] != want[i] { - t.Fatalf("parts[%d] = %q, want %q", i, got[i], want[i]) - } - } -} - -func TestBrowserOpenCommandPartsUsesDarwinDefault(t *testing.T) { - got, err := browserOpenCommandParts("darwin", "", "http://localhost:1234/") - if err != nil { - t.Fatalf("browserOpenCommandParts returned error: %v", err) - } - want := []string{"open", "-a", "Firefox", "http://localhost:1234/"} - if len(got) != len(want) { - t.Fatalf("len(parts) = %d, want %d", len(got), len(want)) - } - for i := range want { - if got[i] != want[i] { - t.Fatalf("parts[%d] = %q, want %q", i, got[i], want[i]) - } +func TestBrowserOpenCommandPartsRequiresCommand(t *testing.T) { + _, err := browserOpenCommandParts("", "http://localhost:1234/") + if err == nil { + t.Fatalf("expected error for empty open command") } } -func TestBrowserOpenCommandPartsOverrideAppendsURL(t *testing.T) { - got, err := browserOpenCommandParts("linux", "chromium --new-window", "http://localhost:1234/") +func TestBrowserOpenCommandPartsAppendsURLWhenMissing(t *testing.T) { + got, err := browserOpenCommandParts("chromium --new-window", "http://localhost:1234/") if err != nil { t.Fatalf("browserOpenCommandParts returned error: %v", err) } @@ -53,8 +28,8 @@ func TestBrowserOpenCommandPartsOverrideAppendsURL(t *testing.T) { } } -func TestBrowserOpenCommandPartsOverrideReplacesPlaceholder(t *testing.T) { - got, err := browserOpenCommandParts("linux", "open-browser --target={url}", "http://localhost:1234/") +func TestBrowserOpenCommandPartsReplacesURLPlaceholder(t *testing.T) { + got, err := browserOpenCommandParts("open-browser --target={url}", "http://localhost:1234/") if err != nil { t.Fatalf("browserOpenCommandParts returned error: %v", err) } @@ -69,7 +44,7 @@ func TestBrowserOpenCommandPartsOverrideReplacesPlaceholder(t *testing.T) { } } -func TestMaybeOpenLiveBrowserDisabledSkipsOpen(t *testing.T) { +func TestMaybeOpenLiveBrowserWithoutCommandSkipsOpen(t *testing.T) { called := false orig := openBrowserURLFn openBrowserURLFn = func(url, openCommand string) error { @@ -78,16 +53,16 @@ func TestMaybeOpenLiveBrowserDisabledSkipsOpen(t *testing.T) { } t.Cleanup(func() { openBrowserURLFn = orig }) - err := maybeOpenLiveBrowser("http://localhost:1234/", LiveServerOptions{AutoOpenBrowser: false}) + err := maybeOpenLiveBrowser("http://localhost:1234/", LiveServerOptions{}) if err != nil { t.Fatalf("maybeOpenLiveBrowser returned error: %v", err) } if called { - t.Fatalf("expected browser opener not to be called when disabled") + t.Fatalf("expected browser opener not to be called without open command") } } -func TestMaybeOpenLiveBrowserEnabledCallsOpen(t *testing.T) { +func TestMaybeOpenLiveBrowserWithCommandCallsOpen(t *testing.T) { called := false orig := openBrowserURLFn openBrowserURLFn = func(url, openCommand string) error { @@ -103,8 +78,7 @@ func TestMaybeOpenLiveBrowserEnabledCallsOpen(t *testing.T) { t.Cleanup(func() { openBrowserURLFn = orig }) err := maybeOpenLiveBrowser("http://localhost:1234/", LiveServerOptions{ - AutoOpenBrowser: true, - OpenCommand: "chromium", + OpenCommand: "chromium", }) if err != nil { t.Fatalf("maybeOpenLiveBrowser returned error: %v", err) @@ -122,7 +96,7 @@ func TestMaybeOpenLiveBrowserPropagatesOpenError(t *testing.T) { t.Cleanup(func() { openBrowserURLFn = orig }) err := maybeOpenLiveBrowser("http://localhost:1234/", LiveServerOptions{ - AutoOpenBrowser: true, + OpenCommand: "chromium", }) if err == nil || err.Error() != "launch failed" { t.Fatalf("expected launch failed error, got %v", err) |
