blob: a62116677de1eda11521d70b7a0923000deb1f5c [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 (
Colin Crossfaeb7aa2017-02-01 14:12:44 -080018 "io/ioutil"
19 "os"
20 "path/filepath"
21 "testing"
22
23 "android/soong/android"
Colin Crossfaeb7aa2017-02-01 14:12:44 -080024)
25
26type dataFile struct {
27 path string
28 file string
29}
30
31var 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
111func 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
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800118 for _, test := range testDataTests {
119 t.Run(test.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800120 config := android.TestConfig(buildDir, nil, "", map[string][]byte{
121 "dir/Android.bp": []byte(test.modules),
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800122 "dir/baz": nil,
123 "dir/bar/baz": nil,
124 })
Colin Crossae8600b2020-10-29 17:09:13 -0700125 ctx := android.NewTestContext(config)
Colin Cross4b49b762019-11-22 15:25:03 -0800126 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
127 ctx.RegisterModuleType("test", newTest)
Colin Crossae8600b2020-10-29 17:09:13 -0700128 ctx.Register()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800129
Lukacs T. Berkib838b0a2021-09-02 11:46:24 +0200130 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800131 android.FailIfErrored(t, errs)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800132 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800133 android.FailIfErrored(t, errs)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800134
Colin Crosscec81712017-07-13 14:43:27 -0700135 foo := ctx.ModuleForTests("foo", "")
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800136
Colin Crosscec81712017-07-13 14:43:27 -0700137 got := foo.Module().(*testDataTest).data
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800138 if len(got) != len(test.data) {
139 t.Errorf("expected %d data files, got %d",
140 len(test.data), len(got))
141 }
142
143 for i := range got {
144 if i >= len(test.data) {
145 break
146 }
147
148 path := filepath.Join(test.data[i].path, test.data[i].file)
149 if test.data[i].file != got[i].Rel() ||
150 path != got[i].String() {
Dan Willemsen59339a22018-07-22 21:18:45 -0700151 t.Errorf("expected %s:%s got %s:%s",
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800152 path, test.data[i].file,
153 got[i].String(), got[i].Rel())
154 }
155 }
156 })
157 }
158}
159
160type testDataTest struct {
161 android.ModuleBase
162 data android.Paths
163 Properties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800164 Data []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800165 }
166}
167
Colin Cross36242852017-06-23 15:06:31 -0700168func newTest() android.Module {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800169 m := &testDataTest{}
Colin Cross36242852017-06-23 15:06:31 -0700170 m.AddProperties(&m.Properties)
171 android.InitAndroidModule(m)
172 return m
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800173}
174
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800175func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800176 test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800177}