-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
84 lines (76 loc) · 2.11 KB
/
Copy patherrors_test.go
File metadata and controls
84 lines (76 loc) · 2.11 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
package trexec
import (
"context"
"errors"
"testing"
)
func TestExitErrorMessage(t *testing.T) {
tests := []struct {
name string
err ExitError
want string
}{
{
name: "normal exit code",
err: ExitError{ExitCode: 1},
want: "trexec: process exited with code 1",
},
{
name: "killed by signal",
err: ExitError{ExitCode: -1, Signal: "killed"},
want: "trexec: process killed by killed",
},
{
name: "cancelled with exit code",
err: ExitError{ExitCode: 1, Cancelled: true},
want: "trexec: process exited with code 1 (cancelled)",
},
{
name: "cancelled and killed",
err: ExitError{ExitCode: -1, Signal: "killed", Cancelled: true},
want: "trexec: process killed by killed (cancelled)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.err.Error(); got != tt.want {
t.Errorf("Error() = %q, want %q", got, tt.want)
}
})
}
}
func TestExitErrorUnwrap(t *testing.T) {
t.Run("cancelled unwraps to context.Canceled", func(t *testing.T) {
err := &ExitError{ExitCode: -1, Cancelled: true}
if !errors.Is(err, context.Canceled) {
t.Error("errors.Is(err, context.Canceled) = false, want true")
}
})
t.Run("not cancelled unwraps to nil", func(t *testing.T) {
err := &ExitError{ExitCode: 1, Cancelled: false}
if errors.Is(err, context.Canceled) {
t.Error("errors.Is(err, context.Canceled) = true, want false")
}
})
}
func TestExitErrorAs(t *testing.T) {
var origErr error = &ExitError{ExitCode: 42, Signal: "terminated"}
var exitErr *ExitError
if !errors.As(origErr, &exitErr) {
t.Fatal("errors.As failed")
}
if exitErr.ExitCode != 42 {
t.Errorf("ExitCode = %d, want 42", exitErr.ExitCode)
}
if exitErr.Signal != "terminated" {
t.Errorf("Signal = %q, want %q", exitErr.Signal, "terminated")
}
}
func TestSentinelErrors(t *testing.T) {
if ErrAlreadyStarted.Error() != "trexec: command already started" {
t.Errorf("unexpected ErrAlreadyStarted message: %q", ErrAlreadyStarted.Error())
}
if ErrNotStarted.Error() != "trexec: command not started" {
t.Errorf("unexpected ErrNotStarted message: %q", ErrNotStarted.Error())
}
}