• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 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 android
16
17import (
18	"io/ioutil"
19	"os"
20	"reflect"
21	"testing"
22)
23
24type pathDepsMutatorTestModule struct {
25	ModuleBase
26	props struct {
27		Foo string   `android:"path"`
28		Bar []string `android:"path,arch_variant"`
29		Baz *string  `android:"path"`
30		Qux string
31	}
32
33	sourceDeps []string
34}
35
36func pathDepsMutatorTestModuleFactory() Module {
37	module := &pathDepsMutatorTestModule{}
38	module.AddProperties(&module.props)
39	InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
40	return module
41}
42
43func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
44	ctx.VisitDirectDepsWithTag(SourceDepTag, func(dep Module) {
45		p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
46	})
47}
48
49func TestPathDepsMutator(t *testing.T) {
50	tests := []struct {
51		name string
52		bp   string
53		deps []string
54	}{
55		{
56			name: "all",
57			bp: `
58			test {
59				name: "foo",
60				foo: ":a",
61				bar: [":b"],
62				baz: ":c",
63				qux: ":d",
64			}`,
65			deps: []string{"a", "b", "c"},
66		},
67		{
68			name: "arch variant",
69			bp: `
70			test {
71				name: "foo",
72				arch: {
73					arm64: {
74						bar: [":a"],
75					},
76					arm: {
77						bar: [":b"],
78					},
79				},
80				bar: [":c"],
81			}`,
82			deps: []string{"c", "a"},
83		},
84	}
85
86	buildDir, err := ioutil.TempDir("", "soong_path_properties_test")
87	if err != nil {
88		t.Fatal(err)
89	}
90	defer os.RemoveAll(buildDir)
91
92	for _, test := range tests {
93		t.Run(test.name, func(t *testing.T) {
94			config := TestArchConfig(buildDir, nil)
95			ctx := NewTestArchContext()
96
97			ctx.RegisterModuleType("test", ModuleFactoryAdaptor(pathDepsMutatorTestModuleFactory))
98			ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory))
99
100			bp := test.bp + `
101				filegroup {
102					name: "a",
103				}
104
105				filegroup {
106					name: "b",
107				}
108
109				filegroup {
110					name: "c",
111				}
112
113				filegroup {
114					name: "d",
115				}
116			`
117
118			mockFS := map[string][]byte{
119				"Android.bp": []byte(bp),
120			}
121
122			ctx.MockFileSystem(mockFS)
123
124			ctx.Register()
125			_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
126			FailIfErrored(t, errs)
127			_, errs = ctx.PrepareBuildActions(config)
128			FailIfErrored(t, errs)
129
130			m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule)
131
132			if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
133				t.Errorf("want deps %q, got %q", w, g)
134			}
135		})
136	}
137}
138