| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 ( | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 18 | 	"path/filepath" | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 19 | 	"reflect" | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 20 | 	"testing" | 
 | 21 |  | 
 | 22 | 	"github.com/google/blueprint" | 
 | 23 | ) | 
 | 24 |  | 
 | 25 | func TestDependingOnModuleInSameNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 26 | 	result := GroupFixturePreparers( | 
 | 27 | 		prepareForTestWithNamespace, | 
 | 28 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 29 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 30 | 				soong_namespace { | 
 | 31 | 				} | 
 | 32 | 				test_module { | 
 | 33 | 					name: "a", | 
 | 34 | 				} | 
 | 35 | 				test_module { | 
 | 36 | 					name: "b", | 
 | 37 | 					deps: ["a"], | 
 | 38 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 39 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 40 | 		}), | 
 | 41 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 42 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 43 | 	a := getModule(result, "a") | 
 | 44 | 	b := getModule(result, "b") | 
 | 45 | 	if !dependsOn(result, b, a) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 46 | 		t.Errorf("module b does not depend on module a in the same namespace") | 
 | 47 | 	} | 
 | 48 | } | 
 | 49 |  | 
 | 50 | func TestDependingOnModuleInRootNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 51 | 	result := GroupFixturePreparers( | 
 | 52 | 		prepareForTestWithNamespace, | 
 | 53 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 54 | 			".": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 55 | 				test_module { | 
 | 56 | 					name: "b", | 
 | 57 | 					deps: ["a"], | 
 | 58 | 				} | 
 | 59 | 				test_module { | 
 | 60 | 					name: "a", | 
 | 61 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 62 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 63 | 		}), | 
 | 64 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 65 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 66 | 	a := getModule(result, "a") | 
 | 67 | 	b := getModule(result, "b") | 
 | 68 | 	if !dependsOn(result, b, a) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 69 | 		t.Errorf("module b in root namespace does not depend on module a in the root namespace") | 
 | 70 | 	} | 
 | 71 | } | 
 | 72 |  | 
 | 73 | func TestImplicitlyImportRootNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 74 | 	GroupFixturePreparers( | 
 | 75 | 		prepareForTestWithNamespace, | 
 | 76 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 77 | 			".": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 78 | 				test_module { | 
 | 79 | 					name: "a", | 
 | 80 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 81 | 			`, | 
 | 82 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 83 | 				soong_namespace { | 
 | 84 | 				} | 
 | 85 | 				test_module { | 
 | 86 | 					name: "b", | 
 | 87 | 					deps: ["a"], | 
 | 88 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 89 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 90 | 		}), | 
 | 91 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 92 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 93 | 	// RunTest will report any errors | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 94 | } | 
 | 95 |  | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 96 | func TestDependingOnBlueprintModuleInRootNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 97 | 	GroupFixturePreparers( | 
 | 98 | 		prepareForTestWithNamespace, | 
 | 99 | 		dirBpToPreparer(map[string]string{ | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 100 | 			".": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 101 | 				blueprint_test_module { | 
 | 102 | 					name: "a", | 
 | 103 | 				} | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 104 | 			`, | 
 | 105 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 106 | 				soong_namespace { | 
 | 107 | 				} | 
 | 108 | 				blueprint_test_module { | 
 | 109 | 					name: "b", | 
 | 110 | 					deps: ["a"], | 
 | 111 | 				} | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 112 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 113 | 		}), | 
 | 114 | 	).RunTest(t) | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 115 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 116 | 	// RunTest will report any errors | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 117 | } | 
 | 118 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 119 | func TestDependingOnModuleInImportedNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 120 | 	result := GroupFixturePreparers( | 
 | 121 | 		prepareForTestWithNamespace, | 
 | 122 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 123 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 124 | 				soong_namespace { | 
 | 125 | 				} | 
 | 126 | 				test_module { | 
 | 127 | 					name: "a", | 
 | 128 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 129 | 			`, | 
 | 130 | 			"dir2": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 131 | 				soong_namespace { | 
 | 132 | 					imports: ["dir1"], | 
 | 133 | 				} | 
 | 134 | 				test_module { | 
 | 135 | 					name: "b", | 
 | 136 | 					deps: ["a"], | 
 | 137 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 138 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 139 | 		}), | 
 | 140 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 141 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 142 | 	a := getModule(result, "a") | 
 | 143 | 	b := getModule(result, "b") | 
 | 144 | 	if !dependsOn(result, b, a) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 145 | 		t.Errorf("module b does not depend on module a in the same namespace") | 
 | 146 | 	} | 
 | 147 | } | 
 | 148 |  | 
 | 149 | func TestDependingOnModuleInNonImportedNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 150 | 	GroupFixturePreparers( | 
 | 151 | 		prepareForTestWithNamespace, | 
 | 152 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 153 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 154 | 				soong_namespace { | 
 | 155 | 				} | 
 | 156 | 				test_module { | 
 | 157 | 					name: "a", | 
 | 158 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 159 | 			`, | 
 | 160 | 			"dir2": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 161 | 				soong_namespace { | 
 | 162 | 				} | 
 | 163 | 				test_module { | 
 | 164 | 					name: "a", | 
 | 165 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 166 | 			`, | 
 | 167 | 			"dir3": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 168 | 				soong_namespace { | 
 | 169 | 				} | 
 | 170 | 				test_module { | 
 | 171 | 					name: "b", | 
 | 172 | 					deps: ["a"], | 
 | 173 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 174 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 175 | 		}), | 
 | 176 | 	). | 
| Steven Moreland | 671dc23 | 2023-03-31 20:48:42 +0000 | [diff] [blame] | 177 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir3/Android.bp:4:5: "b" depends on undefined module "a". | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 178 | Module "b" is defined in namespace "dir3" which can read these 2 namespaces: ["dir3" "."] | 
| Steven Moreland | 671dc23 | 2023-03-31 20:48:42 +0000 | [diff] [blame] | 179 | Module "a" can be found in these namespaces: ["dir1" "dir2"]\E | 
 | 180 | Or did you mean ["soong_namespace"]?`)). | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 181 | 		RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 182 | } | 
 | 183 |  | 
 | 184 | func TestDependingOnModuleByFullyQualifiedReference(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 185 | 	result := GroupFixturePreparers( | 
 | 186 | 		prepareForTestWithNamespace, | 
 | 187 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 188 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 189 | 				soong_namespace { | 
 | 190 | 				} | 
 | 191 | 				test_module { | 
 | 192 | 					name: "a", | 
 | 193 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 194 | 			`, | 
 | 195 | 			"dir2": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 196 | 				soong_namespace { | 
 | 197 | 				} | 
 | 198 | 				test_module { | 
 | 199 | 					name: "b", | 
 | 200 | 					deps: ["//dir1:a"], | 
 | 201 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 202 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 203 | 		}), | 
 | 204 | 	).RunTest(t) | 
 | 205 |  | 
 | 206 | 	a := getModule(result, "a") | 
 | 207 | 	b := getModule(result, "b") | 
 | 208 | 	if !dependsOn(result, b, a) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 209 | 		t.Errorf("module b does not depend on module a") | 
 | 210 | 	} | 
 | 211 | } | 
 | 212 |  | 
 | 213 | func TestSameNameInTwoNamespaces(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 214 | 	result := GroupFixturePreparers( | 
 | 215 | 		prepareForTestWithNamespace, | 
 | 216 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 217 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 218 | 				soong_namespace { | 
 | 219 | 				} | 
 | 220 | 				test_module { | 
 | 221 | 					name: "a", | 
 | 222 | 					id: "1", | 
 | 223 | 				} | 
 | 224 | 				test_module { | 
 | 225 | 					name: "b", | 
 | 226 | 					deps: ["a"], | 
 | 227 | 					id: "2", | 
 | 228 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 229 | 			`, | 
 | 230 | 			"dir2": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 231 | 				soong_namespace { | 
 | 232 | 				} | 
 | 233 | 				test_module { | 
 | 234 | 					name: "a", | 
 | 235 | 					id:"3", | 
 | 236 | 				} | 
 | 237 | 				test_module { | 
 | 238 | 					name: "b", | 
 | 239 | 					deps: ["a"], | 
 | 240 | 					id:"4", | 
 | 241 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 242 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 243 | 		}), | 
 | 244 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 245 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 246 | 	one := findModuleById(result, "1") | 
 | 247 | 	two := findModuleById(result, "2") | 
 | 248 | 	three := findModuleById(result, "3") | 
 | 249 | 	four := findModuleById(result, "4") | 
 | 250 | 	if !dependsOn(result, two, one) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 251 | 		t.Fatalf("Module 2 does not depend on module 1 in its namespace") | 
 | 252 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 253 | 	if dependsOn(result, two, three) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 254 | 		t.Fatalf("Module 2 depends on module 3 in another namespace") | 
 | 255 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 256 | 	if !dependsOn(result, four, three) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 257 | 		t.Fatalf("Module 4 does not depend on module 3 in its namespace") | 
 | 258 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 259 | 	if dependsOn(result, four, one) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 260 | 		t.Fatalf("Module 4 depends on module 1 in another namespace") | 
 | 261 | 	} | 
 | 262 | } | 
 | 263 |  | 
 | 264 | func TestSearchOrder(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 265 | 	result := GroupFixturePreparers( | 
 | 266 | 		prepareForTestWithNamespace, | 
 | 267 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 268 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 269 | 				soong_namespace { | 
 | 270 | 				} | 
 | 271 | 				test_module { | 
 | 272 | 					name: "a", | 
 | 273 | 					id: "1", | 
 | 274 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 275 | 			`, | 
 | 276 | 			"dir2": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 277 | 				soong_namespace { | 
 | 278 | 				} | 
 | 279 | 				test_module { | 
 | 280 | 					name: "a", | 
 | 281 | 					id:"2", | 
 | 282 | 				} | 
 | 283 | 				test_module { | 
 | 284 | 					name: "b", | 
 | 285 | 					id:"3", | 
 | 286 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 287 | 			`, | 
 | 288 | 			"dir3": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 289 | 				soong_namespace { | 
 | 290 | 				} | 
 | 291 | 				test_module { | 
 | 292 | 					name: "a", | 
 | 293 | 					id:"4", | 
 | 294 | 				} | 
 | 295 | 				test_module { | 
 | 296 | 					name: "b", | 
 | 297 | 					id:"5", | 
 | 298 | 				} | 
 | 299 | 				test_module { | 
 | 300 | 					name: "c", | 
 | 301 | 					id:"6", | 
 | 302 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 303 | 			`, | 
 | 304 | 			".": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 305 | 				test_module { | 
 | 306 | 					name: "a", | 
 | 307 | 					id: "7", | 
 | 308 | 				} | 
 | 309 | 				test_module { | 
 | 310 | 					name: "b", | 
 | 311 | 					id: "8", | 
 | 312 | 				} | 
 | 313 | 				test_module { | 
 | 314 | 					name: "c", | 
 | 315 | 					id: "9", | 
 | 316 | 				} | 
 | 317 | 				test_module { | 
 | 318 | 					name: "d", | 
 | 319 | 					id: "10", | 
 | 320 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 321 | 			`, | 
 | 322 | 			"dir4": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 323 | 				soong_namespace { | 
 | 324 | 					imports: ["dir1", "dir2", "dir3"] | 
 | 325 | 				} | 
 | 326 | 				test_module { | 
 | 327 | 					name: "test_me", | 
 | 328 | 					id:"0", | 
 | 329 | 					deps: ["a", "b", "c", "d"], | 
 | 330 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 331 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 332 | 		}), | 
 | 333 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 334 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 335 | 	testMe := findModuleById(result, "0") | 
 | 336 | 	if !dependsOn(result, testMe, findModuleById(result, "1")) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 337 | 		t.Errorf("test_me doesn't depend on id 1") | 
 | 338 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 339 | 	if !dependsOn(result, testMe, findModuleById(result, "3")) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 340 | 		t.Errorf("test_me doesn't depend on id 3") | 
 | 341 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 342 | 	if !dependsOn(result, testMe, findModuleById(result, "6")) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 343 | 		t.Errorf("test_me doesn't depend on id 6") | 
 | 344 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 345 | 	if !dependsOn(result, testMe, findModuleById(result, "10")) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 346 | 		t.Errorf("test_me doesn't depend on id 10") | 
 | 347 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 348 | 	if numDeps(result, testMe) != 4 { | 
 | 349 | 		t.Errorf("num dependencies of test_me = %v, not 4\n", numDeps(result, testMe)) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 350 | 	} | 
 | 351 | } | 
 | 352 |  | 
 | 353 | func TestTwoNamespacesCanImportEachOther(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 354 | 	GroupFixturePreparers( | 
 | 355 | 		prepareForTestWithNamespace, | 
 | 356 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 357 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 358 | 				soong_namespace { | 
 | 359 | 					imports: ["dir2"] | 
 | 360 | 				} | 
 | 361 | 				test_module { | 
 | 362 | 					name: "a", | 
 | 363 | 				} | 
 | 364 | 				test_module { | 
 | 365 | 					name: "c", | 
 | 366 | 					deps: ["b"], | 
 | 367 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 368 | 			`, | 
 | 369 | 			"dir2": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 370 | 				soong_namespace { | 
 | 371 | 					imports: ["dir1"], | 
 | 372 | 				} | 
 | 373 | 				test_module { | 
 | 374 | 					name: "b", | 
 | 375 | 					deps: ["a"], | 
 | 376 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 377 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 378 | 		}), | 
 | 379 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 380 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 381 | 	// RunTest will report any errors | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 382 | } | 
 | 383 |  | 
 | 384 | func TestImportingNonexistentNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 385 | 	GroupFixturePreparers( | 
 | 386 | 		prepareForTestWithNamespace, | 
 | 387 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 388 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 389 | 				soong_namespace { | 
 | 390 | 					imports: ["a_nonexistent_namespace"] | 
 | 391 | 				} | 
 | 392 | 				test_module { | 
 | 393 | 					name: "a", | 
 | 394 | 					deps: ["a_nonexistent_module"] | 
 | 395 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 396 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 397 | 		}), | 
 | 398 | 	). | 
 | 399 | 		// should complain about the missing namespace and not complain about the unresolvable dependency | 
 | 400 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:2:5: module "soong_namespace": namespace a_nonexistent_namespace does not exist\E`)). | 
 | 401 | 		RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 402 | } | 
 | 403 |  | 
 | 404 | func TestNamespacesDontInheritParentNamespaces(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 405 | 	GroupFixturePreparers( | 
 | 406 | 		prepareForTestWithNamespace, | 
 | 407 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 408 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 409 | 				soong_namespace { | 
 | 410 | 				} | 
 | 411 | 				test_module { | 
 | 412 | 					name: "a", | 
 | 413 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 414 | 			`, | 
 | 415 | 			"dir1/subdir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 416 | 				soong_namespace { | 
 | 417 | 				} | 
 | 418 | 				test_module { | 
 | 419 | 					name: "b", | 
 | 420 | 					deps: ["a"], | 
 | 421 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 422 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 423 | 		}), | 
 | 424 | 	). | 
| Steven Moreland | 671dc23 | 2023-03-31 20:48:42 +0000 | [diff] [blame] | 425 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/subdir1/Android.bp:4:5: "b" depends on undefined module "a". | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 426 | Module "b" is defined in namespace "dir1/subdir1" which can read these 2 namespaces: ["dir1/subdir1" "."] | 
| Steven Moreland | 671dc23 | 2023-03-31 20:48:42 +0000 | [diff] [blame] | 427 | Module "a" can be found in these namespaces: ["dir1"]\E | 
 | 428 | Or did you mean ["soong_namespace"]?`)). | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 429 | 		RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 430 | } | 
 | 431 |  | 
 | 432 | func TestModulesDoReceiveParentNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 433 | 	GroupFixturePreparers( | 
 | 434 | 		prepareForTestWithNamespace, | 
 | 435 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 436 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 437 | 				soong_namespace { | 
 | 438 | 				} | 
 | 439 | 				test_module { | 
 | 440 | 					name: "a", | 
 | 441 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 442 | 			`, | 
 | 443 | 			"dir1/subdir": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 444 | 				test_module { | 
 | 445 | 					name: "b", | 
 | 446 | 					deps: ["a"], | 
 | 447 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 448 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 449 | 		}), | 
 | 450 | 	).RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 451 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 452 | 	// RunTest will report any errors | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 453 | } | 
 | 454 |  | 
 | 455 | func TestNamespaceImportsNotTransitive(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 456 | 	GroupFixturePreparers( | 
 | 457 | 		prepareForTestWithNamespace, | 
 | 458 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 459 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 460 | 				soong_namespace { | 
 | 461 | 				} | 
 | 462 | 				test_module { | 
 | 463 | 					name: "a", | 
 | 464 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 465 | 			`, | 
 | 466 | 			"dir2": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 467 | 				soong_namespace { | 
 | 468 | 					imports: ["dir1"], | 
 | 469 | 				} | 
 | 470 | 				test_module { | 
 | 471 | 					name: "b", | 
 | 472 | 					deps: ["a"], | 
 | 473 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 474 | 			`, | 
 | 475 | 			"dir3": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 476 | 				soong_namespace { | 
 | 477 | 					imports: ["dir2"], | 
 | 478 | 				} | 
 | 479 | 				test_module { | 
 | 480 | 					name: "c", | 
 | 481 | 					deps: ["a"], | 
 | 482 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 483 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 484 | 		}), | 
 | 485 | 	). | 
| Steven Moreland | 671dc23 | 2023-03-31 20:48:42 +0000 | [diff] [blame] | 486 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir3/Android.bp:5:5: "c" depends on undefined module "a". | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 487 | Module "c" is defined in namespace "dir3" which can read these 3 namespaces: ["dir3" "dir2" "."] | 
| Steven Moreland | 671dc23 | 2023-03-31 20:48:42 +0000 | [diff] [blame] | 488 | Module "a" can be found in these namespaces: ["dir1"]\E | 
 | 489 | Or did you mean ["b"]?`)). | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 490 | 		RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 491 | } | 
 | 492 |  | 
 | 493 | func TestTwoNamepacesInSameDir(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 494 | 	GroupFixturePreparers( | 
 | 495 | 		prepareForTestWithNamespace, | 
 | 496 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 497 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 498 | 				soong_namespace { | 
 | 499 | 				} | 
 | 500 | 				soong_namespace { | 
 | 501 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 502 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 503 | 		}), | 
 | 504 | 	). | 
 | 505 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:4:5: namespace dir1 already exists\E`)). | 
 | 506 | 		RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 507 | } | 
 | 508 |  | 
 | 509 | func TestNamespaceNotAtTopOfFile(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 510 | 	GroupFixturePreparers( | 
 | 511 | 		prepareForTestWithNamespace, | 
 | 512 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 513 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 514 | 				test_module { | 
 | 515 | 					name: "a" | 
 | 516 | 				} | 
 | 517 | 				soong_namespace { | 
 | 518 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 519 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 520 | 		}), | 
 | 521 | 	). | 
 | 522 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:5:5: a namespace must be the first module in the file\E`)). | 
 | 523 | 		RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 524 | } | 
 | 525 |  | 
 | 526 | func TestTwoModulesWithSameNameInSameNamespace(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 527 | 	GroupFixturePreparers( | 
 | 528 | 		prepareForTestWithNamespace, | 
 | 529 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 530 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 531 | 				soong_namespace { | 
 | 532 | 				} | 
 | 533 | 				test_module { | 
 | 534 | 					name: "a" | 
 | 535 | 				} | 
 | 536 | 				test_module { | 
 | 537 | 					name: "a" | 
 | 538 | 				} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 539 | 			`, | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 540 | 		}), | 
 | 541 | 	). | 
 | 542 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:7:5: module "a" already defined | 
 | 543 |        dir1/Android.bp:4:5 <-- previous definition here\E`)). | 
 | 544 | 		RunTest(t) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 545 | } | 
 | 546 |  | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 547 | func TestDeclaringNamespaceInNonAndroidBpFile(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 548 | 	GroupFixturePreparers( | 
 | 549 | 		prepareForTestWithNamespace, | 
 | 550 | 		FixtureWithRootAndroidBp(` | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 551 | 				build = ["include.bp"] | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 552 | 		`), | 
 | 553 | 		FixtureAddTextFile("include.bp", ` | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 554 | 				soong_namespace { | 
 | 555 | 				} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 556 | 		`), | 
 | 557 | 	). | 
 | 558 | 		ExtendWithErrorHandler(FixtureExpectsOneErrorPattern( | 
 | 559 | 			`\Qinclude.bp:2:5: A namespace may only be declared in a file named Android.bp\E`, | 
 | 560 | 		)). | 
 | 561 | 		RunTest(t) | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 562 | } | 
 | 563 |  | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 564 | // so that the generated .ninja file will have consistent names | 
 | 565 | func TestConsistentNamespaceNames(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 566 | 	result := GroupFixturePreparers( | 
 | 567 | 		prepareForTestWithNamespace, | 
 | 568 | 		dirBpToPreparer(map[string]string{ | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 569 | 			"dir1": "soong_namespace{}", | 
 | 570 | 			"dir2": "soong_namespace{}", | 
 | 571 | 			"dir3": "soong_namespace{}", | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 572 | 		}), | 
 | 573 | 	).RunTest(t) | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 574 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 575 | 	ns1, _ := result.NameResolver.namespaceAt("dir1") | 
 | 576 | 	ns2, _ := result.NameResolver.namespaceAt("dir2") | 
 | 577 | 	ns3, _ := result.NameResolver.namespaceAt("dir3") | 
| Jeff Gaston | b274ed3 | 2017-12-01 17:10:33 -0800 | [diff] [blame] | 578 | 	actualIds := []string{ns1.id, ns2.id, ns3.id} | 
 | 579 | 	expectedIds := []string{"1", "2", "3"} | 
 | 580 | 	if !reflect.DeepEqual(actualIds, expectedIds) { | 
 | 581 | 		t.Errorf("Incorrect namespace ids.\nactual: %s\nexpected: %s\n", actualIds, expectedIds) | 
 | 582 | 	} | 
 | 583 | } | 
 | 584 |  | 
| Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 585 | // so that the generated .ninja file will have consistent names | 
 | 586 | func TestRename(t *testing.T) { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 587 | 	GroupFixturePreparers( | 
 | 588 | 		prepareForTestWithNamespace, | 
 | 589 | 		dirBpToPreparer(map[string]string{ | 
| Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 590 | 			"dir1": ` | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 591 | 				soong_namespace { | 
 | 592 | 				} | 
 | 593 | 				test_module { | 
 | 594 | 					name: "a", | 
 | 595 | 					deps: ["c"], | 
 | 596 | 				} | 
 | 597 | 				test_module { | 
 | 598 | 					name: "b", | 
 | 599 | 					rename: "c", | 
 | 600 | 				} | 
 | 601 | 			`, | 
 | 602 | 		}), | 
 | 603 | 	).RunTest(t) | 
 | 604 |  | 
 | 605 | 	// RunTest will report any errors | 
| Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 606 | } | 
 | 607 |  | 
| Paul Duffin | 3f7bf9f | 2022-11-08 12:21:15 +0000 | [diff] [blame] | 608 | func TestNamespace_Exports(t *testing.T) { | 
 | 609 | 	result := GroupFixturePreparers( | 
 | 610 | 		prepareForTestWithNamespace, | 
 | 611 | 		FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
 | 612 | 			variables.NamespacesToExport = []string{"dir1"} | 
 | 613 | 		}), | 
 | 614 | 		dirBpToPreparer(map[string]string{ | 
 | 615 | 			"dir1": ` | 
 | 616 | 				soong_namespace { | 
 | 617 | 				} | 
 | 618 | 				test_module { | 
 | 619 | 					name: "a", | 
 | 620 | 				} | 
 | 621 | 			`, | 
 | 622 | 			"dir2": ` | 
 | 623 | 				soong_namespace { | 
 | 624 | 				} | 
 | 625 | 				test_module { | 
 | 626 | 					name: "b", | 
 | 627 | 				} | 
 | 628 | 			`, | 
 | 629 | 		}), | 
 | 630 | 	).RunTest(t) | 
 | 631 |  | 
 | 632 | 	aModule := result.Module("a", "") | 
 | 633 | 	AssertBoolEquals(t, "a exported", true, aModule.ExportedToMake()) | 
 | 634 | 	bModule := result.Module("b", "") | 
 | 635 | 	AssertBoolEquals(t, "b not exported", false, bModule.ExportedToMake()) | 
 | 636 | } | 
 | 637 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 638 | // some utils to support the tests | 
 | 639 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 640 | var prepareForTestWithNamespace = GroupFixturePreparers( | 
 | 641 | 	FixtureRegisterWithContext(registerNamespaceBuildComponents), | 
 | 642 | 	FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
 | 643 | 		ctx.PreArchMutators(RegisterNamespaceMutator) | 
 | 644 | 	}), | 
 | 645 | 	FixtureModifyContext(func(ctx *TestContext) { | 
 | 646 | 		ctx.RegisterModuleType("test_module", newTestModule) | 
 | 647 | 		ctx.Context.RegisterModuleType("blueprint_test_module", newBlueprintTestModule) | 
 | 648 | 		ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { | 
 | 649 | 			ctx.BottomUp("rename", renameMutator) | 
 | 650 | 		}) | 
 | 651 | 	}), | 
 | 652 | ) | 
 | 653 |  | 
 | 654 | // dirBpToPreparer takes a map from directory to the contents of the Android.bp file and produces a | 
 | 655 | // FixturePreparer. | 
 | 656 | func dirBpToPreparer(bps map[string]string) FixturePreparer { | 
 | 657 | 	files := make(MockFS, len(bps)) | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 658 | 	files["Android.bp"] = []byte("") | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 659 | 	for dir, text := range bps { | 
| Jeff Gaston | 5c3886d | 2017-11-30 16:46:47 -0800 | [diff] [blame] | 660 | 		files[filepath.Join(dir, "Android.bp")] = []byte(text) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 661 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 662 | 	return files.AddToFixture() | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 663 | } | 
 | 664 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 665 | func dependsOn(result *TestResult, module TestingModule, possibleDependency TestingModule) bool { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 666 | 	depends := false | 
 | 667 | 	visit := func(dependency blueprint.Module) { | 
 | 668 | 		if dependency == possibleDependency.module { | 
 | 669 | 			depends = true | 
 | 670 | 		} | 
 | 671 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 672 | 	result.VisitDirectDeps(module.module, visit) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 673 | 	return depends | 
 | 674 | } | 
 | 675 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 676 | func numDeps(result *TestResult, module TestingModule) int { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 677 | 	count := 0 | 
 | 678 | 	visit := func(dependency blueprint.Module) { | 
 | 679 | 		count++ | 
 | 680 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 681 | 	result.VisitDirectDeps(module.module, visit) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 682 | 	return count | 
 | 683 | } | 
 | 684 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 685 | func getModule(result *TestResult, moduleName string) TestingModule { | 
 | 686 | 	return result.ModuleForTests(moduleName, "") | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 687 | } | 
 | 688 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 689 | func findModuleById(result *TestResult, id string) (module TestingModule) { | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 690 | 	visit := func(candidate blueprint.Module) { | 
 | 691 | 		testModule, ok := candidate.(*testModule) | 
 | 692 | 		if ok { | 
 | 693 | 			if testModule.properties.Id == id { | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 694 | 				module = newTestingModule(result.config, testModule) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 695 | 			} | 
 | 696 | 		} | 
 | 697 | 	} | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 698 | 	result.VisitAllModules(visit) | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 699 | 	return module | 
 | 700 | } | 
 | 701 |  | 
 | 702 | type testModule struct { | 
 | 703 | 	ModuleBase | 
 | 704 | 	properties struct { | 
| Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 705 | 		Rename string | 
 | 706 | 		Deps   []string | 
 | 707 | 		Id     string | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 708 | 	} | 
 | 709 | } | 
 | 710 |  | 
 | 711 | func (m *testModule) DepsMutator(ctx BottomUpMutatorContext) { | 
| Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 712 | 	if m.properties.Rename != "" { | 
 | 713 | 		ctx.Rename(m.properties.Rename) | 
 | 714 | 	} | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 715 | 	for _, d := range m.properties.Deps { | 
 | 716 | 		ctx.AddDependency(ctx.Module(), nil, d) | 
 | 717 | 	} | 
 | 718 | } | 
 | 719 |  | 
 | 720 | func (m *testModule) GenerateAndroidBuildActions(ModuleContext) { | 
 | 721 | } | 
 | 722 |  | 
| Colin Cross | eafb10c | 2018-04-16 13:58:10 -0700 | [diff] [blame] | 723 | func renameMutator(ctx BottomUpMutatorContext) { | 
 | 724 | 	if m, ok := ctx.Module().(*testModule); ok { | 
 | 725 | 		if m.properties.Rename != "" { | 
 | 726 | 			ctx.Rename(m.properties.Rename) | 
 | 727 | 		} | 
 | 728 | 	} | 
 | 729 | } | 
 | 730 |  | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 731 | func newTestModule() Module { | 
 | 732 | 	m := &testModule{} | 
 | 733 | 	m.AddProperties(&m.properties) | 
 | 734 | 	InitAndroidModule(m) | 
 | 735 | 	return m | 
 | 736 | } | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 737 |  | 
 | 738 | type blueprintTestModule struct { | 
 | 739 | 	blueprint.SimpleName | 
 | 740 | 	properties struct { | 
 | 741 | 		Deps []string | 
 | 742 | 	} | 
 | 743 | } | 
 | 744 |  | 
| Paul Duffin | 0fc6d32 | 2021-07-06 22:36:33 +0100 | [diff] [blame] | 745 | func (b *blueprintTestModule) DynamicDependencies(_ blueprint.DynamicDependerModuleContext) []string { | 
| Colin Cross | cd84b4e | 2019-06-14 11:26:09 -0700 | [diff] [blame] | 746 | 	return b.properties.Deps | 
 | 747 | } | 
 | 748 |  | 
 | 749 | func (b *blueprintTestModule) GenerateBuildActions(blueprint.ModuleContext) { | 
 | 750 | } | 
 | 751 |  | 
 | 752 | func newBlueprintTestModule() (blueprint.Module, []interface{}) { | 
 | 753 | 	m := &blueprintTestModule{} | 
 | 754 | 	return m, []interface{}{&m.properties, &m.SimpleName.Properties} | 
 | 755 | } |