...
1
2
3
4 package errors
5
6 import (
7 "fmt"
8 "strings"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "sigs.k8s.io/cli-utils/pkg/inventory"
13 )
14
15 func TestTextForError(t *testing.T) {
16 testCases := map[string]struct {
17 err error
18 cmdNameBase string
19 expectFound bool
20 expectedErrText string
21 }{
22 "kapply command base name": {
23 err: &inventory.NoInventoryObjError{},
24 cmdNameBase: "kapply",
25 expectFound: true,
26 expectedErrText: "Please run \"kapply init\" command.",
27 },
28 "different command base name": {
29 err: &inventory.NoInventoryObjError{},
30 cmdNameBase: "mycommand",
31 expectFound: true,
32 expectedErrText: "Please run \"mycommand init\" command.",
33 },
34 "known error without directives in the template": {
35 err: &inventory.MultipleInventoryObjError{},
36 cmdNameBase: "kapply",
37 expectFound: true,
38 expectedErrText: "Package has multiple inventory object templates.",
39 },
40 "unknown error": {
41 err: fmt.Errorf("this is a test"),
42 cmdNameBase: "kapply",
43 expectFound: false,
44 },
45 "unknown error type": {
46 err: sliceError{},
47 cmdNameBase: "kapply",
48 expectFound: false,
49 },
50 }
51
52 for tn, tc := range testCases {
53 t.Run(tn, func(t *testing.T) {
54 errText, found := textForError(tc.err, tc.cmdNameBase)
55
56 if !tc.expectFound {
57 assert.False(t, found)
58 return
59 }
60
61 assert.True(t, found)
62 assert.Contains(t, errText, strings.TrimSpace(tc.expectedErrText))
63 })
64 }
65 }
66
67 type sliceError []string
68
69 func (s sliceError) Error() string {
70 return "this is a test"
71 }
72
View as plain text