|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestConfiguration_Validate(t *testing.T) { |
| 11 | + testCases := []struct { |
| 12 | + desc string |
| 13 | + cfg *Configuration |
| 14 | + }{ |
| 15 | + { |
| 16 | + desc: "version", |
| 17 | + cfg: &Configuration{ |
| 18 | + Version: "v1.57.0", |
| 19 | + Plugins: []*Plugin{ |
| 20 | + { |
| 21 | + Module: "example.org/foo/bar", |
| 22 | + Import: "example.org/foo/bar/test", |
| 23 | + Version: "v1.2.3", |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + { |
| 29 | + desc: "path", |
| 30 | + cfg: &Configuration{ |
| 31 | + Version: "v1.57.0", |
| 32 | + Plugins: []*Plugin{ |
| 33 | + { |
| 34 | + Module: "example.org/foo/bar", |
| 35 | + Import: "example.org/foo/bar/test", |
| 36 | + Path: "/my/path", |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + for _, test := range testCases { |
| 44 | + test := test |
| 45 | + t.Run(test.desc, func(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + err := test.cfg.Validate() |
| 49 | + require.NoError(t, err) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestConfiguration_Validate_error(t *testing.T) { |
| 55 | + testCases := []struct { |
| 56 | + desc string |
| 57 | + cfg *Configuration |
| 58 | + expected string |
| 59 | + }{ |
| 60 | + { |
| 61 | + desc: "missing version", |
| 62 | + cfg: &Configuration{}, |
| 63 | + expected: "root field 'version' is required", |
| 64 | + }, |
| 65 | + { |
| 66 | + desc: "no plugins", |
| 67 | + cfg: &Configuration{ |
| 68 | + Version: "v1.57.0", |
| 69 | + }, |
| 70 | + expected: "no plugins defined", |
| 71 | + }, |
| 72 | + { |
| 73 | + desc: "missing module", |
| 74 | + cfg: &Configuration{ |
| 75 | + Version: "v1.57.0", |
| 76 | + Plugins: []*Plugin{ |
| 77 | + { |
| 78 | + Module: "", |
| 79 | + Import: "example.org/foo/bar/test", |
| 80 | + Version: "v1.2.3", |
| 81 | + Path: "/my/path", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + expected: "field 'module' is required", |
| 86 | + }, |
| 87 | + { |
| 88 | + desc: "module version and path", |
| 89 | + cfg: &Configuration{ |
| 90 | + Version: "v1.57.0", |
| 91 | + Plugins: []*Plugin{ |
| 92 | + { |
| 93 | + Module: "example.org/foo/bar", |
| 94 | + Import: "example.org/foo/bar/test", |
| 95 | + Version: "v1.2.3", |
| 96 | + Path: "/my/path", |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + expected: "invalid configuration: 'version' and 'path' should not be provided at the same time", |
| 101 | + }, |
| 102 | + { |
| 103 | + desc: "no module version and path", |
| 104 | + cfg: &Configuration{ |
| 105 | + Version: "v1.57.0", |
| 106 | + Plugins: []*Plugin{ |
| 107 | + { |
| 108 | + Module: "example.org/foo/bar", |
| 109 | + Import: "example.org/foo/bar/test", |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + expected: "missing information: 'version' or 'path' should be provided", |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, test := range testCases { |
| 118 | + test := test |
| 119 | + t.Run(test.desc, func(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + err := test.cfg.Validate() |
| 123 | + |
| 124 | + assert.EqualError(t, err, test.expected) |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments