blob: 6471a3cf5110a3fbda697ed9840c14f6cf67ec40 [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"`
28 Bar []string `android:"path"`
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 InitAndroidModule(module)
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
69 buildDir, err := ioutil.TempDir("", "soong_path_properties_test")
70 if err != nil {
71 t.Fatal(err)
72 }
73 defer os.RemoveAll(buildDir)
74
75 for _, test := range tests {
76 t.Run(test.name, func(t *testing.T) {
77 config := TestConfig(buildDir, nil)
78 ctx := NewTestContext()
79
80 ctx.RegisterModuleType("test", ModuleFactoryAdaptor(pathDepsMutatorTestModuleFactory))
81 ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory))
82
83 bp := test.bp + `
84 filegroup {
85 name: "a",
86 }
87
88 filegroup {
89 name: "b",
90 }
91
92 filegroup {
93 name: "c",
94 }
95
96 filegroup {
97 name: "d",
98 }
99 `
100
101 mockFS := map[string][]byte{
102 "Android.bp": []byte(bp),
103 }
104
105 ctx.MockFileSystem(mockFS)
106
107 ctx.Register()
108 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
109 FailIfErrored(t, errs)
110 _, errs = ctx.PrepareBuildActions(config)
111 FailIfErrored(t, errs)
112
113 m := ctx.ModuleForTests("foo", "").Module().(*pathDepsMutatorTestModule)
114
115 if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) {
116 t.Errorf("want deps %q, got %q", w, g)
117 }
118 })
119 }
120}