blob: 434edcdf6def118a8af2b032c6b573ee819818d0 [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"
Colin Cross795c3772017-03-16 16:50:10 -070025 "android/soong/genrule"
Colin Crossfaeb7aa2017-02-01 14:12:44 -080026)
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
Colin Cross6ccbc912017-10-10 23:07:38 -0700120 config := android.TestConfig(buildDir, nil)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800121
122 for _, test := range testDataTests {
123 t.Run(test.name, func(t *testing.T) {
Colin Crosscec81712017-07-13 14:43:27 -0700124 ctx := android.NewTestContext()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800125 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 })
Colin Cross36242852017-06-23 15:06:31 -0700131 ctx.RegisterModuleType("filegroup",
132 android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
133 ctx.RegisterModuleType("test",
134 android.ModuleFactoryAdaptor(newTest))
Colin Crosscec81712017-07-13 14:43:27 -0700135 ctx.Register()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800136
137 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
138 fail(t, errs)
139 _, errs = ctx.PrepareBuildActions(config)
140 fail(t, errs)
141
Colin Crosscec81712017-07-13 14:43:27 -0700142 foo := ctx.ModuleForTests("foo", "")
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800143
Colin Crosscec81712017-07-13 14:43:27 -0700144 got := foo.Module().(*testDataTest).data
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800145 if len(got) != len(test.data) {
146 t.Errorf("expected %d data files, got %d",
147 len(test.data), len(got))
148 }
149
150 for i := range got {
151 if i >= len(test.data) {
152 break
153 }
154
155 path := filepath.Join(test.data[i].path, test.data[i].file)
156 if test.data[i].file != got[i].Rel() ||
157 path != got[i].String() {
158 fmt.Errorf("expected %s:%s got %s:%s",
159 path, test.data[i].file,
160 got[i].String(), got[i].Rel())
161 }
162 }
163 })
164 }
165}
166
167type testDataTest struct {
168 android.ModuleBase
169 data android.Paths
170 Properties struct {
171 Data []string
172 }
173}
174
Colin Cross36242852017-06-23 15:06:31 -0700175func newTest() android.Module {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800176 m := &testDataTest{}
Colin Cross36242852017-06-23 15:06:31 -0700177 m.AddProperties(&m.Properties)
178 android.InitAndroidModule(m)
179 return m
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800180}
181
182func (test *testDataTest) DepsMutator(ctx android.BottomUpMutatorContext) {
183 android.ExtractSourcesDeps(ctx, test.Properties.Data)
184}
185
186func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
187 test.data = ctx.ExpandSources(test.Properties.Data, nil)
188}
189
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800190func fail(t *testing.T, errs []error) {
191 if len(errs) > 0 {
192 for _, err := range errs {
193 t.Error(err)
194 }
195 t.FailNow()
196 }
197}