-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcmdio_test.go
More file actions
executable file
·238 lines (203 loc) · 5.35 KB
/
cmdio_test.go
File metadata and controls
executable file
·238 lines (203 loc) · 5.35 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Copyright © 2020 streamz <bytecodenerd@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmdio
import (
"bytes"
"context"
"io"
"os"
"os/user"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
_, b, _, _ = runtime.Caller(0)
Testdata = filepath.Dir(b) + "/testdata/"
)
type stringReader struct {
data []string
step int
}
func (r stringReader) Read(p []byte) (n int, err error) {
if r.step < len(r.data) {
s := r.data[r.step]
n = copy(p, s)
r.step++
} else {
err = io.EOF
}
return
}
func stdOptions() *Options {
usr, _ := user.Current()
return &Options{
In: os.Stdin,
Out: os.Stdout,
Err: os.Stderr,
Env: append([]string{"CMDIO_TEST=running test"}, os.Environ()...),
Usr: usr,
}
}
func bufOptions(in io.Reader, out, err io.Writer) func() *Options {
usr, _ := user.Current()
return func() *Options {
return &Options{
In: in,
Out: out,
Err: err,
Usr: usr,
}
}
}
func run(t *testing.T, script string, optFn func() *Options) *Info {
t.Helper()
return New(optFn).Run(Testdata + script)
}
func start(t *testing.T, script string, optFn func() *Options) *Info {
t.Helper()
_, ctx := New(optFn).Start(Testdata + script)
info := <-ctx
return &info
}
func terminate(t *testing.T, script string, optFn func() *Options) (*Info, error) {
t.Helper()
cmd := New(optFn)
started, ctx := cmd.Start(Testdata + script)
<-started
time.Sleep(time.Second)
// terminate the process
err := cmd.Terminate()
info := <-ctx
return &info, err
}
func TestRun(t *testing.T) {
t.Run("basic_run", func(t *testing.T) {
info := run(t, "program.sh", stdOptions)
assert.NoError(t, info.Error)
assert.Equal(t, 0, info.Exit)
assert.True(t, info.Finished)
})
}
func TestReRun(t *testing.T) {
t.Run("cannot_reuse_cmdio", func(t *testing.T) {
cmd := New(stdOptions)
info := cmd.Run(Testdata + "program.sh")
assert.NoError(t, info.Error)
info = cmd.Run(Testdata + "program.sh")
assert.Error(t, info.Error)
assert.Contains(t, info.Error.Error(), "cannot reuse CmdIo")
})
}
func TestStart(t *testing.T) {
t.Run("async_start", func(t *testing.T) {
info := start(t, "program.sh", stdOptions)
assertStart(t, info)
})
}
func TestTerminate(t *testing.T) {
t.Run("terminate_running", func(t *testing.T) {
info, err := terminate(t, "service.sh", stdOptions)
assert.NoError(t, err)
assertTerminate(t, info)
})
}
func TestStartBufIO(t *testing.T) {
t.Run("io_redirection", func(t *testing.T) {
expected := "hello world\n"
in := stringReader{
step: 0,
data: []string{expected},
}
out := bytes.NewBufferString("")
err := bytes.NewBufferString("")
cmd := New(bufOptions(in, out, err))
info := cmd.Run(Testdata+"io.sh", "-")
assert.NoError(t, info.Error)
assert.Equal(t, expected, out.String())
assert.Equal(t, expected, err.String())
})
}
func assertTerminate(t *testing.T, info *Info) {
assert.Error(t, info.Error)
assert.False(t, info.Finished, "info should not be finished")
assert.True(t, info.Signaled, "info should be Signaled")
assert.Equal(
t,
15,
info.Exit,
"should exit with 15")
}
func assertStart(t *testing.T, info *Info) {
assert.NoError(t, info.Error)
assert.True(t, info.Finished, "info should be finished")
assert.False(t, info.Signaled, "info should not be Signaled")
assert.Equal(
t,
0,
info.Exit,
"should exit with 0")
}
func TestRunContext(t *testing.T) {
t.Run("context_cancellation", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
cmd := New(stdOptions)
info := cmd.RunContext(ctx, Testdata+"service.sh")
// Command should be terminated due to context timeout
assert.True(t, info.Signaled || info.Error != nil)
})
t.Run("context_not_cancelled", func(t *testing.T) {
ctx := context.Background()
cmd := New(stdOptions)
info := cmd.RunContext(ctx, Testdata+"program.sh")
assert.NoError(t, info.Error)
assert.Equal(t, 0, info.Exit)
assert.True(t, info.Finished)
})
}
func TestStartContext(t *testing.T) {
t.Run("start_with_context", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
cmd := New(stdOptions)
started, complete := cmd.StartContext(ctx, Testdata+"service.sh")
<-started
info := <-complete
// Command should be terminated due to context timeout
assert.True(t, info.Signaled || info.Error != nil)
})
}
// Benchmarks
func BenchmarkRun(b *testing.B) {
for i := 0; i < b.N; i++ {
cmd := New(stdOptions)
_ = cmd.Run(Testdata + "program.sh")
}
}
func BenchmarkStart(b *testing.B) {
for i := 0; i < b.N; i++ {
cmd := New(stdOptions)
_, complete := cmd.Start(Testdata + "program.sh")
<-complete
}
}
func BenchmarkRunContext(b *testing.B) {
ctx := context.Background()
for i := 0; i < b.N; i++ {
cmd := New(stdOptions)
_ = cmd.RunContext(ctx, Testdata+"program.sh")
}
}