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