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