Fix up structure for better testing

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

View file

@ -52,15 +52,18 @@ func FromLauncher(launcher, url string) (*exec.Cmd, error) {
return cmd, nil
}
var linuxExe = func() string {
func linuxExe() string {
exe := "xdg-open"
if !findExe("xdg-open") && findExe("wslview") {
exe = "wslview"
_, err := lookPath(exe)
if err != nil {
_, err := lookPath("wslview")
if err == nil {
exe = "wslview"
}
}
return exe
}
func findExe(command string) bool {
_, err := exec.LookPath(command)
return err == nil
}
var lookPath = exec.LookPath

View file

@ -1,6 +1,7 @@
package browser
import (
"errors"
"reflect"
"testing"
)
@ -52,8 +53,15 @@ 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) {
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)
}