Clean up linux logic to have better default logic

This commit is contained in:
Sam Coe 2020-09-16 16:56:47 +02:00
parent 7551139caf
commit fd1d09dfc2
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
2 changed files with 19 additions and 15 deletions

View file

@ -30,11 +30,7 @@ func ForOS(goos, url string) *exec.Cmd {
r := strings.NewReplacer("&", "^&")
args = append(args, "/c", "start", r.Replace(url))
default:
if findExe("wslview") {
exe = "wslview"
} else {
exe = "xdg-open"
}
exe = linuxExe()
args = append(args, url)
}
@ -56,7 +52,15 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) {
return cmd, nil
}
var findExe = func(command string) bool {
var linuxExe = func() string {
exe := "xdg-open"
if !findExe("xdg-open") && findExe("wslview") {
exe = "wslview"
}
return exe
}
func findExe(command string) bool {
_, err := exec.LookPath(command)
return err == nil
}

View file

@ -11,10 +11,10 @@ func TestForOS(t *testing.T) {
url string
}
tests := []struct {
name string
args args
findExe bool
want []string
name string
args args
exe string
want []string
}{
{
name: "macOS",
@ -30,8 +30,8 @@ func TestForOS(t *testing.T) {
goos: "linux",
url: "https://example.com/path?a=1&b=2",
},
findExe: false, // wslview does not exist on standard Linux
want: []string{"xdg-open", "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",
@ -39,8 +39,8 @@ func TestForOS(t *testing.T) {
goos: "linux",
url: "https://example.com/path?a=1&b=2",
},
findExe: true, // wslview exists on WSL
want: []string{"wslview", "https://example.com/path?a=1&b=2"},
exe: "wslview",
want: []string{"wslview", "https://example.com/path?a=1&b=2"},
},
{
name: "Windows",
@ -53,7 +53,7 @@ func TestForOS(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
findExe = func(string) bool { return tt.findExe }
linuxExe = func() string { return tt.exe }
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)
}