2021-10-16 14:38:26 +02:00
|
|
|
//go:build windows
|
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package oscommands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-03-29 14:46:46 +02:00
|
|
|
"github.com/cli/safeexec"
|
2022-01-03 06:15:26 +02:00
|
|
|
"github.com/go-errors/errors"
|
2021-10-16 14:38:26 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-03 06:15:26 +02:00
|
|
|
// handling this in a separate file because str.ToArgv has different behaviour if we're on windows
|
|
|
|
|
2021-10-16 14:38:26 +02:00
|
|
|
func TestOSCommandOpenFileWindows(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
filename string
|
2021-12-31 01:44:47 +02:00
|
|
|
runner *FakeCmdObjRunner
|
2021-10-16 14:38:26 +02:00
|
|
|
test func(error)
|
|
|
|
}
|
|
|
|
|
2023-03-29 14:46:46 +02:00
|
|
|
fullCmdPath, _ := safeexec.LookPath("cmd")
|
|
|
|
|
2021-10-16 14:38:26 +02:00
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2021-12-31 01:44:47 +02:00
|
|
|
filename: "test",
|
|
|
|
runner: NewFakeRunner(t).
|
2023-03-29 14:46:46 +02:00
|
|
|
ExpectArgs([]string{fullCmdPath, "/c", "start", "", "test"}, "", errors.New("error")),
|
2021-12-31 01:44:47 +02:00
|
|
|
test: func(err error) {
|
2021-10-16 14:38:26 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:44:47 +02:00
|
|
|
filename: "test",
|
|
|
|
runner: NewFakeRunner(t).
|
2023-03-29 14:46:46 +02:00
|
|
|
ExpectArgs([]string{fullCmdPath, "/c", "start", "", "test"}, "", nil),
|
2021-12-31 01:44:47 +02:00
|
|
|
test: func(err error) {
|
2021-10-16 14:38:26 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:44:47 +02:00
|
|
|
filename: "filename with spaces",
|
|
|
|
runner: NewFakeRunner(t).
|
2023-03-29 14:46:46 +02:00
|
|
|
ExpectArgs([]string{fullCmdPath, "/c", "start", "", "filename with spaces"}, "", nil),
|
2021-12-31 01:44:47 +02:00
|
|
|
test: func(err error) {
|
2021-10-16 14:38:26 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:44:47 +02:00
|
|
|
filename: "let's_test_with_single_quote",
|
|
|
|
runner: NewFakeRunner(t).
|
2023-03-29 14:46:46 +02:00
|
|
|
ExpectArgs([]string{fullCmdPath, "/c", "start", "", "let's_test_with_single_quote"}, "", nil),
|
2021-12-31 01:44:47 +02:00
|
|
|
test: func(err error) {
|
2021-10-16 14:38:26 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-31 01:44:47 +02:00
|
|
|
filename: "$USER.txt",
|
|
|
|
runner: NewFakeRunner(t).
|
2023-03-29 14:46:46 +02:00
|
|
|
ExpectArgs([]string{fullCmdPath, "/c", "start", "", "$USER.txt"}, "", nil),
|
2021-12-31 01:44:47 +02:00
|
|
|
test: func(err error) {
|
2021-10-16 14:38:26 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
2021-12-31 01:44:47 +02:00
|
|
|
oSCmd := NewDummyOSCommandWithRunner(s.runner)
|
|
|
|
platform := &Platform{
|
|
|
|
OS: "windows",
|
|
|
|
Shell: "cmd",
|
|
|
|
ShellArg: "/c",
|
|
|
|
}
|
|
|
|
oSCmd.Platform = platform
|
|
|
|
oSCmd.Cmd.platform = platform
|
|
|
|
oSCmd.UserConfig.OS.OpenCommand = `start "" {{filename}}`
|
2021-10-16 14:38:26 +02:00
|
|
|
|
2021-12-31 01:44:47 +02:00
|
|
|
s.test(oSCmd.OpenFile(s.filename))
|
2021-10-16 14:38:26 +02:00
|
|
|
}
|
|
|
|
}
|