blob: e98c1365bf1ed840530d98c05c030e00574fcad6 [file] [log] [blame]
Colin Cross1b488422019-03-04 22:33:56 -08001// 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 (
Colin Cross1b488422019-03-04 22:33:56 -080018 "reflect"
19 "testing"
20)
21
22type pathDepsMutatorTestModule struct {
23 ModuleBase
24 props struct {
25 Foo string `android:"path"`
Colin Crossa3a97412019-03-18 12:24:29 -070026 Bar []string `android:"path,arch_variant"`
Colin Cross1b488422019-03-04 22:33:56 -080027 Baz *string `android:"path"`
28 Qux string
29 }
30
31 sourceDeps []string
32}
33
34func pathDepsMutatorTestModuleFactory() Module {
35 module := &pathDepsMutatorTestModule{}
36 module.AddProperties(&module.props)
Colin Crossa3a97412019-03-18 12:24:29 -070037 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Colin Cross1b488422019-03-04 22:33:56 -080038 return module
39}
40
41func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross41955e82019-05-29 14:40:35 -070042 ctx.VisitDirectDeps(func(dep Module) {
43 if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok {
44 p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
45 }
Colin Cross1b488422019-03-04 22:33:56 -080046 })
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"],
Colin Cross41955e82019-05-29 14:40:35 -070062 baz: ":c{.bar}",
Colin Cross1b488422019-03-04 22:33:56 -080063 qux: ":d",
64 }`,
65 deps: []string{"a", "b", "c"},
66 },
Colin Crossa3a97412019-03-18 12:24:29 -070067 {
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 },
Colin Cross1b488422019-03-04 22:33:56 -080084 }
85
Colin Cross1b488422019-03-04 22:33:56 -080086 for _, test := range tests {
87 t.Run(test.name, func(t *testing.T) {
Colin Crossa3a97412019-03-18 12:24:29 -070088 config := TestArchConfig(buildDir, nil)
89 ctx := NewTestArchContext()
Colin Cross1b488422019-03-04 22:33:56 -080090
91 ctx.RegisterModuleType("test", ModuleFactoryAdaptor(pathDepsMutatorTestModuleFactory))
92 ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory))
93
94 bp := test.bp + `
95 filegroup {
96 name: "a",
97 }
98
99 filegroup {
100 name: "b",
101 }
102
103 filegroup {
104 name: "c",
105 }
106
107 filegroup {
108 name: "d",
109 }
110 `
111
112 mockFS := map[string][]byte{
113 "Android.bp": []byte(bp),
114 }
115
116 ctx.MockFileSystem(mockFS)
117
118 ctx.Register()
119 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
120 FailIfErrored(t, errs)
121 _, errs = ctx.PrepareBuildActions(config)
122 FailIfErrored(t, errs)
123
Colin Crossa3a97412019-03-18 12:24:29 -0700124 m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule)
Colin Cross1b488422019-03-04 22:33:56 -0800125
126 if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
127 t.Errorf("want deps %q, got %q", w, g)
128 }
129 })
130 }
131}