-
Notifications
You must be signed in to change notification settings - Fork 5
/
wrap_test.go
151 lines (131 loc) · 3.44 KB
/
wrap_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"strings"
"testing"
"github.com/abhinav/tmux-fastcopy/internal/envtest"
"github.com/abhinav/tmux-fastcopy/internal/iotest"
"github.com/abhinav/tmux-fastcopy/internal/log/logtest"
"github.com/abhinav/tmux-fastcopy/internal/tmux"
"github.com/abhinav/tmux-fastcopy/internal/tmux/tmuxtest"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWrapper(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
tests := []struct {
desc string
paneInfo tmux.PaneInfo
options []string // options reported by tmux show-options
wantConfig config
}{
{
desc: "minimal",
paneInfo: tmux.PaneInfo{
ID: "%1",
Width: 80,
Height: 40,
},
wantConfig: config{
Pane: "%1",
Tmux: "tmux",
},
},
{
desc: "has options",
options: []string{
"@fastcopy-action pbcopy",
"@fastcopy-alphabet asdfghjkl",
},
paneInfo: tmux.PaneInfo{
ID: "%3",
Width: 80,
Height: 40,
},
wantConfig: config{
Pane: "%3",
Action: "pbcopy",
Alphabet: alphabet("asdfghjkl"),
Tmux: "tmux",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
mockTmux := tmuxtest.NewMockDriver(ctrl)
mockTmux.EXPECT().NewSession(gomock.Any()).
Do(func(req tmux.NewSessionRequest) {
fset := flag.NewFlagSet(_name, flag.ContinueOnError)
fset.SetOutput(iotest.Writer(t))
var gotConfig config
gotConfig.RegisterFlags(fset)
require.NoError(t, fset.Parse(req.Command[1:]))
// zero out log file for comparison.
if assert.NotEmpty(t, gotConfig.LogFile, "log file must be specified") {
gotConfig.LogFile = ""
}
assert.Equal(t, tt.wantConfig, gotConfig)
})
mockTmux.EXPECT().WaitForSignal(gomock.Any())
mockTmux.EXPECT().ShowOptions(gomock.Any()).
Return([]byte(strings.Join(tt.options, "\n")+"\n"), nil)
w := wrapper{
Tmux: mockTmux,
Log: logtest.NewLogger(t),
Executable: func() (string, error) {
return _name, nil
},
Getenv: envtest.Empty.Getenv,
Getpid: func() int { return 42 },
inspectPane: func(tmux.Driver, string) (*tmux.PaneInfo, error) {
return &tt.paneInfo, nil
},
}
assert.NoError(t, w.Run(&config{}))
})
}
}
func TestWrapper_destroyUnattached(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockTmux := tmuxtest.NewMockDriver(ctrl)
mockTmux.EXPECT().ShowOptions(gomock.Any()).
Return([]byte("destroy-unattached on\n"), nil)
mockTmux.EXPECT().WaitForSignal(gomock.Any())
// destroy-unattached is set to off before we create a new session,
// and set back to on after we create a new session.
gomock.InOrder(
mockTmux.EXPECT().SetOption(tmux.SetOptionRequest{
Global: true,
Name: "destroy-unattached",
Value: "off",
}).Return(nil),
mockTmux.EXPECT().NewSession(gomock.Any()),
mockTmux.EXPECT().SetOption(tmux.SetOptionRequest{
Global: true,
Name: "destroy-unattached",
Value: "on",
}).Return(nil),
)
w := wrapper{
Tmux: mockTmux,
Log: logtest.NewLogger(t),
Executable: func() (string, error) {
return _name, nil
},
Getenv: envtest.Empty.Getenv,
Getpid: func() int { return 42 },
inspectPane: func(tmux.Driver, string) (*tmux.PaneInfo, error) {
return &tmux.PaneInfo{
ID: "%1",
Width: 80,
Height: 40,
}, nil
},
}
assert.NoError(t, w.Run(&config{}))
}