config_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. package android
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. "testing"
  20. )
  21. func validateConfigAnnotations(configurable jsonConfigurable) (err error) {
  22. reflectType := reflect.TypeOf(configurable)
  23. reflectType = reflectType.Elem()
  24. for i := 0; i < reflectType.NumField(); i++ {
  25. field := reflectType.Field(i)
  26. jsonTag := field.Tag.Get("json")
  27. // Check for mistakes in the json tag
  28. if jsonTag != "" && !strings.HasPrefix(jsonTag, ",") {
  29. if !strings.Contains(jsonTag, ",") {
  30. // Probably an accidental rename, most likely "omitempty" instead of ",omitempty"
  31. return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+
  32. "Did you mean to use an annotation of %q?\n"+
  33. "(Alternatively, to change the json name of the field, rename the field in source instead.)",
  34. reflectType.Name(), field.Name, field.Tag, jsonTag, ","+jsonTag)
  35. } else {
  36. // Although this rename was probably intentional,
  37. // a json annotation is still more confusing than renaming the source variable
  38. requestedName := strings.Split(jsonTag, ",")[0]
  39. return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+
  40. "To change the json name of the field, rename the field in source instead.",
  41. reflectType.Name(), field.Name, field.Tag, requestedName)
  42. }
  43. }
  44. }
  45. return nil
  46. }
  47. type configType struct {
  48. PopulateMe *bool `json:"omitempty"`
  49. }
  50. func (c *configType) SetDefaultConfig() {
  51. }
  52. // tests that ValidateConfigAnnotation works
  53. func TestValidateConfigAnnotations(t *testing.T) {
  54. config := configType{}
  55. err := validateConfigAnnotations(&config)
  56. expectedError := `Field configType.PopulateMe has tag json:"omitempty" which specifies to change its json field name to "omitempty".
  57. Did you mean to use an annotation of ",omitempty"?
  58. (Alternatively, to change the json name of the field, rename the field in source instead.)`
  59. if err.Error() != expectedError {
  60. t.Errorf("Incorrect error; expected:\n"+
  61. "%s\n"+
  62. "got:\n"+
  63. "%s",
  64. expectedError, err.Error())
  65. }
  66. }
  67. // run validateConfigAnnotations against each type that might have json annotations
  68. func TestProductConfigAnnotations(t *testing.T) {
  69. err := validateConfigAnnotations(&productVariables{})
  70. if err != nil {
  71. t.Errorf(err.Error())
  72. }
  73. validateConfigAnnotations(&FileConfigurableOptions{})
  74. if err != nil {
  75. t.Errorf(err.Error())
  76. }
  77. }
  78. func TestMissingVendorConfig(t *testing.T) {
  79. c := &config{}
  80. if c.VendorConfig("test").Bool("not_set") {
  81. t.Errorf("Expected false")
  82. }
  83. }