blob: 962ff26851038fcc203c8471566fe360e3dbc5c2 [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 Cross6ccbc912017-10-10 23:07:38 -0700118 config := android.TestConfig(buildDir, nil)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800119
120 for _, test := range testDataTests {
121 t.Run(test.name, func(t *testing.T) {
Colin Crosscec81712017-07-13 14:43:27 -0700122 ctx := android.NewTestContext()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800123 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 })
Colin Cross4b49b762019-11-22 15:25:03 -0800129 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
130 ctx.RegisterModuleType("test", newTest)
Colin Crosscec81712017-07-13 14:43:27 -0700131 ctx.Register()
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800132
133 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
Logan Chien42039712018-03-12 16:29:17 +0800134 android.FailIfErrored(t, errs)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800135 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800136 android.FailIfErrored(t, errs)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800137
Colin Crosscec81712017-07-13 14:43:27 -0700138 foo := ctx.ModuleForTests("foo", "")
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800139
Colin Crosscec81712017-07-13 14:43:27 -0700140 got := foo.Module().(*testDataTest).data
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800141 if len(got) != len(test.data) {
142 t.Errorf("expected %d data files, got %d",
143 len(test.data), len(got))
144 }
145
146 for i := range got {
147 if i >= len(test.data) {
148 break
149 }
150
151 path := filepath.Join(test.data[i].path, test.data[i].file)
152 if test.data[i].file != got[i].Rel() ||
153 path != got[i].String() {
Dan Willemsen59339a22018-07-22 21:18:45 -0700154 t.Errorf("expected %s:%s got %s:%s",
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800155 path, test.data[i].file,
156 got[i].String(), got[i].Rel())
157 }
158 }
159 })
160 }
161}
162
163type testDataTest struct {
164 android.ModuleBase
165 data android.Paths
166 Properties struct {
Colin Cross27b922f2019-03-04 22:35:41 -0800167 Data []string `android:"path"`
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800168 }
169}
170
Colin Cross36242852017-06-23 15:06:31 -0700171func newTest() android.Module {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800172 m := &testDataTest{}
Colin Cross36242852017-06-23 15:06:31 -0700173 m.AddProperties(&m.Properties)
174 android.InitAndroidModule(m)
175 return m
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800176}
177
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800178func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800179 test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800180}