config_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "path/filepath"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. )
  22. func validateConfigAnnotations(configurable jsonConfigurable) (err error) {
  23. reflectType := reflect.TypeOf(configurable)
  24. reflectType = reflectType.Elem()
  25. for i := 0; i < reflectType.NumField(); i++ {
  26. field := reflectType.Field(i)
  27. jsonTag := field.Tag.Get("json")
  28. // Check for mistakes in the json tag
  29. if jsonTag != "" && !strings.HasPrefix(jsonTag, ",") {
  30. if !strings.Contains(jsonTag, ",") {
  31. // Probably an accidental rename, most likely "omitempty" instead of ",omitempty"
  32. return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+
  33. "Did you mean to use an annotation of %q?\n"+
  34. "(Alternatively, to change the json name of the field, rename the field in source instead.)",
  35. reflectType.Name(), field.Name, field.Tag, jsonTag, ","+jsonTag)
  36. } else {
  37. // Although this rename was probably intentional,
  38. // a json annotation is still more confusing than renaming the source variable
  39. requestedName := strings.Split(jsonTag, ",")[0]
  40. return fmt.Errorf("Field %s.%s has tag %s which specifies to change its json field name to %q.\n"+
  41. "To change the json name of the field, rename the field in source instead.",
  42. reflectType.Name(), field.Name, field.Tag, requestedName)
  43. }
  44. }
  45. }
  46. return nil
  47. }
  48. type configType struct {
  49. PopulateMe *bool `json:"omitempty"`
  50. }
  51. func (c *configType) SetDefaultConfig() {
  52. }
  53. // tests that ValidateConfigAnnotation works
  54. func TestValidateConfigAnnotations(t *testing.T) {
  55. config := configType{}
  56. err := validateConfigAnnotations(&config)
  57. expectedError := `Field configType.PopulateMe has tag json:"omitempty" which specifies to change its json field name to "omitempty".
  58. Did you mean to use an annotation of ",omitempty"?
  59. (Alternatively, to change the json name of the field, rename the field in source instead.)`
  60. if err.Error() != expectedError {
  61. t.Errorf("Incorrect error; expected:\n"+
  62. "%s\n"+
  63. "got:\n"+
  64. "%s",
  65. expectedError, err.Error())
  66. }
  67. }
  68. // run validateConfigAnnotations against each type that might have json annotations
  69. func TestProductConfigAnnotations(t *testing.T) {
  70. err := validateConfigAnnotations(&productVariables{})
  71. if err != nil {
  72. t.Errorf(err.Error())
  73. }
  74. }
  75. func TestMissingVendorConfig(t *testing.T) {
  76. c := &config{}
  77. if c.VendorConfig("test").Bool("not_set") {
  78. t.Errorf("Expected false")
  79. }
  80. }
  81. func verifyProductVariableMarshaling(t *testing.T, v productVariables) {
  82. dir := t.TempDir()
  83. path := filepath.Join(dir, "test.variables")
  84. err := saveToConfigFile(&v, path)
  85. if err != nil {
  86. t.Errorf("Couldn't save default product config: %q", err)
  87. }
  88. var v2 productVariables
  89. err = loadFromConfigFile(&v2, path)
  90. if err != nil {
  91. t.Errorf("Couldn't load default product config: %q", err)
  92. }
  93. }
  94. func TestDefaultProductVariableMarshaling(t *testing.T) {
  95. v := productVariables{}
  96. v.SetDefaultConfig()
  97. verifyProductVariableMarshaling(t, v)
  98. }
  99. func TestBootJarsMarshaling(t *testing.T) {
  100. v := productVariables{}
  101. v.SetDefaultConfig()
  102. v.BootJars = ConfiguredJarList{
  103. apexes: []string{"apex"},
  104. jars: []string{"jar"},
  105. }
  106. verifyProductVariableMarshaling(t, v)
  107. }
  108. func assertStringEquals(t *testing.T, expected, actual string) {
  109. if actual != expected {
  110. t.Errorf("expected %q found %q", expected, actual)
  111. }
  112. }
  113. func TestConfiguredJarList(t *testing.T) {
  114. list1 := CreateTestConfiguredJarList([]string{"apex1:jarA"})
  115. t.Run("create", func(t *testing.T) {
  116. assertStringEquals(t, "apex1:jarA", list1.String())
  117. })
  118. t.Run("create invalid - missing apex", func(t *testing.T) {
  119. defer func() {
  120. err := recover().(error)
  121. assertStringEquals(t, "malformed (apex, jar) pair: 'jarA', expected format: <apex>:<jar>", err.Error())
  122. }()
  123. CreateTestConfiguredJarList([]string{"jarA"})
  124. })
  125. t.Run("create invalid - empty apex", func(t *testing.T) {
  126. defer func() {
  127. err := recover().(error)
  128. assertStringEquals(t, "invalid apex '' in <apex>:<jar> pair ':jarA', expected format: <apex>:<jar>", err.Error())
  129. }()
  130. CreateTestConfiguredJarList([]string{":jarA"})
  131. })
  132. list2 := list1.Append("apex2", "jarB")
  133. t.Run("append", func(t *testing.T) {
  134. assertStringEquals(t, "apex1:jarA,apex2:jarB", list2.String())
  135. })
  136. t.Run("append does not modify", func(t *testing.T) {
  137. assertStringEquals(t, "apex1:jarA", list1.String())
  138. })
  139. // Make sure that two lists created by appending to the same list do not share storage.
  140. list3 := list1.Append("apex3", "jarC")
  141. t.Run("append does not share", func(t *testing.T) {
  142. assertStringEquals(t, "apex1:jarA,apex2:jarB", list2.String())
  143. assertStringEquals(t, "apex1:jarA,apex3:jarC", list3.String())
  144. })
  145. list4 := list3.RemoveList(list1)
  146. t.Run("remove", func(t *testing.T) {
  147. assertStringEquals(t, "apex3:jarC", list4.String())
  148. })
  149. t.Run("remove does not modify", func(t *testing.T) {
  150. assertStringEquals(t, "apex1:jarA,apex3:jarC", list3.String())
  151. })
  152. // Make sure that two lists created by removing from the same list do not share storage.
  153. list5 := list3.RemoveList(CreateTestConfiguredJarList([]string{"apex3:jarC"}))
  154. t.Run("remove", func(t *testing.T) {
  155. assertStringEquals(t, "apex3:jarC", list4.String())
  156. assertStringEquals(t, "apex1:jarA", list5.String())
  157. })
  158. }