2018-08-16 05:55:55 +02:00
|
|
|
package commands
|
|
|
|
|
2018-08-21 23:17:44 +02:00
|
|
|
import (
|
2018-08-22 22:31:43 +02:00
|
|
|
"os/exec"
|
2018-08-21 23:17:44 +02:00
|
|
|
"testing"
|
2018-08-16 05:55:55 +02:00
|
|
|
|
2018-08-21 23:17:44 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-08-22 22:30:02 +02:00
|
|
|
func newDummyOSCommand() *OSCommand {
|
|
|
|
return NewOSCommand(newDummyLog())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOSCommandRunCommandWithOutput(t *testing.T) {
|
2018-08-21 23:17:44 +02:00
|
|
|
type scenario struct {
|
|
|
|
command string
|
|
|
|
test func(string, error)
|
2018-08-16 05:55:55 +02:00
|
|
|
}
|
2018-08-21 23:17:44 +02:00
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"echo -n '123'",
|
|
|
|
func(output string, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, "123", output)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"rmdir unexisting-folder",
|
|
|
|
func(output string, err error) {
|
|
|
|
assert.Regexp(t, ".*No such file or directory.*", err.Error())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
2018-08-22 22:30:02 +02:00
|
|
|
s.test(newDummyOSCommand().RunCommandWithOutput(s.command))
|
2018-08-21 23:17:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:30:02 +02:00
|
|
|
func TestOSCommandRunCommand(t *testing.T) {
|
2018-08-21 23:17:44 +02:00
|
|
|
type scenario struct {
|
|
|
|
command string
|
|
|
|
test func(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"rmdir unexisting-folder",
|
|
|
|
func(err error) {
|
|
|
|
assert.Regexp(t, ".*No such file or directory.*", err.Error())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
2018-08-22 22:30:02 +02:00
|
|
|
s.test(newDummyOSCommand().RunCommand(s.command))
|
2018-08-16 05:55:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 23:17:44 +02:00
|
|
|
|
2018-08-22 22:31:43 +02:00
|
|
|
func TestOSCommandGetOpenCommand(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
command func(string, ...string) *exec.Cmd
|
|
|
|
test func(string, string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
return exec.Command("exit", "1")
|
|
|
|
},
|
|
|
|
func(name string, trail string, err error) {
|
|
|
|
assert.EqualError(t, err, "Unsure what command to use to open this file")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
assert.Equal(t, "which", name)
|
|
|
|
assert.Len(t, arg, 1)
|
|
|
|
assert.Regexp(t, "xdg-open|cygstart|open", arg[0])
|
|
|
|
return exec.Command("echo")
|
|
|
|
},
|
|
|
|
func(name string, trail string, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Regexp(t, "xdg-open|cygstart|open", name)
|
|
|
|
assert.Regexp(t, " \\&\\>/dev/null \\&|", trail)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
OSCmd := newDummyOSCommand()
|
|
|
|
OSCmd.command = s.command
|
|
|
|
|
|
|
|
s.test(OSCmd.getOpenCommand())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOSCommandOpenFile(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
filename string
|
|
|
|
command func(string, ...string) *exec.Cmd
|
|
|
|
test func(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
return exec.Command("exit", "1")
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
assert.EqualError(t, err, "Unsure what command to use to open this file")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
if name == "which" {
|
|
|
|
return exec.Command("echo")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(arg) {
|
|
|
|
case 1:
|
|
|
|
assert.Regexp(t, "open|cygstart", name)
|
|
|
|
assert.EqualValues(t, "test", arg[0])
|
|
|
|
case 3:
|
|
|
|
assert.Equal(t, "xdg-open", name)
|
|
|
|
assert.EqualValues(t, "test", arg[0])
|
|
|
|
assert.Regexp(t, " \\&\\>/dev/null \\&|", arg[1])
|
|
|
|
assert.EqualValues(t, "&", arg[2])
|
|
|
|
default:
|
|
|
|
assert.Fail(t, "Unexisting command given")
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec.Command("echo")
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
OSCmd := newDummyOSCommand()
|
|
|
|
OSCmd.command = s.command
|
|
|
|
|
|
|
|
s.test(OSCmd.OpenFile(s.filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOSCommandEditFile(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
filename string
|
|
|
|
command func(string, ...string) *exec.Cmd
|
|
|
|
getenv func(string) string
|
|
|
|
getGlobalGitConfig func(string) (string, error)
|
|
|
|
test func(*exec.Cmd, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
return exec.Command("exit", "1")
|
|
|
|
},
|
|
|
|
func(env string) string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
func(cf string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
func(cmd *exec.Cmd, err error) {
|
|
|
|
assert.EqualError(t, err, "No editor defined in $VISUAL, $EDITOR, or git config")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
if name == "which" {
|
|
|
|
return exec.Command("exit", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, "nano", name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(env string) string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
func(cf string) (string, error) {
|
|
|
|
return "nano", nil
|
|
|
|
},
|
|
|
|
func(cmd *exec.Cmd, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
if name == "which" {
|
|
|
|
return exec.Command("exit", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, "nano", name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(env string) string {
|
|
|
|
if env == "VISUAL" {
|
|
|
|
return "nano"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
func(cf string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
func(cmd *exec.Cmd, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
if name == "which" {
|
|
|
|
return exec.Command("exit", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, "emacs", name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(env string) string {
|
|
|
|
if env == "EDITOR" {
|
|
|
|
return "emacs"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
func(cf string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
func(cmd *exec.Cmd, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
func(name string, arg ...string) *exec.Cmd {
|
|
|
|
if name == "which" {
|
|
|
|
return exec.Command("echo")
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.EqualValues(t, "vi", name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
func(env string) string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
func(cf string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
},
|
|
|
|
func(cmd *exec.Cmd, err error) {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
|
|
|
OSCmd := newDummyOSCommand()
|
|
|
|
OSCmd.command = s.command
|
|
|
|
OSCmd.getGlobalGitConfig = s.getGlobalGitConfig
|
|
|
|
OSCmd.getenv = s.getenv
|
|
|
|
|
|
|
|
s.test(OSCmd.EditFile(s.filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:30:02 +02:00
|
|
|
func TestOSCommandQuote(t *testing.T) {
|
|
|
|
osCommand := newDummyOSCommand()
|
2018-08-21 23:17:44 +02:00
|
|
|
|
|
|
|
actual := osCommand.Quote("hello `test`")
|
|
|
|
|
|
|
|
expected := osCommand.Platform.escapedQuote + "hello \\`test\\`" + osCommand.Platform.escapedQuote
|
|
|
|
|
|
|
|
assert.EqualValues(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:30:02 +02:00
|
|
|
func TestOSCommandUnquote(t *testing.T) {
|
|
|
|
osCommand := newDummyOSCommand()
|
2018-08-21 23:17:44 +02:00
|
|
|
|
|
|
|
actual := osCommand.Unquote(`hello "test"`)
|
|
|
|
|
|
|
|
expected := "hello test"
|
|
|
|
|
|
|
|
assert.EqualValues(t, expected, actual)
|
|
|
|
}
|