Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 17 | import ( |
| 18 | "testing" |
| 19 | ) |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 20 | |
| 21 | func TestSrcIsModule(t *testing.T) { |
| 22 | type args struct { |
| 23 | s string |
| 24 | } |
| 25 | tests := []struct { |
| 26 | name string |
| 27 | args args |
| 28 | wantModule string |
| 29 | }{ |
| 30 | { |
| 31 | name: "file", |
| 32 | args: args{ |
| 33 | s: "foo", |
| 34 | }, |
| 35 | wantModule: "", |
| 36 | }, |
| 37 | { |
| 38 | name: "module", |
| 39 | args: args{ |
| 40 | s: ":foo", |
| 41 | }, |
| 42 | wantModule: "foo", |
| 43 | }, |
| 44 | { |
| 45 | name: "tag", |
| 46 | args: args{ |
| 47 | s: ":foo{.bar}", |
| 48 | }, |
| 49 | wantModule: "foo{.bar}", |
| 50 | }, |
| 51 | { |
| 52 | name: "extra colon", |
| 53 | args: args{ |
| 54 | s: ":foo:bar", |
| 55 | }, |
| 56 | wantModule: "foo:bar", |
| 57 | }, |
| 58 | } |
| 59 | for _, tt := range tests { |
| 60 | t.Run(tt.name, func(t *testing.T) { |
| 61 | if gotModule := SrcIsModule(tt.args.s); gotModule != tt.wantModule { |
| 62 | t.Errorf("SrcIsModule() = %v, want %v", gotModule, tt.wantModule) |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestSrcIsModuleWithTag(t *testing.T) { |
| 69 | type args struct { |
| 70 | s string |
| 71 | } |
| 72 | tests := []struct { |
| 73 | name string |
| 74 | args args |
| 75 | wantModule string |
| 76 | wantTag string |
| 77 | }{ |
| 78 | { |
| 79 | name: "file", |
| 80 | args: args{ |
| 81 | s: "foo", |
| 82 | }, |
| 83 | wantModule: "", |
| 84 | wantTag: "", |
| 85 | }, |
| 86 | { |
| 87 | name: "module", |
| 88 | args: args{ |
| 89 | s: ":foo", |
| 90 | }, |
| 91 | wantModule: "foo", |
| 92 | wantTag: "", |
| 93 | }, |
| 94 | { |
| 95 | name: "tag", |
| 96 | args: args{ |
| 97 | s: ":foo{.bar}", |
| 98 | }, |
| 99 | wantModule: "foo", |
| 100 | wantTag: ".bar", |
| 101 | }, |
| 102 | { |
| 103 | name: "empty tag", |
| 104 | args: args{ |
| 105 | s: ":foo{}", |
| 106 | }, |
| 107 | wantModule: "foo", |
| 108 | wantTag: "", |
| 109 | }, |
| 110 | { |
| 111 | name: "extra colon", |
| 112 | args: args{ |
| 113 | s: ":foo:bar", |
| 114 | }, |
| 115 | wantModule: "foo:bar", |
| 116 | }, |
| 117 | { |
| 118 | name: "invalid tag", |
| 119 | args: args{ |
| 120 | s: ":foo{.bar", |
| 121 | }, |
| 122 | wantModule: "foo{.bar", |
| 123 | }, |
| 124 | { |
| 125 | name: "invalid tag 2", |
| 126 | args: args{ |
| 127 | s: ":foo.bar}", |
| 128 | }, |
| 129 | wantModule: "foo.bar}", |
| 130 | }, |
| 131 | } |
| 132 | for _, tt := range tests { |
| 133 | t.Run(tt.name, func(t *testing.T) { |
| 134 | gotModule, gotTag := SrcIsModuleWithTag(tt.args.s) |
| 135 | if gotModule != tt.wantModule { |
| 136 | t.Errorf("SrcIsModuleWithTag() gotModule = %v, want %v", gotModule, tt.wantModule) |
| 137 | } |
| 138 | if gotTag != tt.wantTag { |
| 139 | t.Errorf("SrcIsModuleWithTag() gotTag = %v, want %v", gotTag, tt.wantTag) |
| 140 | } |
| 141 | }) |
| 142 | } |
| 143 | } |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 144 | |
| 145 | type depsModule struct { |
| 146 | ModuleBase |
| 147 | props struct { |
| 148 | Deps []string |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func (m *depsModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 153 | } |
| 154 | |
| 155 | func (m *depsModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 156 | ctx.AddDependency(ctx.Module(), nil, m.props.Deps...) |
| 157 | } |
| 158 | |
| 159 | func depsModuleFactory() Module { |
| 160 | m := &depsModule{} |
| 161 | m.AddProperties(&m.props) |
| 162 | InitAndroidModule(m) |
| 163 | return m |
| 164 | } |
| 165 | |
| 166 | func TestErrorDependsOnDisabledModule(t *testing.T) { |
| 167 | ctx := NewTestContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 168 | ctx.RegisterModuleType("deps", depsModuleFactory) |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 169 | |
| 170 | bp := ` |
| 171 | deps { |
| 172 | name: "foo", |
| 173 | deps: ["bar"], |
| 174 | } |
| 175 | deps { |
| 176 | name: "bar", |
| 177 | enabled: false, |
| 178 | } |
| 179 | ` |
| 180 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 181 | config := TestConfig(buildDir, nil, bp, nil) |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 182 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 183 | ctx.Register(config) |
Jooyung Han | d48f3c3 | 2019-08-23 11:18:57 +0900 | [diff] [blame] | 184 | |
| 185 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 186 | FailIfErrored(t, errs) |
| 187 | _, errs = ctx.PrepareBuildActions(config) |
| 188 | FailIfNoMatchingErrors(t, `module "foo": depends on disabled module "bar"`, errs) |
| 189 | } |
Jingwen Chen | ce679d2 | 2020-09-23 04:30:02 +0000 | [diff] [blame^] | 190 | |
| 191 | func TestValidateCorrectBuildParams(t *testing.T) { |
| 192 | config := TestConfig(buildDir, nil, "", nil) |
| 193 | pathContext := PathContextForTesting(config) |
| 194 | bparams := convertBuildParams(BuildParams{ |
| 195 | // Test with Output |
| 196 | Output: PathForOutput(pathContext, "undeclared_symlink"), |
| 197 | SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 198 | }) |
| 199 | |
| 200 | err := validateBuildParams(bparams) |
| 201 | if err != nil { |
| 202 | t.Error(err) |
| 203 | } |
| 204 | |
| 205 | bparams = convertBuildParams(BuildParams{ |
| 206 | // Test with ImplicitOutput |
| 207 | ImplicitOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 208 | SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 209 | }) |
| 210 | |
| 211 | err = validateBuildParams(bparams) |
| 212 | if err != nil { |
| 213 | t.Error(err) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | func TestValidateIncorrectBuildParams(t *testing.T) { |
| 218 | config := TestConfig(buildDir, nil, "", nil) |
| 219 | pathContext := PathContextForTesting(config) |
| 220 | params := BuildParams{ |
| 221 | Output: PathForOutput(pathContext, "regular_output"), |
| 222 | Outputs: PathsForOutput(pathContext, []string{"out1", "out2"}), |
| 223 | ImplicitOutput: PathForOutput(pathContext, "implicit_output"), |
| 224 | ImplicitOutputs: PathsForOutput(pathContext, []string{"i_out1", "_out2"}), |
| 225 | SymlinkOutput: PathForOutput(pathContext, "undeclared_symlink"), |
| 226 | } |
| 227 | |
| 228 | bparams := convertBuildParams(params) |
| 229 | err := validateBuildParams(bparams) |
| 230 | if err != nil { |
| 231 | FailIfNoMatchingErrors(t, "undeclared_symlink is not a declared output or implicit output", []error{err}) |
| 232 | } else { |
| 233 | t.Errorf("Expected build params to fail validation: %+v", bparams) |
| 234 | } |
| 235 | } |