Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | type pathDepsMutatorTestModule struct { |
| 25 | ModuleBase |
| 26 | props struct { |
| 27 | Foo string `android:"path"` |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 28 | Bar []string `android:"path,arch_variant"` |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 29 | Baz *string `android:"path"` |
| 30 | Qux string |
| 31 | } |
| 32 | |
| 33 | sourceDeps []string |
| 34 | } |
| 35 | |
| 36 | func pathDepsMutatorTestModuleFactory() Module { |
| 37 | module := &pathDepsMutatorTestModule{} |
| 38 | module.AddProperties(&module.props) |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 39 | InitAndroidArchModule(module, DeviceSupported, MultilibBoth) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 40 | return module |
| 41 | } |
| 42 | |
| 43 | func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame^] | 44 | ctx.VisitDirectDeps(func(dep Module) { |
| 45 | if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok { |
| 46 | p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep)) |
| 47 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 48 | }) |
| 49 | } |
| 50 | |
| 51 | func 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 Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame^] | 64 | baz: ":c{.bar}", |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 65 | qux: ":d", |
| 66 | }`, |
| 67 | deps: []string{"a", "b", "c"}, |
| 68 | }, |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 69 | { |
| 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 Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 86 | } |
| 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 Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 96 | config := TestArchConfig(buildDir, nil) |
| 97 | ctx := NewTestArchContext() |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 98 | |
| 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 Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 132 | m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 133 | |
| 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 | } |