Merge pull request #1708 from cli/wsl-detection
Fallback browser when `xdg-open` does not exist
This commit is contained in:
commit
cd32ef8389
2 changed files with 37 additions and 1 deletions
|
|
@ -30,7 +30,7 @@ func ForOS(goos, url string) *exec.Cmd {
|
|||
r := strings.NewReplacer("&", "^&")
|
||||
args = append(args, "/c", "start", r.Replace(url))
|
||||
default:
|
||||
exe = "xdg-open"
|
||||
exe = linuxExe()
|
||||
args = append(args, url)
|
||||
}
|
||||
|
||||
|
|
@ -51,3 +51,19 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) {
|
|||
cmd.Stderr = os.Stderr
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func linuxExe() string {
|
||||
exe := "xdg-open"
|
||||
|
||||
_, err := lookPath(exe)
|
||||
if err != nil {
|
||||
_, err := lookPath("wslview")
|
||||
if err == nil {
|
||||
exe = "wslview"
|
||||
}
|
||||
}
|
||||
|
||||
return exe
|
||||
}
|
||||
|
||||
var lookPath = exec.LookPath
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package browser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
|
@ -13,6 +14,7 @@ func TestForOS(t *testing.T) {
|
|||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
exe string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
|
|
@ -29,8 +31,18 @@ func TestForOS(t *testing.T) {
|
|||
goos: "linux",
|
||||
url: "https://example.com/path?a=1&b=2",
|
||||
},
|
||||
exe: "xdg-open",
|
||||
want: []string{"xdg-open", "https://example.com/path?a=1&b=2"},
|
||||
},
|
||||
{
|
||||
name: "WSL",
|
||||
args: args{
|
||||
goos: "linux",
|
||||
url: "https://example.com/path?a=1&b=2",
|
||||
},
|
||||
exe: "wslview",
|
||||
want: []string{"wslview", "https://example.com/path?a=1&b=2"},
|
||||
},
|
||||
{
|
||||
name: "Windows",
|
||||
args: args{
|
||||
|
|
@ -41,6 +53,14 @@ func TestForOS(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
lookPath = func(file string) (string, error) {
|
||||
if file == tt.exe {
|
||||
return file, nil
|
||||
} else {
|
||||
return "", errors.New("not found")
|
||||
}
|
||||
}
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if cmd := ForOS(tt.args.goos, tt.args.url); !reflect.DeepEqual(cmd.Args, tt.want) {
|
||||
t.Errorf("ForOS() = %v, want %v", cmd.Args, tt.want)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue