/
error_printer_test.go
78 lines (63 loc) · 1.52 KB
/
error_printer_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
package termui
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/fatih/color"
)
const (
codeNotImplementedError = iota + 200
codeUnsupportedOSError
)
type ErrorImpl struct {
err error
code int
}
func (e ErrorImpl) Error() string {
return e.err.Error()
}
func (e ErrorImpl) Code() int {
return e.code
}
func UnsupportedOSError(os string) ErrorImpl {
return ErrorImpl{
err: fmt.Errorf("Unsupported OS: %s", os),
code: codeUnsupportedOSError,
}
}
func NotImplementedError() ErrorImpl {
return ErrorImpl{
err: fmt.Errorf("This feature has not been implemented"),
code: codeNotImplementedError,
}
}
func TestErrorPrinter_Printing(t *testing.T) {
saveExitFunction := exitFunction
defer func() {
exitFunction = saveExitFunction
}()
exitCode := -1
exitFunction = func(code int) {
exitCode = codeUnsupportedOSError
}
in, out := &bytes.Buffer{}, &bytes.Buffer{}
ui := New(in, out, nil)
printer := NewErrorPrinter(ui)
err1 := UnsupportedOSError("windows")
err2 := fmt.Errorf("Generic error")
printer.PrintAndExit(err1)
exp := color.RedString("Error (%d): Unsupported OS: windows", codeUnsupportedOSError)
if got := strings.TrimSpace(out.String()); got != exp {
t.Error("wrong error message:", got)
}
if exitCode != codeUnsupportedOSError {
t.Error("wrong exit code:", exitCode)
}
out.Reset()
printer.PrintWarning(err2)
exp = color.YellowString("Warning (%d): Generic error", CodeUnknownError)
if got := strings.TrimSpace(out.String()); got != exp {
t.Error("wrong error message:", got)
}
}