1// 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	"io/ioutil"
19	"os"
20	"path/filepath"
21	"testing"
22
23	"android/soong/android"
24)
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
118	for _, test := range testDataTests {
119		t.Run(test.name, func(t *testing.T) {
120			config := android.TestConfig(buildDir, nil, "", map[string][]byte{
121				"dir/Android.bp": []byte(test.modules),
122				"dir/baz":        nil,
123				"dir/bar/baz":    nil,
124			})
125			ctx := android.NewTestContext(config)
126			ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
127			ctx.RegisterModuleType("test", newTest)
128			ctx.Register()
129
130			_, errs := ctx.ParseBlueprintsFiles("Blueprints")
131			android.FailIfErrored(t, errs)
132			_, errs = ctx.PrepareBuildActions(config)
133			android.FailIfErrored(t, errs)
134
135			foo := ctx.ModuleForTests("foo", "")
136
137			got := foo.Module().(*testDataTest).data
138			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() {
151					t.Errorf("expected %s:%s got %s:%s",
152						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 {
164		Data []string `android:"path"`
165	}
166}
167
168func newTest() android.Module {
169	m := &testDataTest{}
170	m.AddProperties(&m.Properties)
171	android.InitAndroidModule(m)
172	return m
173}
174
175func (test *testDataTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
176	test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
177}
178