|
| 1 | +package configtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.uber.org/multierr" |
| 12 | + |
| 13 | + "github.com/smartcontractkit/chainlink-common/pkg/config" |
| 14 | +) |
| 15 | + |
| 16 | +func AssertFieldsNotNil(t *testing.T, s interface{}) { |
| 17 | + err := assertValNotNil(t, "", reflect.ValueOf(s)) |
| 18 | + _, err = config.MultiErrorList(err) |
| 19 | + assert.NoError(t, err) |
| 20 | +} |
| 21 | + |
| 22 | +// assertFieldsNotNil recursively checks the struct s for nil fields. |
| 23 | +func assertFieldsNotNil(t *testing.T, prefix string, s reflect.Value) (err error) { |
| 24 | + t.Helper() |
| 25 | + require.Equal(t, reflect.Struct, s.Kind()) |
| 26 | + |
| 27 | + typ := s.Type() |
| 28 | + for i := 0; i < s.NumField(); i++ { |
| 29 | + f := s.Field(i) |
| 30 | + key := prefix |
| 31 | + if tf := typ.Field(i); !tf.Anonymous { |
| 32 | + if key != "" { |
| 33 | + key += "." |
| 34 | + } |
| 35 | + key += tf.Name |
| 36 | + } |
| 37 | + err = multierr.Combine(err, assertValNotNil(t, key, f)) |
| 38 | + } |
| 39 | + return |
| 40 | +} |
| 41 | + |
| 42 | +// assertValuesNotNil recursively checks the map m for nil values. |
| 43 | +func assertValuesNotNil(t *testing.T, prefix string, m reflect.Value) (err error) { |
| 44 | + t.Helper() |
| 45 | + require.Equal(t, reflect.Map, m.Kind()) |
| 46 | + if prefix != "" { |
| 47 | + prefix += "." |
| 48 | + } |
| 49 | + |
| 50 | + mi := m.MapRange() |
| 51 | + for mi.Next() { |
| 52 | + key := prefix + mi.Key().String() |
| 53 | + err = multierr.Combine(err, assertValNotNil(t, key, mi.Value())) |
| 54 | + } |
| 55 | + return |
| 56 | +} |
| 57 | + |
| 58 | +// assertElementsNotNil recursively checks the slice s for nil values. |
| 59 | +func assertElementsNotNil(t *testing.T, prefix string, s reflect.Value) (err error) { |
| 60 | + t.Helper() |
| 61 | + require.Equal(t, reflect.Slice, s.Kind()) |
| 62 | + |
| 63 | + for i := 0; i < s.Len(); i++ { |
| 64 | + err = multierr.Combine(err, assertValNotNil(t, prefix, s.Index(i))) |
| 65 | + } |
| 66 | + return |
| 67 | +} |
| 68 | + |
| 69 | +var ( |
| 70 | + textUnmarshaler encoding.TextUnmarshaler |
| 71 | + textUnmarshalerType = reflect.TypeOf(&textUnmarshaler).Elem() |
| 72 | +) |
| 73 | + |
| 74 | +// assertValNotNil recursively checks that val is not nil. val must be a struct, map, slice, or point to one. |
| 75 | +func assertValNotNil(t *testing.T, key string, val reflect.Value) error { |
| 76 | + t.Helper() |
| 77 | + k := val.Kind() |
| 78 | + switch k { //nolint:exhaustive |
| 79 | + case reflect.Ptr: |
| 80 | + if val.IsNil() { |
| 81 | + return fmt.Errorf("%s: nil", key) |
| 82 | + } |
| 83 | + } |
| 84 | + if k == reflect.Ptr { |
| 85 | + if val.Type().Implements(textUnmarshalerType) { |
| 86 | + return nil // skip values unmarshaled from strings |
| 87 | + } |
| 88 | + val = val.Elem() |
| 89 | + } |
| 90 | + switch val.Kind() { |
| 91 | + case reflect.Struct: |
| 92 | + if val.Type().Implements(textUnmarshalerType) { |
| 93 | + return nil // skip values unmarshaled from strings |
| 94 | + } |
| 95 | + return assertFieldsNotNil(t, key, val) |
| 96 | + case reflect.Map: |
| 97 | + if val.IsNil() { |
| 98 | + return nil // not actually a problem |
| 99 | + } |
| 100 | + return assertValuesNotNil(t, key, val) |
| 101 | + case reflect.Slice: |
| 102 | + if val.IsNil() { |
| 103 | + return nil // not actually a problem |
| 104 | + } |
| 105 | + return assertElementsNotNil(t, key, val) |
| 106 | + default: |
| 107 | + return nil |
| 108 | + } |
| 109 | +} |
0 commit comments