bp2build_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2021 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 config
  15. import (
  16. "testing"
  17. "android/soong/android"
  18. )
  19. func TestExpandVars(t *testing.T) {
  20. android_arm64_config := android.TestConfig("out", nil, "", nil)
  21. android_arm64_config.BuildOS = android.Android
  22. android_arm64_config.BuildArch = android.Arm64
  23. testCases := []struct {
  24. description string
  25. config android.Config
  26. stringScope exportedStringVariables
  27. stringListScope exportedStringListVariables
  28. configVars exportedConfigDependingVariables
  29. toExpand string
  30. expectedValues []string
  31. }{
  32. {
  33. description: "no expansion for non-interpolated value",
  34. toExpand: "foo",
  35. expectedValues: []string{"foo"},
  36. },
  37. {
  38. description: "single level expansion for string var",
  39. stringScope: exportedStringVariables{
  40. "foo": "bar",
  41. },
  42. toExpand: "${foo}",
  43. expectedValues: []string{"bar"},
  44. },
  45. {
  46. description: "single level expansion string list var",
  47. stringListScope: exportedStringListVariables{
  48. "foo": []string{"bar"},
  49. },
  50. toExpand: "${foo}",
  51. expectedValues: []string{"bar"},
  52. },
  53. {
  54. description: "mixed level expansion for string list var",
  55. stringScope: exportedStringVariables{
  56. "foo": "${bar}",
  57. "qux": "hello",
  58. },
  59. stringListScope: exportedStringListVariables{
  60. "bar": []string{"baz", "${qux}"},
  61. },
  62. toExpand: "${foo}",
  63. expectedValues: []string{"baz hello"},
  64. },
  65. {
  66. description: "double level expansion",
  67. stringListScope: exportedStringListVariables{
  68. "foo": []string{"${bar}"},
  69. "bar": []string{"baz"},
  70. },
  71. toExpand: "${foo}",
  72. expectedValues: []string{"baz"},
  73. },
  74. {
  75. description: "double level expansion with a literal",
  76. stringListScope: exportedStringListVariables{
  77. "a": []string{"${b}", "c"},
  78. "b": []string{"d"},
  79. },
  80. toExpand: "${a}",
  81. expectedValues: []string{"d c"},
  82. },
  83. {
  84. description: "double level expansion, with two variables in a string",
  85. stringListScope: exportedStringListVariables{
  86. "a": []string{"${b} ${c}"},
  87. "b": []string{"d"},
  88. "c": []string{"e"},
  89. },
  90. toExpand: "${a}",
  91. expectedValues: []string{"d e"},
  92. },
  93. {
  94. description: "triple level expansion with two variables in a string",
  95. stringListScope: exportedStringListVariables{
  96. "a": []string{"${b} ${c}"},
  97. "b": []string{"${c}", "${d}"},
  98. "c": []string{"${d}"},
  99. "d": []string{"foo"},
  100. },
  101. toExpand: "${a}",
  102. expectedValues: []string{"foo foo foo"},
  103. },
  104. {
  105. description: "expansion with config depending vars",
  106. configVars: exportedConfigDependingVariables{
  107. "a": func(c android.Config) string { return c.BuildOS.String() },
  108. "b": func(c android.Config) string { return c.BuildArch.String() },
  109. },
  110. config: android_arm64_config,
  111. toExpand: "${a}-${b}",
  112. expectedValues: []string{"android-arm64"},
  113. },
  114. {
  115. description: "double level multi type expansion",
  116. stringListScope: exportedStringListVariables{
  117. "platform": []string{"${os}-${arch}"},
  118. "const": []string{"const"},
  119. },
  120. configVars: exportedConfigDependingVariables{
  121. "os": func(c android.Config) string { return c.BuildOS.String() },
  122. "arch": func(c android.Config) string { return c.BuildArch.String() },
  123. "foo": func(c android.Config) string { return "foo" },
  124. },
  125. config: android_arm64_config,
  126. toExpand: "${const}/${platform}/${foo}",
  127. expectedValues: []string{"const/android-arm64/foo"},
  128. },
  129. }
  130. for _, testCase := range testCases {
  131. t.Run(testCase.description, func(t *testing.T) {
  132. output, _ := expandVar(testCase.config, testCase.toExpand, testCase.stringScope, testCase.stringListScope, testCase.configVars)
  133. if len(output) != len(testCase.expectedValues) {
  134. t.Errorf("Expected %d values, got %d", len(testCase.expectedValues), len(output))
  135. }
  136. for i, actual := range output {
  137. expectedValue := testCase.expectedValues[i]
  138. if actual != expectedValue {
  139. t.Errorf("Actual value '%s' doesn't match expected value '%s'", actual, expectedValue)
  140. }
  141. }
  142. })
  143. }
  144. }
  145. func TestBazelToolchainVars(t *testing.T) {
  146. testCases := []struct {
  147. name string
  148. config android.Config
  149. vars []bazelVarExporter
  150. expectedOut string
  151. }{
  152. {
  153. name: "exports strings",
  154. vars: []bazelVarExporter{
  155. exportedStringVariables{
  156. "a": "b",
  157. "c": "d",
  158. },
  159. },
  160. expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
  161. _a = "b"
  162. _c = "d"
  163. constants = struct(
  164. a = _a,
  165. c = _c,
  166. )`,
  167. },
  168. {
  169. name: "exports string lists",
  170. vars: []bazelVarExporter{
  171. exportedStringListVariables{
  172. "a": []string{"b1", "b2"},
  173. "c": []string{"d1", "d2"},
  174. },
  175. },
  176. expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
  177. _a = [
  178. "b1",
  179. "b2",
  180. ]
  181. _c = [
  182. "d1",
  183. "d2",
  184. ]
  185. constants = struct(
  186. a = _a,
  187. c = _c,
  188. )`,
  189. },
  190. {
  191. name: "exports string lists dicts",
  192. vars: []bazelVarExporter{
  193. exportedStringListDictVariables{
  194. "a": map[string][]string{"b1": []string{"b2"}},
  195. "c": map[string][]string{"d1": []string{"d2"}},
  196. },
  197. },
  198. expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
  199. _a = {
  200. "b1": ["b2"],
  201. }
  202. _c = {
  203. "d1": ["d2"],
  204. }
  205. constants = struct(
  206. a = _a,
  207. c = _c,
  208. )`,
  209. },
  210. {
  211. name: "sorts across types",
  212. vars: []bazelVarExporter{
  213. exportedStringVariables{
  214. "b": "b-val",
  215. "d": "d-val",
  216. },
  217. exportedStringListVariables{
  218. "c": []string{"c-val"},
  219. "e": []string{"e-val"},
  220. },
  221. exportedStringListDictVariables{
  222. "a": map[string][]string{"a1": []string{"a2"}},
  223. "f": map[string][]string{"f1": []string{"f2"}},
  224. },
  225. },
  226. expectedOut: `# GENERATED FOR BAZEL FROM SOONG. DO NOT EDIT.
  227. _a = {
  228. "a1": ["a2"],
  229. }
  230. _b = "b-val"
  231. _c = ["c-val"]
  232. _d = "d-val"
  233. _e = ["e-val"]
  234. _f = {
  235. "f1": ["f2"],
  236. }
  237. constants = struct(
  238. a = _a,
  239. b = _b,
  240. c = _c,
  241. d = _d,
  242. e = _e,
  243. f = _f,
  244. )`,
  245. },
  246. }
  247. for _, tc := range testCases {
  248. t.Run(tc.name, func(t *testing.T) {
  249. out := bazelToolchainVars(tc.config, tc.vars...)
  250. if out != tc.expectedOut {
  251. t.Errorf("Expected \n%s, got \n%s", tc.expectedOut, out)
  252. }
  253. })
  254. }
  255. }