cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/config/options/selector_test.go
89lines · modecode
| 1 | package options_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | |
| 10 | "github.com/cloudflare/pint/internal/checks" |
| 11 | "github.com/cloudflare/pint/internal/config/options" |
| 12 | ) |
| 13 | |
| 14 | func TestSelectorSettings(t *testing.T) { |
| 15 | type testCaseT struct { |
| 16 | err error |
| 17 | conf options.SelectorSettings |
| 18 | } |
| 19 | |
| 20 | testCases := []testCaseT{ |
| 21 | { |
| 22 | conf: options.SelectorSettings{ |
| 23 | Key: "summary", |
| 24 | RequiredLabels: []string{"foo"}, |
| 25 | }, |
| 26 | }, |
| 27 | { |
| 28 | conf: options.SelectorSettings{}, |
| 29 | err: errors.New("selector key cannot be empty"), |
| 30 | }, |
| 31 | { |
| 32 | conf: options.SelectorSettings{ |
| 33 | Key: ".++", |
| 34 | }, |
| 35 | err: errors.New("error parsing regexp: invalid nested repetition operator: `++`"), |
| 36 | }, |
| 37 | { |
| 38 | conf: options.SelectorSettings{ |
| 39 | Key: ".+", |
| 40 | Severity: "foo", |
| 41 | }, |
| 42 | err: errors.New("unknown severity: foo"), |
| 43 | }, |
| 44 | { |
| 45 | conf: options.SelectorSettings{ |
| 46 | Key: "summary", |
| 47 | }, |
| 48 | err: errors.New("requiredLabels cannot be empty"), |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for _, tc := range testCases { |
| 53 | t.Run(fmt.Sprintf("%v", tc.conf), func(t *testing.T) { |
| 54 | err := tc.conf.Validate() |
| 55 | if err == nil || tc.err == nil { |
| 56 | require.Equal(t, err, tc.err) |
| 57 | } else { |
| 58 | require.EqualError(t, err, tc.err.Error()) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestSelectorSettingsGetSeverity(t *testing.T) { |
| 65 | type testCaseT struct { |
| 66 | conf options.SelectorSettings |
| 67 | fallback checks.Severity |
| 68 | expected checks.Severity |
| 69 | } |
| 70 | |
| 71 | testCases := []testCaseT{ |
| 72 | { |
| 73 | conf: options.SelectorSettings{Severity: "info"}, |
| 74 | fallback: checks.Bug, |
| 75 | expected: checks.Information, |
| 76 | }, |
| 77 | { |
| 78 | conf: options.SelectorSettings{}, |
| 79 | fallback: checks.Bug, |
| 80 | expected: checks.Bug, |
| 81 | }, |
| 82 | } |
| 83 | |
| 84 | for _, tc := range testCases { |
| 85 | t.Run(fmt.Sprintf("%v", tc.conf), func(t *testing.T) { |
| 86 | require.Equal(t, tc.expected, tc.conf.GetSeverity(tc.fallback)) |
| 87 | }) |
| 88 | } |
| 89 | } |
| 90 | |