| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
| 10 | // TestExitError_Error tests the Error() method of ExitError. |
| 11 | // |
| 12 | |
| 13 | func TestExitError_Error(t *testing.T) { |
| 14 | t.Parallel() |
| 15 | |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | e ExitError |
| 19 | want string |
| 20 | }{ |
| 21 | { |
| 22 | name: "empty message", |
| 23 | e: ExitError{ExitCode: ExSuccess, Message: ""}, |
| 24 | want: "", |
| 25 | }, |
| 26 | { |
| 27 | name: "usage error with invalid flag", |
| 28 | e: ExitError{ExitCode: ExUsage, Message: "invalid flag"}, |
| 29 | want: "invalid flag", |
| 30 | }, |
| 31 | { |
| 32 | name: "config error with missing token", |
| 33 | e: ExitError{ExitCode: ExConfig, Message: "missing token"}, |
| 34 | want: "missing token", |
| 35 | }, |
| 36 | { |
| 37 | name: "unicode message", |
| 38 | e: ExitError{ExitCode: ExUnavailable, Message: "通知错误"}, |
| 39 | want: "通知错误", |
| 40 | }, |
| 41 | { |
| 42 | name: "long message", |
| 43 | e: ExitError{ExitCode: ExUsage, Message: strings.Repeat("a", 1000)}, |
| 44 | want: strings.Repeat("a", 1000), |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | for _, tt := range tests { |
| 49 | t.Run(tt.name, func(t *testing.T) { |
| 50 | t.Parallel() |
| 51 | |
| 52 | got := tt.e.Error() |
| 53 | assert.Equal(t, tt.want, got, "ExitError.Error() returned unexpected value") |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // TestExitError_Error_ImplementsInterface verifies ExitError implements the error interface. |
| 59 | func TestExitError_Error_ImplementsInterface(t *testing.T) { |
| 60 | t.Parallel() |
| 61 | |
| 62 | // Verify ExitError implements error interface |
| 63 | var _ error = ExitError{ExitCode: ExSuccess, Message: "test"} |
| 64 | |
| 65 | // Verify error interface methods work correctly |
| 66 | err := ExitError{ExitCode: ExConfig, Message: "interface test"} |
| 67 | assert.Equal(t, "interface test", err.Error()) |
| 68 | } |
| 69 | |
| 70 | // TestInvalidUsage tests the InvalidUsage() factory function. |
| 71 | func TestInvalidUsage(t *testing.T) { |
| 72 | t.Parallel() |
| 73 | |
| 74 | tests := []struct { |
| 75 | name string |
| 76 | message string |
| 77 | want ExitError |
| 78 | }{ |
| 79 | { |
| 80 | name: "simple message with bad args", |
| 81 | message: "bad args", |
| 82 | want: ExitError{ExitCode: ExUsage, Message: "bad args"}, |
| 83 | }, |
| 84 | { |
| 85 | name: "empty message", |
| 86 | message: "", |
| 87 | want: ExitError{ExitCode: ExUsage, Message: ""}, |
| 88 | }, |
| 89 | { |
| 90 | name: "message with formatting usage", |
| 91 | message: "usage: cmd --flag", |
| 92 | want: ExitError{ExitCode: ExUsage, Message: "usage: cmd --flag"}, |
| 93 | }, |
| 94 | { |
| 95 | name: "message with special characters", |
| 96 | message: "error: $VAR & | pipe", |
| 97 | want: ExitError{ExitCode: ExUsage, Message: "error: $VAR & | pipe"}, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | for _, tt := range tests { |
| 102 | t.Run(tt.name, func(t *testing.T) { |
| 103 | t.Parallel() |
| 104 | |
| 105 | got := InvalidUsage(tt.message) |
| 106 | assert.Equal(t, tt.want, got, "InvalidUsage() returned unexpected ExitError") |
| 107 | assert.Equal(t, ExUsage, got.ExitCode, "InvalidUsage() should have ExitCode ExUsage") |
| 108 | assert.Equal(t, tt.message, got.Message, "InvalidUsage() should preserve message") |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // TestTaskUnavailable tests the TaskUnavailable() factory function. |
| 114 | func TestTaskUnavailable(t *testing.T) { |
| 115 | t.Parallel() |
| 116 | |
| 117 | tests := []struct { |
| 118 | name string |
| 119 | message string |
| 120 | want ExitError |
| 121 | }{ |
| 122 | { |
| 123 | name: "service down with smtp server unreachable", |
| 124 | message: "smtp server unreachable", |
| 125 | want: ExitError{ExitCode: ExUnavailable, Message: "smtp server unreachable"}, |
| 126 | }, |
| 127 | { |
| 128 | name: "network error with connection timeout", |
| 129 | message: "connection timeout", |
| 130 | want: ExitError{ExitCode: ExUnavailable, Message: "connection timeout"}, |
| 131 | }, |
| 132 | { |
| 133 | name: "empty message", |
| 134 | message: "", |
| 135 | want: ExitError{ExitCode: ExUnavailable, Message: ""}, |
| 136 | }, |
| 137 | } |
| 138 | |
| 139 | for _, tt := range tests { |
| 140 | t.Run(tt.name, func(t *testing.T) { |
| 141 | t.Parallel() |
| 142 | |
| 143 | got := TaskUnavailable(tt.message) |
| 144 | assert.Equal(t, tt.want, got, "TaskUnavailable() returned unexpected ExitError") |
| 145 | assert.Equal(t, ExUnavailable, got.ExitCode, "TaskUnavailable() should have ExitCode ExUnavailable") |
| 146 | assert.Equal(t, tt.message, got.Message, "TaskUnavailable() should preserve message") |
| 147 | }) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // TestConfigurationError tests the ConfigurationError() factory function. |
| 152 | func TestConfigurationError(t *testing.T) { |
| 153 | t.Parallel() |
| 154 | |
| 155 | tests := []struct { |
| 156 | name string |
| 157 | message string |
| 158 | want ExitError |
| 159 | }{ |
| 160 | { |
| 161 | name: "missing token for discord", |
| 162 | message: "discord: missing webhook", |
| 163 | want: ExitError{ExitCode: ExConfig, Message: "discord: missing webhook"}, |
| 164 | }, |
| 165 | { |
| 166 | name: "invalid URL with parse error", |
| 167 | message: "parse error: scheme", |
| 168 | want: ExitError{ExitCode: ExConfig, Message: "parse error: scheme"}, |
| 169 | }, |
| 170 | { |
| 171 | name: "empty message", |
| 172 | message: "", |
| 173 | want: ExitError{ExitCode: ExConfig, Message: ""}, |
| 174 | }, |
| 175 | } |
| 176 | |
| 177 | for _, tt := range tests { |
| 178 | t.Run(tt.name, func(t *testing.T) { |
| 179 | t.Parallel() |
| 180 | |
| 181 | got := ConfigurationError(tt.message) |
| 182 | assert.Equal(t, tt.want, got, "ConfigurationError() returned unexpected ExitError") |
| 183 | assert.Equal(t, ExConfig, got.ExitCode, "ConfigurationError() should have ExitCode ExConfig") |
| 184 | assert.Equal(t, tt.message, got.Message, "ConfigurationError() should preserve message") |
| 185 | }) |
| 186 | } |
| 187 | } |