-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_test.go
42 lines (37 loc) · 1.05 KB
/
action_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
package main
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Action_Execute_Errors(t *testing.T) {
noOutput := ""
kases := []struct {
statement string
errmsg string
output string
}{
// command prints error
{"bash -c \"echo foo; exit 1\"", "exit status 1", "foo\n"},
}
for _, kase := range kases {
logger := NewMockLogger()
action := NewAction(kase.statement).WithStdout(logger)
assert.ErrorContains(t, action.Execute(), kase.errmsg)
if kase.output != noOutput {
assert.Contains(t, logger.Outputs(), kase.output)
}
}
}
func Test_Action_Can_Accept_StandardInput(t *testing.T) {
logger := NewMockLogger()
b := bytes.NewBufferString("hello\n")
// we need to disable env expansion in statement so that `$s` is not replaced
// with empty space (`$s` will be set during command execution, not before)
action := NewAction("bash -c \"read s && echo $s\"").
WithStdin(b).
WithEnvExpansion(false).
WithStdout(logger)
assert.NoError(t, action.Execute())
assert.Contains(t, logger.Outputs(), "hello\n")
}