Jeff Gaston | f5f6e49 | 2017-11-20 14:31:11 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
| 20 | "strings" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func validateConfigAnnotations(configurable jsonConfigurable) (err error) { |
| 25 | reflectType := reflect.TypeOf(configurable) |
| 26 | reflectType = reflectType.Elem() |
| 27 | for i := 0; i < reflectType.NumField(); i++ { |
| 28 | field := reflectType.Field(i) |
| 29 | jsonTag := field.Tag.Get("json") |
| 30 | // Check for mistakes in the json tag |
| 31 | if jsonTag != "" && !strings.HasPrefix(jsonTag, ",") { |
| 32 | if !strings.Contains(jsonTag, ",") { |
| 33 | // Probably an accidental rename, most likely "omitempty" instead of ",omitempty" |
| 34 | return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+ |
| 35 | "Did you mean to use an annotation of %q?\n"+ |
| 36 | "(Alternatively, to change the json name of the field, rename the field in source instead.)", |
| 37 | reflectType.Name(), field.Name, field.Tag, jsonTag, ","+jsonTag) |
| 38 | } else { |
| 39 | // Although this rename was probably intentional, |
| 40 | // a json annotation is still more confusing than renaming the source variable |
| 41 | requestedName := strings.Split(jsonTag, ",")[0] |
| 42 | return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+ |
| 43 | "To change the json name of the field, rename the field in source instead.", |
| 44 | reflectType.Name(), field.Name, field.Tag, requestedName) |
| 45 | |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | type configType struct { |
Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 53 | PopulateMe *bool `json:"omitempty"` |
Jeff Gaston | f5f6e49 | 2017-11-20 14:31:11 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | func (c *configType) SetDefaultConfig() { |
| 57 | } |
| 58 | |
| 59 | // tests that ValidateConfigAnnotation works |
| 60 | func TestValidateConfigAnnotations(t *testing.T) { |
| 61 | config := configType{} |
| 62 | err := validateConfigAnnotations(&config) |
Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 63 | expectedError := `Field configType.PopulateMe has tag json:"omitempty" which specifies to change its json field name to "omitempty". |
Jeff Gaston | f5f6e49 | 2017-11-20 14:31:11 -0800 | [diff] [blame] | 64 | Did you mean to use an annotation of ",omitempty"? |
| 65 | (Alternatively, to change the json name of the field, rename the field in source instead.)` |
| 66 | if err.Error() != expectedError { |
| 67 | t.Errorf("Incorrect error; expected:\n"+ |
| 68 | "%s\n"+ |
| 69 | "got:\n"+ |
| 70 | "%s", |
| 71 | expectedError, err.Error()) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // run validateConfigAnnotations against each type that might have json annotations |
| 76 | func TestProductConfigAnnotations(t *testing.T) { |
| 77 | err := validateConfigAnnotations(&productVariables{}) |
| 78 | if err != nil { |
| 79 | t.Errorf(err.Error()) |
| 80 | } |
| 81 | |
| 82 | validateConfigAnnotations(&FileConfigurableOptions{}) |
| 83 | if err != nil { |
| 84 | t.Errorf(err.Error()) |
| 85 | } |
| 86 | } |
Dan Willemsen | 0fe7866 | 2018-03-26 12:41:18 -0700 | [diff] [blame] | 87 | |
| 88 | func TestMissingVendorConfig(t *testing.T) { |
| 89 | c := &config{} |
| 90 | if c.VendorConfig("test").Bool("not_set") { |
| 91 | t.Errorf("Expected false") |
| 92 | } |
| 93 | } |