blob: fa187fa6f0dff95dbf940e056f229dbf384c37cd [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 (
18 "io/ioutil"
19 "os"
20 "reflect"
21 "testing"
22)
23
24type pathDepsMutatorTestModule struct {
25 ModuleBase
26 props struct {
27 Foo string `android:"path"`
Colin Crossa3a97412019-03-18 12:24:29 -070028 Bar []string `android:"path,arch_variant"`
Colin Cross1b488422019-03-04 22:33:56 -080029 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)
Colin Crossa3a97412019-03-18 12:24:29 -070039 InitAndroidArchModule(module, DeviceSupported, MultilibBoth)
Colin Cross1b488422019-03-04 22:33:56 -080040 return module
41}
42
43func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross41955e82019-05-29 14:40:35 -070044 ctx.VisitDirectDeps(func(dep Module) {
45 if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok {
46 p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep))
47 }
Colin Cross1b488422019-03-04 22:33:56 -080048 })
49}
50
51func TestPathDepsMutator(t *testing.T) {
52 tests := []struct {
53 name string
54 bp string
55 deps []string
56 }{
57 {
58 name: "all",
59 bp: `
60 test {
61 name: "foo",
62 foo: ":a",
63 bar: [":b"],
Colin Cross41955e82019-05-29 14:40:35 -070064 baz: ":c{.bar}",
Colin Cross1b488422019-03-04 22:33:56 -080065 qux: ":d",
66 }`,
67 deps: []string{"a", "b", "c"},
68 },
Colin Crossa3a97412019-03-18 12:24:29 -070069 {
70 name: "arch variant",
71 bp: `
72 test {
73 name: "foo",
74 arch: {
75 arm64: {
76 bar: [":a"],
77 },
78 arm: {
79 bar: [":b"],
80 },
81 },
82 bar: [":c"],
83 }`,
84 deps: []string{"c", "a"},
85 },
Colin Cross1b488422019-03-04 22:33:56 -080086 }
87
88 buildDir, err := ioutil.TempDir("", "soong_path_properties_test")
89 if err != nil {
90 t.Fatal(err)
91 }
92 defer os.RemoveAll(buildDir)
93
94 for _, test := range tests {
95 t.Run(test.name, func(t *testing.T) {
Colin Crossa3a97412019-03-18 12:24:29 -070096 config := TestArchConfig(buildDir, nil)
97 ctx := NewTestArchContext()
Colin Cross1b488422019-03-04 22:33:56 -080098
99 ctx.RegisterModuleType("test", ModuleFactoryAdaptor(pathDepsMutatorTestModuleFactory))
100 ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory))
101
102 bp := test.bp + `
103 filegroup {
104 name: "a",
105 }
106
107 filegroup {
108 name: "b",
109 }
110
111 filegroup {
112 name: "c",
113 }
114
115 filegroup {
116 name: "d",
117 }
118 `
119
120 mockFS := map[string][]byte{
121 "Android.bp": []byte(bp),
122 }
123
124 ctx.MockFileSystem(mockFS)
125
126 ctx.Register()
127 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
128 FailIfErrored(t, errs)
129 _, errs = ctx.PrepareBuildActions(config)
130 FailIfErrored(t, errs)
131
Colin Crossa3a97412019-03-18 12:24:29 -0700132 m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule)
Colin Cross1b488422019-03-04 22:33:56 -0800133
134 if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
135 t.Errorf("want deps %q, got %q", w, g)
136 }
137 })
138 }
139}