summaryrefslogtreecommitdiffstats
path: root/pkg/commands/os_test.go
diff options
context:
space:
mode:
authorAnthony HAMON <anthony.hamon@iadvize.com>2018-08-22 22:31:43 +0200
committerAnthony HAMON <anthony.hamon@iadvize.com>2018-08-26 01:58:20 +0200
commited2dcd9e461d99b7969dee73fb96233947ee0ecf (patch)
tree8e4c465654f9bd759735882c9664f6baf3534c0b /pkg/commands/os_test.go
parent75e08993eacb88e98df30e1ee4dff500d2c19a3d (diff)
add tests
Diffstat (limited to 'pkg/commands/os_test.go')
-rw-r--r--pkg/commands/os_test.go221
1 files changed, 221 insertions, 0 deletions
diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go
index 9d5cc1354..d78391d17 100644
--- a/pkg/commands/os_test.go
+++ b/pkg/commands/os_test.go
@@ -1,6 +1,7 @@
package commands
import (
+ "os/exec"
"testing"
"github.com/stretchr/testify/assert"
@@ -57,6 +58,226 @@ func TestOSCommandRunCommand(t *testing.T) {
}
}
+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))
+ }
+}
+
func TestOSCommandQuote(t *testing.T) {
osCommand := newDummyOSCommand()