2021-10-16 14:38:26 +02:00
|
|
|
//go:build !windows
|
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package oscommands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-12-31 01:44:47 +02:00
|
|
|
"github.com/go-errors/errors"
|
2021-10-16 14:38:26 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-06-11 04:42:16 +02:00
|
|
|
func TestOSCommandRunWithOutput(t *testing.T) {
|
|
|
|
type scenario struct {
|
2023-05-21 09:00:29 +02:00
|
|
|
args []string
|
|
|
|
test func(string, error)
|
2022-06-11 04:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2023-05-21 09:00:29 +02:00
|
|
|
[]string{"echo", "-n", "123"},
|
2022-06-11 04:42:16 +02:00
|
|
|
func(output string, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, "123", output)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-05-21 09:00:29 +02:00
|
|
|
[]string{"rmdir", "unexisting-folder"},
|
2022-06-11 04:42:16 +02:00
|
|
|
func(output string, err error) {
|
|
|
|
assert.Regexp(t, "rmdir.*unexisting-folder.*", err.Error())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
c := NewDummyOSCommand()
|
2023-05-21 09:00:29 +02:00
|
|
|
s.test(c.Cmd.New(s.args).RunWithOutput())
|
2022-06-11 04:42:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:38:26 +02:00
|
|
|
func TestOSCommandOpenFileDarwin(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2021-12-31 01:44:47 +02:00
|
|
|
filename: "test",
|
|
|
|
runner: NewFakeRunner(t).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `open "test"`}, "", errors.New("error")),
|
|
|
|
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).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `open "test"`}, "", nil),
|
|
|
|
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).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `open "filename with spaces"`}, "", nil),
|
|
|
|
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)
|
|
|
|
oSCmd.Platform.OS = "darwin"
|
2023-03-28 18:00:40 +02:00
|
|
|
oSCmd.UserConfig.OS.Open = "open {{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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestOSCommandOpenFileLinux tests the OpenFile command on Linux
|
|
|
|
func TestOSCommandOpenFileLinux(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
2021-12-31 01:44:47 +02:00
|
|
|
filename: "test",
|
|
|
|
runner: NewFakeRunner(t).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `xdg-open "test" > /dev/null`}, "", errors.New("error")),
|
|
|
|
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).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `xdg-open "test" > /dev/null`}, "", nil),
|
|
|
|
test: func(err error) {
|
|
|
|
assert.NoError(t, err)
|
2021-10-16 14:38:26 +02:00
|
|
|
},
|
2021-12-31 01:44:47 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
filename: "filename with spaces",
|
|
|
|
runner: NewFakeRunner(t).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `xdg-open "filename with spaces" > /dev/null`}, "", nil),
|
|
|
|
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).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `xdg-open "let's_test_with_single_quote" > /dev/null`}, "", nil),
|
|
|
|
test: func(err error) {
|
|
|
|
assert.NoError(t, err)
|
2021-10-16 14:38:26 +02:00
|
|
|
},
|
2021-12-31 01:44:47 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
filename: "$USER.txt",
|
|
|
|
runner: NewFakeRunner(t).
|
|
|
|
ExpectArgs([]string{"bash", "-c", `xdg-open "\$USER.txt" > /dev/null`}, "", nil),
|
|
|
|
test: func(err error) {
|
2021-10-16 14:38:26 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
2021-12-31 01:44:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
oSCmd := NewDummyOSCommandWithRunner(s.runner)
|
|
|
|
oSCmd.Platform.OS = "linux"
|
2023-03-28 18:00:40 +02:00
|
|
|
oSCmd.UserConfig.OS.Open = `xdg-open {{filename}} > /dev/null`
|
2021-12-31 01:44:47 +02:00
|
|
|
|
|
|
|
s.test(oSCmd.OpenFile(s.filename))
|
|
|
|
}
|
|
|
|
}
|