blob: 56c3e38ca720550c11fe38254278f5e815c22c12 [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 Crossfaeb7aa2017-02-01 14:12:44 -080025)
26
27type dataFile struct {
28 path string
29 file string
30}
31
32var testDataTests = []struct {
33 name string
34 modules string
35 data []dataFile
36}{
37 {
38 name: "data files",
39 modules: `
40 test {
41 name: "foo",
42 data: [
43 "baz",
44 "bar/baz",
45 ],
46 }`,
47 data: []dataFile{
48 {"dir", "baz"},
49 {"dir", "bar/baz"},
50 },
51 },
52 {
53 name: "filegroup",
54 modules: `
55 filegroup {
56 name: "fg",
57 srcs: [
58 "baz",
59 "bar/baz",
60 ],
61 }
62
63 test {
64 name: "foo",
65 data: [":fg"],
66 }`,
67 data: []dataFile{
68 {"dir", "baz"},
69 {"dir", "bar/baz"},
70 },
71 },
72 {
73 name: "relative filegroup",
74 modules: `
75 filegroup {
76 name: "fg",
77 srcs: [
78 "bar/baz",
79 ],
80 path: "bar",
81 }
82
83 test {
84 name: "foo",
85 data: [":fg"],
86 }`,
87 data: []dataFile{
88 {"dir/bar", "baz"},
89 },
90 },
91 {
92 name: "relative filegroup trailing slash",
93 modules: `
94 filegroup {
95 name: "fg",
96 srcs: [
97 "bar/baz",
98 ],
99 path: "bar/",
100 }
101
102 test {
103 name: "foo",
104 data: [":fg"],
105 }`,
106 data: []dataFile{
107 {"dir/bar", "baz"},
108 },
109 },
110}
111
112func TestDataTests(t *testing.T) {
113 buildDir, err := ioutil.TempDir("", "soong_test_test")
114 if err != nil {
115 t.Fatal(err)
116 }
117 defer os.RemoveAll(buildDir)
118
Colin Cross6ccbc912017-10-10 23:07:38 -0700119 config := android.TestConfig(buildDir, nil)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800120
121 for _, test := range testDataTests {
122 t.Run(test.name, func(t *testing.T) {
Colin Crosscec81712017-07-13 14:43:27 -0700123 ctx := android.NewTestContext()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800124 ctx.MockFileSystem(map[string][]byte{
125 "Blueprints": []byte(`subdirs = ["dir"]`),
126 "dir/Blueprints": []byte(test.modules),
127 "dir/baz": nil,
128 "dir/bar/baz": nil,
129 })
Colin Cross36242852017-06-23 15:06:31 -0700130 ctx.RegisterModuleType("filegroup",
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -0700131 android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross36242852017-06-23 15:06:31 -0700132 ctx.RegisterModuleType("test",
133 android.ModuleFactoryAdaptor(newTest))
Colin Crosscec81712017-07-13 14:43:27 -0700134 ctx.Register()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800135
136 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
Logan Chien42039712018-03-12 16:29:17 +0800137 android.FailIfErrored(t, errs)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800138 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800139 android.FailIfErrored(t, errs)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800140
Colin Crosscec81712017-07-13 14:43:27 -0700141 foo := ctx.ModuleForTests("foo", "")
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800142
Colin Crosscec81712017-07-13 14:43:27 -0700143 got := foo.Module().(*testDataTest).data
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800144 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
Colin Cross36242852017-06-23 15:06:31 -0700174func newTest() android.Module {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800175 m := &testDataTest{}
Colin Cross36242852017-06-23 15:06:31 -0700176 m.AddProperties(&m.Properties)
177 android.InitAndroidModule(m)
178 return m
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800179}
180
181func (test *testDataTest) DepsMutator(ctx android.BottomUpMutatorContext) {
182 android.ExtractSourcesDeps(ctx, test.Properties.Data)
183}
184
185func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
186 test.data = ctx.ExpandSources(test.Properties.Data, nil)
187}