-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patherrors_test.go
More file actions
80 lines (67 loc) · 2.6 KB
/
errors_test.go
File metadata and controls
80 lines (67 loc) · 2.6 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
package cmd
import (
"fmt"
"testing"
)
func TestExitCodeError(t *testing.T) {
// Test the exitCodeError type
originalErr := fmt.Errorf("test error")
exitErr := exitWithCode(42, originalErr)
if exitErr.Error() != "test error" {
t.Errorf("Expected error message 'test error', got '%s'", exitErr.Error())
}
if exitCodeErr, ok := exitErr.(exitCodeError); ok {
if exitCodeErr.ExitCode() != 42 {
t.Errorf("Expected exit code 42, got %d", exitCodeErr.ExitCode())
}
} else {
t.Error("exitWithCode should return exitCodeError")
}
}
func TestExitCodeError_NilError(t *testing.T) {
exitErr := exitWithCode(1, nil)
if exitErr.Error() != "" {
t.Errorf("Expected empty error message for nil error, got '%s'", exitErr.Error())
}
if exitCodeErr, ok := exitErr.(interface{ ExitCode() int }); ok {
if exitCodeErr.ExitCode() != 1 {
t.Errorf("Expected exit code 1, got %d", exitCodeErr.ExitCode())
}
} else {
t.Error("exitWithCode should return exitCodeError")
}
}
func TestExitAuthenticationError(t *testing.T) {
originalErr := fmt.Errorf("authentication failed: invalid API key")
exitErr := exitWithCode(ExitAuthenticationError, originalErr)
if exitErr.Error() != "authentication failed: invalid API key" {
t.Errorf("Expected error message 'authentication failed: invalid API key', got '%s'", exitErr.Error())
}
if exitCodeErr, ok := exitErr.(interface{ ExitCode() int }); ok {
if exitCodeErr.ExitCode() != ExitAuthenticationError {
t.Errorf("Expected exit code %d (ExitAuthenticationError), got %d", ExitAuthenticationError, exitCodeErr.ExitCode())
}
if exitCodeErr.ExitCode() != 4 {
t.Errorf("Expected exit code 4 for authentication error, got %d", exitCodeErr.ExitCode())
}
} else {
t.Error("exitWithCode should return exitCodeError with ExitCode method")
}
}
func TestExitPermissionDenied(t *testing.T) {
originalErr := fmt.Errorf("permission denied: insufficient access to service")
exitErr := exitWithCode(ExitPermissionDenied, originalErr)
if exitErr.Error() != "permission denied: insufficient access to service" {
t.Errorf("Expected error message 'permission denied: insufficient access to service', got '%s'", exitErr.Error())
}
if exitCodeErr, ok := exitErr.(interface{ ExitCode() int }); ok {
if exitCodeErr.ExitCode() != ExitPermissionDenied {
t.Errorf("Expected exit code %d (ExitPermissionDenied), got %d", ExitPermissionDenied, exitCodeErr.ExitCode())
}
if exitCodeErr.ExitCode() != 5 {
t.Errorf("Expected exit code 5 for permission denied, got %d", exitCodeErr.ExitCode())
}
} else {
t.Error("exitWithCode should return exitCodeError with ExitCode method")
}
}