-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_test.go
More file actions
197 lines (179 loc) · 3.56 KB
/
shell_test.go
File metadata and controls
197 lines (179 loc) · 3.56 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package ppow
import (
"fmt"
"os"
"runtime"
"strings"
"syscall"
"testing"
"time"
"github.com/dottedmag/termlog"
)
type cmdTest struct {
name string
cmd string
bufferr bool
shells []string
logHas string
buffHas string
err bool
procerr bool
kill bool
}
func testCmd(t *testing.T, shell string, ct cmdTest) {
if ct.shells != nil {
issh := func() bool {
for _, v := range ct.shells {
if v == shell {
return true
}
}
return false
}()
if !issh {
t.Skip("skipping")
return
}
}
lt := termlog.NewLogTest()
exec, err := NewExecutor(shell, ct.cmd, "")
if err != nil {
t.Error(err)
return
}
type result struct {
err error
pstate *ExecState
}
ch := make(chan result)
go func() {
err, pstate := exec.Run(lt.Log.Stream(""), ct.bufferr)
ch <- result{err: err, pstate: pstate}
}()
// Wait for the first output to make sure process is running
for {
time.Sleep(100 * time.Millisecond)
if lt.String() != "" {
break
}
}
if ct.kill {
err := exec.Signal(syscall.SIGKILL)
if err != nil {
t.Errorf("Error stopping: %s", err)
return
}
time.Sleep(1 * time.Second)
}
res := <-ch
if (res.err != nil) != ct.err {
t.Errorf("Unexpected invocation error: %s", err)
}
if (res.pstate.Error != nil) != ct.procerr {
t.Errorf("Unexpected process error: %s, %s", res.pstate.Error, res.pstate.ErrOutput)
}
if ct.buffHas != "" && !strings.Contains(res.pstate.ErrOutput, ct.buffHas) {
t.Errorf("Unexpected buffer return: %s", res.pstate.ErrOutput)
}
if ct.logHas != "" && !strings.Contains(lt.String(), ct.logHas) {
t.Errorf("Unexpected log return: %s", lt.String())
}
}
var shellTests = []cmdTest{
{
name: "echosuccess",
cmd: "echo ppowtest; true",
logHas: "ppowtest",
},
{
name: "echofail",
cmd: "echo ppowtest; false",
logHas: "ppowtest",
procerr: true,
},
{
name: "unknowncmd",
cmd: "definitelynosuchcommand",
procerr: true,
},
{
name: "stderr-posix",
cmd: "echo ppowstderr >&2",
bufferr: true,
buffHas: "ppowstderr",
shells: []string{"ppow", "sh", "bash"},
},
{
name: "stderr-powershell",
cmd: "Write-Error \"ppowstderr\"",
bufferr: true,
procerr: true,
buffHas: "ppowstderr",
shells: []string{"powershell"},
},
{
name: "kill",
cmd: "echo ppowtest; echo; sleep 999999",
logHas: "ppowtest",
kill: true,
procerr: true,
},
}
func TestShells(t *testing.T) {
shellTesting = true
var shells []string
if runtime.GOOS == "windows" {
shells = []string{
"powershell",
}
} else {
shells = []string{
"sh",
"bash",
"powershell",
}
}
for _, sh := range shells {
for _, tc := range shellTests {
t.Run(
fmt.Sprintf("%s/%s", sh, tc.name),
func(t *testing.T) {
if _, err := CheckShell(sh); err != nil {
t.Skipf("skipping - %s", err)
return
}
testCmd(t, sh, tc)
},
)
}
}
}
func TestCaseInsensitivePath(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("skipping - only windows has case insensitive PATH")
}
oldpath := os.Getenv("PATH")
fixpath := func() {
os.Unsetenv("Path")
os.Setenv("PATH", oldpath)
}
defer fixpath()
os.Unsetenv("PATH")
os.Setenv("Path", fmt.Sprintf("%s%ctrigger-text", oldpath, os.PathListSeparator))
shellTesting = true
pathTest := cmdTest{
name: "path-test",
cmd: "echo $PATH",
logHas: "trigger-text",
}
sh := "ppow"
t.Run(
"ppow/path-capitalization",
func(t *testing.T) {
if _, err := CheckShell(sh); err != nil {
t.Skipf("skipping - %s", err)
}
testCmd(t, sh, pathTest)
},
)
}