-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_test.go
188 lines (141 loc) · 3.44 KB
/
cmd_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
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
package hype
import (
"context"
"errors"
"strings"
"testing"
"time"
"github.com/markbates/clam"
"github.com/stretchr/testify/require"
)
func Test_NewCmd_Errors(t *testing.T) {
t.Parallel()
r := require.New(t)
ats := &Attributes{}
r.NoError(ats.Set("exec", ""))
table := []struct {
name string
el *Element
e error
}{
{name: "nil element", e: ErrIsNil("element")},
{name: "missing exec", e: ErrAttrNotFound("exec"), el: &Element{}},
{name: "empty exec", e: ErrAttrEmpty("exec"), el: &Element{
Attributes: ats,
}},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
r := require.New(t)
_, err := NewCmd(tt.el)
r.Error(err)
r.True(errors.Is(err, tt.e))
})
}
}
func Test_Cmd_Execute(t *testing.T) {
t.Parallel()
r := require.New(t)
c := &Cmd{
Element: NewEl("cmd", nil),
Args: []string{"echo", "hello"},
}
r.NoError(c.Set("exec", "echo hello"))
r.NoError(c.Set("hide-cmd", ""))
ctx := context.Background()
doc := &Document{
Parser: NewParser(nil),
}
doc.Nodes = append(doc.Nodes, c)
err := doc.Execute(ctx)
r.NoError(err)
r.NotNil(c.Result())
act := c.String()
act = strings.TrimSpace(act)
exp := `<cmd exec="echo hello" hide-cmd=""><pre><code class="language-shell" language="shell">hello</code></pre></cmd>`
// fmt.Println(act)
r.Equal(exp, act)
}
func Test_Cmd_Execute_UnexpectedExit(t *testing.T) {
t.Parallel()
r := require.New(t)
c := &Cmd{
Element: NewEl("cmd", nil),
Args: []string{"go", "run", "main.go"},
}
r.NoError(c.Set("src", "testdata/commands/bad-exit"))
ctx := context.Background()
doc := &Document{
Parser: NewParser(nil),
}
err := c.Execute(ctx, doc)
r.Error(err)
r.True(errors.Is(err, CmdError{}))
c.ExpectedExit = 1
err = c.Execute(ctx, doc)
r.NoError(err)
c.ExpectedExit = -1
err = c.Execute(ctx, doc)
r.NoError(err)
}
func Test_NewCmd(t *testing.T) {
t.Parallel()
r := require.New(t)
el := &Element{
Attributes: &Attributes{},
}
r.NoError(el.Set("environ", "foo=bar,baz=qux"))
r.NoError(el.Set("exec", "echo hello"))
r.NoError(el.Set("exit", "1"))
r.NoError(el.Set("src", "testdata/commands/bad-exit"))
r.NoError(el.Set("timeout", "10ms"))
c, err := NewCmd(el)
r.NoError(err)
r.NotNil(c)
r.Equal([]string{"echo", "hello"}, c.Args)
r.Equal([]string{"foo=bar", "baz=qux"}, c.Env)
r.Equal(1, c.ExpectedExit)
r.Equal(time.Millisecond*10, c.Timeout)
}
func Test_Cmd_Execute_Timeout(t *testing.T) {
t.Parallel()
r := require.New(t)
c := &Cmd{
Element: &Element{
Attributes: &Attributes{},
},
Args: []string{"go", "run", "main.go"},
Timeout: time.Millisecond * 5,
}
r.NoError(c.Set("src", "testdata/commands/timeout"))
ctx := context.Background()
doc := &Document{}
err := c.Execute(ctx, doc)
r.Error(err)
r.True(errors.Is(err, CmdError{}))
}
func Test_Cmd_MarshalJSON(t *testing.T) {
t.Parallel()
res := &CmdResult{
Element: NewEl("result", nil),
Result: &clam.Result{
Args: []string{"echo", "hello"},
Dir: "testdata/commands",
Env: []string{"FOO=bar", "BAR=baz"},
Err: errors.New("this is an error"),
Exit: 1,
Stderr: []byte("this is stderr"),
Stdout: []byte("this is stdout"),
Duration: time.Second,
},
}
c := &Cmd{
Element: NewEl("cmd", nil),
Args: []string{"echo", "hello"},
Env: []string{"FOO=bar", "BAR=baz"},
ExpectedExit: 1,
Timeout: time.Second,
res: res,
}
testJSON(t, "cmd", c)
}