-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner_suite_test.go
123 lines (111 loc) · 2.64 KB
/
runner_suite_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
package cmdio_test
import (
"context"
"errors"
"reflect"
"testing"
"time"
"lesiw.io/cmdio"
"lesiw.io/cmdio/ctr"
"lesiw.io/cmdio/sys"
)
var runners = map[string]*cmdio.Runner{
"sys": sys.Runner(),
"ctr": mustv(ctr.New("alpine")),
}
func TestRunners(t *testing.T) {
t.Cleanup(func() {
for _, rnr := range runners {
if err := rnr.Close(); err != nil {
panic(err)
}
}
})
for name, rnr := range runners {
suite := reflect.TypeOf(rnrtests{})
for i := 0; i < suite.NumMethod(); i++ {
test := suite.Method(i)
t.Run(name+": "+test.Name, func(t *testing.T) {
test.Func.Call([]reflect.Value{
reflect.ValueOf(rnrtests{}),
reflect.ValueOf(t),
reflect.ValueOf(rnr),
})
})
}
}
}
type rnrtests struct{}
func (rnrtests) TestPipe(t *testing.T, rnr *cmdio.Runner) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
rnr = rnr.WithContext(ctx)
r, err := cmdio.GetPipe(
rnr.Command("echo", "hello world"),
rnr.Command("tr", "a-z", "A-Z"),
)
if err != nil {
t.Error(err)
}
if got, want := r.Out, "HELLO WORLD"; got != want {
t.Errorf("[echo hello world] | [tr a-z A-Z] = %q, want %q", got, want)
}
cancel()
}
func (rnrtests) TestPipeToCompletedCommand(t *testing.T, rnr *cmdio.Runner) {
err := cmdio.Pipe(
rnr.Command("sleep", "0.001"),
rnr.Command("true"),
)
if err != nil {
t.Errorf("want <nil>, got %q", err.Error())
}
}
func (rnrtests) TestPipeToFailedCommand(t *testing.T, rnr *cmdio.Runner) {
err := cmdio.Pipe(
rnr.Command("sleep", "0.001"),
rnr.Command("false"),
)
if err == nil {
t.Error("want err, got <nil>")
}
}
func (rnrtests) TestEnv(t *testing.T, rnr *cmdio.Runner) {
rnr = rnr.WithEnv(map[string]string{
"TEST_ENV": "testenv",
})
if got, want := rnr.Env("TEST_ENV"), "testenv"; got != want {
t.Errorf("Env(TEST_ENV) = %q, want %q", got, want)
}
}
func (rnrtests) TestContext(t *testing.T, rnr *cmdio.Runner) {
ctx, cancel := context.WithCancel(context.Background())
rnr = rnr.WithContext(ctx)
ch := make(chan struct{})
go func() {
_, err := rnr.Get("sleep", "5")
if err == nil {
t.Error("Get(sleep 5) err = <nil>, want context.Canceled")
} else if !errors.Is(err, context.Canceled) {
t.Errorf("Get(sleep 5) err = %q, want context.Canceled", err)
}
ch <- struct{}{}
}()
cancel()
<-ch
}
func (rnrtests) TestPwd(t *testing.T, rnr *cmdio.Runner) {
rnr = rnr.WithEnv(map[string]string{"PWD": "/tmp"})
r, err := rnr.Get("pwd")
if err != nil {
t.Fatal(err)
}
if got, want := r.Out, "/tmp"; got != want {
t.Errorf("[pwd] = %q, want %q", got, want)
}
}
func mustv[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}