|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestLintersSettings_Validate(t *testing.T) { |
| 10 | + testCases := []struct { |
| 11 | + desc string |
| 12 | + settings *LintersSettings |
| 13 | + }{ |
| 14 | + { |
| 15 | + desc: "custom linter", |
| 16 | + settings: &LintersSettings{ |
| 17 | + Custom: map[string]CustomLinterSettings{ |
| 18 | + "example": { |
| 19 | + Type: "module", |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + { |
| 25 | + desc: "govet", |
| 26 | + settings: &LintersSettings{ |
| 27 | + Govet: GovetSettings{ |
| 28 | + Enable: []string{"a"}, |
| 29 | + DisableAll: true, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + for _, test := range testCases { |
| 36 | + test := test |
| 37 | + t.Run(test.desc, func(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + err := test.settings.Validate() |
| 41 | + assert.NoError(t, err) |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestLintersSettings_Validate_error(t *testing.T) { |
| 47 | + testCases := []struct { |
| 48 | + desc string |
| 49 | + settings *LintersSettings |
| 50 | + expected string |
| 51 | + }{ |
| 52 | + { |
| 53 | + desc: "custom linter error", |
| 54 | + settings: &LintersSettings{ |
| 55 | + Custom: map[string]CustomLinterSettings{ |
| 56 | + "example": { |
| 57 | + Type: "module", |
| 58 | + Path: "example", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + expected: `custom linter "example": path not supported with module type`, |
| 63 | + }, |
| 64 | + { |
| 65 | + desc: "govet error", |
| 66 | + settings: &LintersSettings{ |
| 67 | + Govet: GovetSettings{ |
| 68 | + EnableAll: true, |
| 69 | + DisableAll: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + expected: "govet: enable-all and disable-all can't be combined", |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, test := range testCases { |
| 77 | + test := test |
| 78 | + t.Run(test.desc, func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + |
| 81 | + err := test.settings.Validate() |
| 82 | + |
| 83 | + assert.EqualError(t, err, test.expected) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestCustomLinterSettings_Validate(t *testing.T) { |
| 89 | + testCases := []struct { |
| 90 | + desc string |
| 91 | + settings *CustomLinterSettings |
| 92 | + }{ |
| 93 | + { |
| 94 | + desc: "only path", |
| 95 | + settings: &CustomLinterSettings{ |
| 96 | + Path: "example", |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + desc: "path and type goplugin", |
| 101 | + settings: &CustomLinterSettings{ |
| 102 | + Type: "goplugin", |
| 103 | + Path: "example", |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + desc: "type module", |
| 108 | + settings: &CustomLinterSettings{ |
| 109 | + Type: "module", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, test := range testCases { |
| 115 | + test := test |
| 116 | + t.Run(test.desc, func(t *testing.T) { |
| 117 | + t.Parallel() |
| 118 | + |
| 119 | + err := test.settings.Validate() |
| 120 | + assert.NoError(t, err) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestCustomLinterSettings_Validate_error(t *testing.T) { |
| 126 | + testCases := []struct { |
| 127 | + desc string |
| 128 | + settings *CustomLinterSettings |
| 129 | + expected string |
| 130 | + }{ |
| 131 | + { |
| 132 | + desc: "missing path", |
| 133 | + settings: &CustomLinterSettings{}, |
| 134 | + expected: "path is required", |
| 135 | + }, |
| 136 | + { |
| 137 | + desc: "module and path", |
| 138 | + settings: &CustomLinterSettings{ |
| 139 | + Type: "module", |
| 140 | + Path: "example", |
| 141 | + }, |
| 142 | + expected: "path not supported with module type", |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + for _, test := range testCases { |
| 147 | + test := test |
| 148 | + t.Run(test.desc, func(t *testing.T) { |
| 149 | + t.Parallel() |
| 150 | + |
| 151 | + err := test.settings.Validate() |
| 152 | + |
| 153 | + assert.EqualError(t, err, test.expected) |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestGovetSettings_Validate(t *testing.T) { |
| 159 | + testCases := []struct { |
| 160 | + desc string |
| 161 | + settings *GovetSettings |
| 162 | + }{ |
| 163 | + { |
| 164 | + desc: "empty", |
| 165 | + settings: &GovetSettings{}, |
| 166 | + }, |
| 167 | + { |
| 168 | + desc: "disable-all and enable", |
| 169 | + settings: &GovetSettings{ |
| 170 | + Enable: []string{"a"}, |
| 171 | + DisableAll: true, |
| 172 | + }, |
| 173 | + }, |
| 174 | + { |
| 175 | + desc: "enable-all and disable", |
| 176 | + settings: &GovetSettings{ |
| 177 | + Disable: []string{"a"}, |
| 178 | + EnableAll: true, |
| 179 | + }, |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + for _, test := range testCases { |
| 184 | + test := test |
| 185 | + t.Run(test.desc, func(t *testing.T) { |
| 186 | + t.Parallel() |
| 187 | + |
| 188 | + err := test.settings.Validate() |
| 189 | + assert.NoError(t, err) |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestGovetSettings_Validate_error(t *testing.T) { |
| 195 | + testCases := []struct { |
| 196 | + desc string |
| 197 | + settings *GovetSettings |
| 198 | + expected string |
| 199 | + }{ |
| 200 | + { |
| 201 | + desc: "enable-all and disable-all", |
| 202 | + settings: &GovetSettings{ |
| 203 | + EnableAll: true, |
| 204 | + DisableAll: true, |
| 205 | + }, |
| 206 | + expected: "govet: enable-all and disable-all can't be combined", |
| 207 | + }, |
| 208 | + { |
| 209 | + desc: "enable-all and enable", |
| 210 | + settings: &GovetSettings{ |
| 211 | + EnableAll: true, |
| 212 | + Enable: []string{"a"}, |
| 213 | + }, |
| 214 | + expected: "govet: enable-all and enable can't be combined", |
| 215 | + }, |
| 216 | + { |
| 217 | + desc: "disable-all and disable", |
| 218 | + settings: &GovetSettings{ |
| 219 | + DisableAll: true, |
| 220 | + Disable: []string{"a"}, |
| 221 | + }, |
| 222 | + expected: "govet: disable-all and disable can't be combined", |
| 223 | + }, |
| 224 | + } |
| 225 | + |
| 226 | + for _, test := range testCases { |
| 227 | + test := test |
| 228 | + t.Run(test.desc, func(t *testing.T) { |
| 229 | + t.Parallel() |
| 230 | + |
| 231 | + err := test.settings.Validate() |
| 232 | + |
| 233 | + assert.EqualError(t, err, test.expected) |
| 234 | + }) |
| 235 | + } |
| 236 | +} |
0 commit comments