52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
//go:build windows
|
|||
|
|
|
||
|
|
package files
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestWindowsExecutablePickerReturnsCleanAbsoluteExecutable(t *testing.T) {
|
||
|
|
runner := &fakeRunner{out: []byte("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\r\n")}
|
||
|
|
picker := newExecutablePickerWithRunner(runner)
|
||
|
|
got, err := picker.ChooseExecutable(context.Background())
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if got != `C:\Program Files\Google\Chrome\Application\chrome.exe` {
|
||
|
|
t.Fatalf("path = %q", got)
|
||
|
|
}
|
||
|
|
command := strings.Join(runner.args, " ")
|
||
|
|
if runner.name != "powershell.exe" || !strings.Contains(command, "-STA") || !strings.Contains(command, "OpenFileDialog") {
|
||
|
|
t.Fatalf("unexpected picker command: %s %v", runner.name, runner.args)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWindowsExecutablePickerRejectsInvalidSelection(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
out string
|
||
|
|
}{
|
||
|
|
{name: "relative", out: `chrome.exe`},
|
||
|
|
{name: "not executable", out: `C:\Browser\chrome.txt`},
|
||
|
|
}
|
||
|
|
for _, test := range tests {
|
||
|
|
t.Run(test.name, func(t *testing.T) {
|
||
|
|
picker := newExecutablePickerWithRunner(&fakeRunner{out: []byte(test.out)})
|
||
|
|
if _, err := picker.ChooseExecutable(context.Background()); err == nil {
|
||
|
|
t.Fatal("invalid selection was accepted")
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWindowsExecutablePickerTreatsEmptySelectionAsCanceled(t *testing.T) {
|
||
|
|
picker := newExecutablePickerWithRunner(&fakeRunner{})
|
||
|
|
if _, err := picker.ChooseExecutable(context.Background()); !errors.Is(err, ErrSelectionCanceled) {
|
||
|
|
t.Fatalf("error = %v", err)
|
||
|
|
}
|
||
|
|
}
|