From 30d8fa0a2781b6b4cc2b9c0d4301b47c95a13d05 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 19 Dec 2020 20:58:39 +0100 Subject: Add `BROWSER` support to `OpenURL()` If the `BROWSER` environment variable is set, open the given URL using the command specified in the variable. Run the command in a shell process, either from the `SHELL` environment variable or `/bin/sh` on Unix OSes. Run it through `cmd` on Windows. Essentially repurposes my code from https://github.com/pkg/browser/pull/14. --- browserenv_unix_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 browserenv_unix_test.go (limited to 'browserenv_unix_test.go') diff --git a/browserenv_unix_test.go b/browserenv_unix_test.go new file mode 100644 index 0000000..bcc7c2d --- /dev/null +++ b/browserenv_unix_test.go @@ -0,0 +1,29 @@ +// +build !windows + +package browserenv + +import ( + "fmt" + "os" + "reflect" + "testing" +) + +func TestBrowserCommand(t *testing.T) { + envValue := "open -a Firefox" + url := "https://duckduckgo.com" + + cmd := browserCommand(envValue, url) + + shell := os.Getenv("SHELL") + if shell == "" { + shell = "/bin/sh" + } + + browserCommand := fmt.Sprintf("%s '%s'", envValue, url) + + wantArgs := []string{shell, "-c", browserCommand} + if !reflect.DeepEqual(cmd.Args, wantArgs) { + t.Errorf("got args '%#v' want '%#v'", cmd.Args, wantArgs) + } +} -- cgit v1.2.3