-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
109 lines (92 loc) · 2.51 KB
/
main_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
package main
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
"github.com/abhinav/tmux-fastcopy/internal/envtest"
"github.com/abhinav/tmux-fastcopy/internal/log"
"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 TestVersion(t *testing.T) {
t.Parallel()
var stdout, stderr bytes.Buffer
defer func() {
assert.Empty(t, stderr.String(), "stderr should be empty")
}()
err := run(&mainCmd{
Stdout: &stdout,
Stderr: &stderr,
Getenv: envtest.Empty.Getenv,
}, []string{"-version"})
require.NoError(t, err)
assert.Contains(t, stdout.String(), _version)
}
type fakeTmux struct{ tmux.Driver }
func (fakeTmux) SetLogger(*log.Logger) {}
func TestMainParentSignal(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
mockTmux := tmuxtest.NewMockDriver(ctrl)
var stdout, stderr bytes.Buffer
defer func() {
assert.Empty(t, stdout.String(), "stdout must be empty")
assert.Empty(t, stderr.String(), "stderr must be empty")
}()
mockTmux.EXPECT().
SendSignal(_signalPrefix + "42").
Return(nil)
err := (&mainCmd{
Stdout: io.Discard,
Stderr: &stderr,
Getenv: envtest.MustPairs(_parentPIDEnv, "42").Getenv,
newTmuxDriver: func(string) tmuxShellDriver {
return fakeTmux{mockTmux}
},
runTarget: func(interface{ Run(*config) error }, *config) error {
return nil
},
}).Run(&config{})
assert.NoError(t, err)
}
func TestMainTargetPanicWithLog(t *testing.T) {
t.Parallel()
logfile := filepath.Join(t.TempDir(), "log.txt")
var stdout, stderr bytes.Buffer
defer func() {
assert.Empty(t, stdout.String(), "stdout must be empty")
assert.Empty(t, stderr.String(), "stderr must be empty")
}()
ctrl := gomock.NewController(t)
mockTmux := tmuxtest.NewMockDriver(ctrl)
called := false
defer func() {
assert.True(t, called, "runTarget was called")
}()
runTarget := func(interface{ Run(*config) error }, *config) error {
called = true
panic("great sadness")
}
err := (&mainCmd{
Stdout: &stdout,
Stderr: &stderr,
Getenv: envtest.Empty.Getenv,
Getpid: func() int { return 42 },
newTmuxDriver: func(string) tmuxShellDriver {
return fakeTmux{mockTmux}
},
runTarget: runTarget,
}).Run(&config{
LogFile: logfile,
})
require.Error(t, err)
assert.Contains(t, err.Error(), "great sadness")
body, err := os.ReadFile(logfile)
require.NoError(t, err)
assert.Contains(t, string(body), "panic: great sadness")
}