test_data_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 cc
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. "android/soong/android"
  21. )
  22. type dataFile struct {
  23. path string
  24. file string
  25. }
  26. var testDataTests = []struct {
  27. name string
  28. modules string
  29. data []dataFile
  30. }{
  31. {
  32. name: "data files",
  33. modules: `
  34. test {
  35. name: "foo",
  36. data: [
  37. "baz",
  38. "bar/baz",
  39. ],
  40. }`,
  41. data: []dataFile{
  42. {"dir", "baz"},
  43. {"dir", "bar/baz"},
  44. },
  45. },
  46. {
  47. name: "filegroup",
  48. modules: `
  49. filegroup {
  50. name: "fg",
  51. srcs: [
  52. "baz",
  53. "bar/baz",
  54. ],
  55. }
  56. test {
  57. name: "foo",
  58. data: [":fg"],
  59. }`,
  60. data: []dataFile{
  61. {"dir", "baz"},
  62. {"dir", "bar/baz"},
  63. },
  64. },
  65. {
  66. name: "relative filegroup",
  67. modules: `
  68. filegroup {
  69. name: "fg",
  70. srcs: [
  71. "bar/baz",
  72. ],
  73. path: "bar",
  74. }
  75. test {
  76. name: "foo",
  77. data: [":fg"],
  78. }`,
  79. data: []dataFile{
  80. {"dir/bar", "baz"},
  81. },
  82. },
  83. {
  84. name: "relative filegroup trailing slash",
  85. modules: `
  86. filegroup {
  87. name: "fg",
  88. srcs: [
  89. "bar/baz",
  90. ],
  91. path: "bar/",
  92. }
  93. test {
  94. name: "foo",
  95. data: [":fg"],
  96. }`,
  97. data: []dataFile{
  98. {"dir/bar", "baz"},
  99. },
  100. },
  101. }
  102. func TestDataTests(t *testing.T) {
  103. buildDir, err := ioutil.TempDir("", "soong_test_test")
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. defer os.RemoveAll(buildDir)
  108. for _, test := range testDataTests {
  109. t.Run(test.name, func(t *testing.T) {
  110. config := android.TestConfig(buildDir, nil, "", map[string][]byte{
  111. "dir/Android.bp": []byte(test.modules),
  112. "dir/baz": nil,
  113. "dir/bar/baz": nil,
  114. })
  115. ctx := android.NewTestContext(config)
  116. ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
  117. ctx.RegisterModuleType("test", newTest)
  118. ctx.Register()
  119. _, errs := ctx.ParseBlueprintsFiles("Android.bp")
  120. android.FailIfErrored(t, errs)
  121. _, errs = ctx.PrepareBuildActions(config)
  122. android.FailIfErrored(t, errs)
  123. foo := ctx.ModuleForTests("foo", "")
  124. got := foo.Module().(*testDataTest).data
  125. if len(got) != len(test.data) {
  126. t.Errorf("expected %d data files, got %d",
  127. len(test.data), len(got))
  128. }
  129. for i := range got {
  130. if i >= len(test.data) {
  131. break
  132. }
  133. path := filepath.Join(test.data[i].path, test.data[i].file)
  134. if test.data[i].file != got[i].Rel() ||
  135. path != got[i].String() {
  136. t.Errorf("expected %s:%s got %s:%s",
  137. path, test.data[i].file,
  138. got[i].String(), got[i].Rel())
  139. }
  140. }
  141. })
  142. }
  143. }
  144. type testDataTest struct {
  145. android.ModuleBase
  146. data android.Paths
  147. Properties struct {
  148. Data []string `android:"path"`
  149. }
  150. }
  151. func newTest() android.Module {
  152. m := &testDataTest{}
  153. m.AddProperties(&m.Properties)
  154. android.InitAndroidModule(m)
  155. return m
  156. }
  157. func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  158. test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
  159. }