-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_error_test.go
85 lines (67 loc) · 1.56 KB
/
cmd_error_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
package hype
import (
"errors"
"fmt"
"io"
"strings"
"testing"
"github.com/markbates/clam"
"github.com/stretchr/testify/require"
)
func Test_CmdError(t *testing.T) {
t.Parallel()
r := require.New(t)
oce := CmdError{
RunError: clam.RunError{
Err: ExecuteError{
Err: io.EOF,
},
},
}
wrapped := fmt.Errorf("error: %w", oce)
r.True(oce.As(&CmdError{}), oce)
r.True(oce.Is(oce), oce)
r.True(oce.Unwrap() == io.EOF, oce)
r.True(errors.As(wrapped, &CmdError{}), wrapped)
r.True(errors.As(wrapped, &ExecuteError{}), wrapped)
r.True(errors.Is(wrapped, CmdError{}), wrapped)
r.True(errors.Is(wrapped, ExecuteError{}), wrapped)
r.True(errors.Is(wrapped, io.EOF), wrapped)
err := errors.Unwrap(oce)
r.Equal(io.EOF, err)
}
func Test_CmdError_MarshalJSON(t *testing.T) {
t.Parallel()
ce := CmdError{
RunError: clam.RunError{
Args: []string{"echo", "hello"},
Env: []string{"FOO=bar", "BAR=baz"},
Err: io.EOF,
Exit: 1,
Output: []byte("foo\nbar\nbaz\n"),
Dir: "/tmp",
},
Filename: "foo.go",
}
testJSON(t, "cmd_error", ce)
}
func Test_CmdError_Error(t *testing.T) {
t.Parallel()
ce := CmdError{
RunError: clam.RunError{
Args: []string{"echo", "hello"},
Env: []string{"FOO=bar", "BAR=baz"},
Err: io.EOF,
Exit: 1,
Output: []byte("foo\nbar\nbaz\n"),
Dir: "/tmp",
},
Filename: "hype.md",
}
r := require.New(t)
act := ce.Error()
act = strings.TrimSpace(act)
exp := "filepath: /tmp/hype.md\ncmd: $ echo hello\nexit: 1\ncmd error: EOF"
exp = strings.TrimSpace(exp)
r.Equal(exp, act)
}