-
Notifications
You must be signed in to change notification settings - Fork 2
/
tmux_wrapper_test.go
87 lines (80 loc) · 2.06 KB
/
tmux_wrapper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package chaakoo
import (
"errors"
"github.com/golang/mock/gomock"
"github.com/pallavJha/chaakoo/mocks"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"strings"
"testing"
)
type TmuxWrapperTestCase struct {
ID int
Error string
Ignore bool
Dimension *Dimension
SessionName string
Windows []*Window
Commands []*struct {
Name string
Args string
Stdout string
Stderr string
Err string
ExitCode int
}
}
func (c TmuxWrapperTestSuite) testTmuxWrapperApply(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var testCases []TmuxWrapperTestCase
if err := viper.UnmarshalKey("configs", &testCases); err != nil {
t.Log("unable to read from the config", err)
t.Fail()
}
for _, testCase := range testCases {
if testCase.Ignore {
continue
}
t.Log("testing, id", testCase.ID)
config := &Config{
SessionName: testCase.SessionName,
Windows: testCase.Windows,
}
err := config.Validate()
require.NoError(t, err)
err = config.Parse()
require.NoError(t, err)
wrapper := NewTmuxWrapper(config, testCase.Dimension)
mockCmdExecutor := mocks.NewMockICommandExecutor(ctrl)
wrapper.executor = mockCmdExecutor
for _, command := range testCase.Commands {
command.Args = strings.TrimSpace(command.Args)
arguments := strings.Split(command.Args, " ")
var errorToReturn error
if len(command.Err) > 0 {
errorToReturn = errors.New(command.Err)
}
adjustSendKeysArgs(arguments)
mockCmdExecutor.EXPECT().Execute(command.Name, adjustSendKeysArgs(arguments)).Return(
command.Stdout, command.Stderr, command.ExitCode, errorToReturn,
)
}
err = wrapper.Apply()
if len(testCase.Error) > 0 {
require.Error(t, err)
require.EqualError(t, err, testCase.Error)
} else {
require.NoError(t, err)
}
}
}
func adjustSendKeysArgs(args []string) []string {
if args[0] != "send-keys" {
return args
}
var newArgs = make([]string, 3)
copy(newArgs, args[0:3])
newArgs = append(newArgs, strings.Join(args[3:len(args)-1], " "), "C-m")
return newArgs
}