testing.go 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. package android
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "regexp"
  19. "sort"
  20. "strings"
  21. "sync"
  22. "testing"
  23. "github.com/google/blueprint"
  24. "github.com/google/blueprint/proptools"
  25. )
  26. func NewTestContext(config Config) *TestContext {
  27. namespaceExportFilter := func(namespace *Namespace) bool {
  28. return true
  29. }
  30. nameResolver := NewNameResolver(namespaceExportFilter)
  31. ctx := &TestContext{
  32. Context: &Context{blueprint.NewContext(), config},
  33. NameResolver: nameResolver,
  34. }
  35. ctx.SetNameInterface(nameResolver)
  36. ctx.postDeps = append(ctx.postDeps, registerPathDepsMutator)
  37. ctx.SetFs(ctx.config.fs)
  38. if ctx.config.mockBpList != "" {
  39. ctx.SetModuleListFile(ctx.config.mockBpList)
  40. }
  41. return ctx
  42. }
  43. var PrepareForTestWithArchMutator = GroupFixturePreparers(
  44. // Configure architecture targets in the fixture config.
  45. FixtureModifyConfig(modifyTestConfigToSupportArchMutator),
  46. // Add the arch mutator to the context.
  47. FixtureRegisterWithContext(func(ctx RegistrationContext) {
  48. ctx.PreDepsMutators(registerArchMutator)
  49. }),
  50. )
  51. var PrepareForTestWithDefaults = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  52. ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
  53. })
  54. var PrepareForTestWithComponentsMutator = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  55. ctx.PreArchMutators(RegisterComponentsMutator)
  56. })
  57. var PrepareForTestWithPrebuilts = FixtureRegisterWithContext(RegisterPrebuiltMutators)
  58. var PrepareForTestWithOverrides = FixtureRegisterWithContext(func(ctx RegistrationContext) {
  59. ctx.PostDepsMutators(RegisterOverridePostDepsMutators)
  60. })
  61. // Test fixture preparer that will register most java build components.
  62. //
  63. // Singletons and mutators should only be added here if they are needed for a majority of java
  64. // module types, otherwise they should be added under a separate preparer to allow them to be
  65. // selected only when needed to reduce test execution time.
  66. //
  67. // Module types do not have much of an overhead unless they are used so this should include as many
  68. // module types as possible. The exceptions are those module types that require mutators and/or
  69. // singletons in order to function in which case they should be kept together in a separate
  70. // preparer.
  71. //
  72. // The mutators in this group were chosen because they are needed by the vast majority of tests.
  73. var PrepareForTestWithAndroidBuildComponents = GroupFixturePreparers(
  74. // Sorted alphabetically as the actual order does not matter as tests automatically enforce the
  75. // correct order.
  76. PrepareForTestWithArchMutator,
  77. PrepareForTestWithComponentsMutator,
  78. PrepareForTestWithDefaults,
  79. PrepareForTestWithFilegroup,
  80. PrepareForTestWithOverrides,
  81. PrepareForTestWithPackageModule,
  82. PrepareForTestWithPrebuilts,
  83. PrepareForTestWithVisibility,
  84. )
  85. // Prepares an integration test with all build components from the android package.
  86. //
  87. // This should only be used by tests that want to run with as much of the build enabled as possible.
  88. var PrepareForIntegrationTestWithAndroid = GroupFixturePreparers(
  89. PrepareForTestWithAndroidBuildComponents,
  90. )
  91. // Prepares a test that may be missing dependencies by setting allow_missing_dependencies to
  92. // true.
  93. var PrepareForTestWithAllowMissingDependencies = GroupFixturePreparers(
  94. FixtureModifyProductVariables(func(variables FixtureProductVariables) {
  95. variables.Allow_missing_dependencies = proptools.BoolPtr(true)
  96. }),
  97. FixtureModifyContext(func(ctx *TestContext) {
  98. ctx.SetAllowMissingDependencies(true)
  99. }),
  100. )
  101. // Prepares a test that disallows non-existent paths.
  102. var PrepareForTestDisallowNonExistentPaths = FixtureModifyConfig(func(config Config) {
  103. config.TestAllowNonExistentPaths = false
  104. })
  105. func NewTestArchContext(config Config) *TestContext {
  106. ctx := NewTestContext(config)
  107. ctx.preDeps = append(ctx.preDeps, registerArchMutator)
  108. return ctx
  109. }
  110. type TestContext struct {
  111. *Context
  112. preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc
  113. bp2buildPreArch, bp2buildDeps, bp2buildMutators []RegisterMutatorFunc
  114. NameResolver *NameResolver
  115. // The list of pre-singletons and singletons registered for the test.
  116. preSingletons, singletons sortableComponents
  117. // The order in which the pre-singletons, mutators and singletons will be run in this test
  118. // context; for debugging.
  119. preSingletonOrder, mutatorOrder, singletonOrder []string
  120. }
  121. func (ctx *TestContext) PreArchMutators(f RegisterMutatorFunc) {
  122. ctx.preArch = append(ctx.preArch, f)
  123. }
  124. func (ctx *TestContext) HardCodedPreArchMutators(f RegisterMutatorFunc) {
  125. // Register mutator function as normal for testing.
  126. ctx.PreArchMutators(f)
  127. }
  128. func (ctx *TestContext) PreDepsMutators(f RegisterMutatorFunc) {
  129. ctx.preDeps = append(ctx.preDeps, f)
  130. }
  131. func (ctx *TestContext) PostDepsMutators(f RegisterMutatorFunc) {
  132. ctx.postDeps = append(ctx.postDeps, f)
  133. }
  134. func (ctx *TestContext) FinalDepsMutators(f RegisterMutatorFunc) {
  135. ctx.finalDeps = append(ctx.finalDeps, f)
  136. }
  137. func (ctx *TestContext) RegisterBp2BuildConfig(config Bp2BuildConfig) {
  138. ctx.config.bp2buildPackageConfig = config
  139. }
  140. // RegisterBp2BuildMutator registers a BazelTargetModule mutator for converting a module
  141. // type to the equivalent Bazel target.
  142. func (ctx *TestContext) RegisterBp2BuildMutator(moduleType string, m func(TopDownMutatorContext)) {
  143. f := func(ctx RegisterMutatorsContext) {
  144. ctx.TopDown(moduleType, m)
  145. }
  146. ctx.config.bp2buildModuleTypeConfig[moduleType] = true
  147. ctx.bp2buildMutators = append(ctx.bp2buildMutators, f)
  148. }
  149. // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules
  150. // into Bazel BUILD targets that should run prior to deps and conversion.
  151. func (ctx *TestContext) PreArchBp2BuildMutators(f RegisterMutatorFunc) {
  152. ctx.bp2buildPreArch = append(ctx.bp2buildPreArch, f)
  153. }
  154. // DepsBp2BuildMutators adds mutators to be register for converting Android Blueprint modules into
  155. // Bazel BUILD targets that should run prior to conversion to resolve dependencies.
  156. func (ctx *TestContext) DepsBp2BuildMutators(f RegisterMutatorFunc) {
  157. ctx.bp2buildDeps = append(ctx.bp2buildDeps, f)
  158. }
  159. // registeredComponentOrder defines the order in which a sortableComponent type is registered at
  160. // runtime and provides support for reordering the components registered for a test in the same
  161. // way.
  162. type registeredComponentOrder struct {
  163. // The name of the component type, used for error messages.
  164. componentType string
  165. // The names of the registered components in the order in which they were registered.
  166. namesInOrder []string
  167. // Maps from the component name to its position in the runtime ordering.
  168. namesToIndex map[string]int
  169. // A function that defines the order between two named components that can be used to sort a slice
  170. // of component names into the same order as they appear in namesInOrder.
  171. less func(string, string) bool
  172. }
  173. // registeredComponentOrderFromExistingOrder takes an existing slice of sortableComponents and
  174. // creates a registeredComponentOrder that contains a less function that can be used to sort a
  175. // subset of that list of names so it is in the same order as the original sortableComponents.
  176. func registeredComponentOrderFromExistingOrder(componentType string, existingOrder sortableComponents) registeredComponentOrder {
  177. // Only the names from the existing order are needed for this so create a list of component names
  178. // in the correct order.
  179. namesInOrder := componentsToNames(existingOrder)
  180. // Populate the map from name to position in the list.
  181. nameToIndex := make(map[string]int)
  182. for i, n := range namesInOrder {
  183. nameToIndex[n] = i
  184. }
  185. // A function to use to map from a name to an index in the original order.
  186. indexOf := func(name string) int {
  187. index, ok := nameToIndex[name]
  188. if !ok {
  189. // Should never happen as tests that use components that are not known at runtime do not sort
  190. // so should never use this function.
  191. panic(fmt.Errorf("internal error: unknown %s %q should be one of %s", componentType, name, strings.Join(namesInOrder, ", ")))
  192. }
  193. return index
  194. }
  195. // The less function.
  196. less := func(n1, n2 string) bool {
  197. i1 := indexOf(n1)
  198. i2 := indexOf(n2)
  199. return i1 < i2
  200. }
  201. return registeredComponentOrder{
  202. componentType: componentType,
  203. namesInOrder: namesInOrder,
  204. namesToIndex: nameToIndex,
  205. less: less,
  206. }
  207. }
  208. // componentsToNames maps from the slice of components to a slice of their names.
  209. func componentsToNames(components sortableComponents) []string {
  210. names := make([]string, len(components))
  211. for i, c := range components {
  212. names[i] = c.componentName()
  213. }
  214. return names
  215. }
  216. // enforceOrdering enforces the supplied components are in the same order as is defined in this
  217. // object.
  218. //
  219. // If the supplied components contains any components that are not registered at runtime, i.e. test
  220. // specific components, then it is impossible to sort them into an order that both matches the
  221. // runtime and also preserves the implicit ordering defined in the test. In that case it will not
  222. // sort the components, instead it will just check that the components are in the correct order.
  223. //
  224. // Otherwise, this will sort the supplied components in place.
  225. func (o *registeredComponentOrder) enforceOrdering(components sortableComponents) {
  226. // Check to see if the list of components contains any components that are
  227. // not registered at runtime.
  228. var unknownComponents []string
  229. testOrder := componentsToNames(components)
  230. for _, name := range testOrder {
  231. if _, ok := o.namesToIndex[name]; !ok {
  232. unknownComponents = append(unknownComponents, name)
  233. break
  234. }
  235. }
  236. // If the slice contains some unknown components then it is not possible to
  237. // sort them into an order that matches the runtime while also preserving the
  238. // order expected from the test, so in that case don't sort just check that
  239. // the order of the known mutators does match.
  240. if len(unknownComponents) > 0 {
  241. // Check order.
  242. o.checkTestOrder(testOrder, unknownComponents)
  243. } else {
  244. // Sort the components.
  245. sort.Slice(components, func(i, j int) bool {
  246. n1 := components[i].componentName()
  247. n2 := components[j].componentName()
  248. return o.less(n1, n2)
  249. })
  250. }
  251. }
  252. // checkTestOrder checks that the supplied testOrder matches the one defined by this object,
  253. // panicking if it does not.
  254. func (o *registeredComponentOrder) checkTestOrder(testOrder []string, unknownComponents []string) {
  255. lastMatchingTest := -1
  256. matchCount := 0
  257. // Take a copy of the runtime order as it is modified during the comparison.
  258. runtimeOrder := append([]string(nil), o.namesInOrder...)
  259. componentType := o.componentType
  260. for i, j := 0, 0; i < len(testOrder) && j < len(runtimeOrder); {
  261. test := testOrder[i]
  262. runtime := runtimeOrder[j]
  263. if test == runtime {
  264. testOrder[i] = test + fmt.Sprintf(" <-- matched with runtime %s %d", componentType, j)
  265. runtimeOrder[j] = runtime + fmt.Sprintf(" <-- matched with test %s %d", componentType, i)
  266. lastMatchingTest = i
  267. i += 1
  268. j += 1
  269. matchCount += 1
  270. } else if _, ok := o.namesToIndex[test]; !ok {
  271. // The test component is not registered globally so assume it is the correct place, treat it
  272. // as having matched and skip it.
  273. i += 1
  274. matchCount += 1
  275. } else {
  276. // Assume that the test list is in the same order as the runtime list but the runtime list
  277. // contains some components that are not present in the tests. So, skip the runtime component
  278. // to try and find the next one that matches the current test component.
  279. j += 1
  280. }
  281. }
  282. // If every item in the test order was either test specific or matched one in the runtime then
  283. // it is in the correct order. Otherwise, it was not so fail.
  284. if matchCount != len(testOrder) {
  285. // The test component names were not all matched with a runtime component name so there must
  286. // either be a component present in the test that is not present in the runtime or they must be
  287. // in the wrong order.
  288. testOrder[lastMatchingTest+1] = testOrder[lastMatchingTest+1] + " <--- unmatched"
  289. panic(fmt.Errorf("the tests uses test specific components %q and so cannot be automatically sorted."+
  290. " Unfortunately it uses %s components in the wrong order.\n"+
  291. "test order:\n %s\n"+
  292. "runtime order\n %s\n",
  293. SortedUniqueStrings(unknownComponents),
  294. componentType,
  295. strings.Join(testOrder, "\n "),
  296. strings.Join(runtimeOrder, "\n ")))
  297. }
  298. }
  299. // registrationSorter encapsulates the information needed to ensure that the test mutators are
  300. // registered, and thereby executed, in the same order as they are at runtime.
  301. //
  302. // It MUST be populated lazily AFTER all package initialization has been done otherwise it will
  303. // only define the order for a subset of all the registered build components that are available for
  304. // the packages being tested.
  305. //
  306. // e.g if this is initialized during say the cc package initialization then any tests run in the
  307. // java package will not sort build components registered by the java package's init() functions.
  308. type registrationSorter struct {
  309. // Used to ensure that this is only created once.
  310. once sync.Once
  311. // The order of pre-singletons
  312. preSingletonOrder registeredComponentOrder
  313. // The order of mutators
  314. mutatorOrder registeredComponentOrder
  315. // The order of singletons
  316. singletonOrder registeredComponentOrder
  317. }
  318. // populate initializes this structure from globally registered build components.
  319. //
  320. // Only the first call has any effect.
  321. func (s *registrationSorter) populate() {
  322. s.once.Do(func() {
  323. // Create an ordering from the globally registered pre-singletons.
  324. s.preSingletonOrder = registeredComponentOrderFromExistingOrder("pre-singleton", preSingletons)
  325. // Created an ordering from the globally registered mutators.
  326. globallyRegisteredMutators := collateGloballyRegisteredMutators()
  327. s.mutatorOrder = registeredComponentOrderFromExistingOrder("mutator", globallyRegisteredMutators)
  328. // Create an ordering from the globally registered singletons.
  329. globallyRegisteredSingletons := collateGloballyRegisteredSingletons()
  330. s.singletonOrder = registeredComponentOrderFromExistingOrder("singleton", globallyRegisteredSingletons)
  331. })
  332. }
  333. // Provides support for enforcing the same order in which build components are registered globally
  334. // to the order in which they are registered during tests.
  335. //
  336. // MUST only be accessed via the globallyRegisteredComponentsOrder func.
  337. var globalRegistrationSorter registrationSorter
  338. // globallyRegisteredComponentsOrder returns the globalRegistrationSorter after ensuring it is
  339. // correctly populated.
  340. func globallyRegisteredComponentsOrder() *registrationSorter {
  341. globalRegistrationSorter.populate()
  342. return &globalRegistrationSorter
  343. }
  344. func (ctx *TestContext) Register() {
  345. globalOrder := globallyRegisteredComponentsOrder()
  346. // Ensure that the pre-singletons used in the test are in the same order as they are used at
  347. // runtime.
  348. globalOrder.preSingletonOrder.enforceOrdering(ctx.preSingletons)
  349. ctx.preSingletons.registerAll(ctx.Context)
  350. mutators := collateRegisteredMutators(ctx.preArch, ctx.preDeps, ctx.postDeps, ctx.finalDeps)
  351. // Ensure that the mutators used in the test are in the same order as they are used at runtime.
  352. globalOrder.mutatorOrder.enforceOrdering(mutators)
  353. mutators.registerAll(ctx.Context)
  354. // Ensure that the singletons used in the test are in the same order as they are used at runtime.
  355. globalOrder.singletonOrder.enforceOrdering(ctx.singletons)
  356. ctx.singletons.registerAll(ctx.Context)
  357. // Save the sorted components order away to make them easy to access while debugging.
  358. ctx.preSingletonOrder = componentsToNames(preSingletons)
  359. ctx.mutatorOrder = componentsToNames(mutators)
  360. ctx.singletonOrder = componentsToNames(singletons)
  361. }
  362. // RegisterForBazelConversion prepares a test context for bp2build conversion.
  363. func (ctx *TestContext) RegisterForBazelConversion() {
  364. RegisterMutatorsForBazelConversion(ctx.Context, ctx.bp2buildPreArch, ctx.bp2buildDeps, ctx.bp2buildMutators)
  365. }
  366. func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {
  367. // This function adapts the old style ParseFileList calls that are spread throughout the tests
  368. // to the new style that takes a config.
  369. return ctx.Context.ParseFileList(rootDir, filePaths, ctx.config)
  370. }
  371. func (ctx *TestContext) ParseBlueprintsFiles(rootDir string) (deps []string, errs []error) {
  372. // This function adapts the old style ParseBlueprintsFiles calls that are spread throughout the
  373. // tests to the new style that takes a config.
  374. return ctx.Context.ParseBlueprintsFiles(rootDir, ctx.config)
  375. }
  376. func (ctx *TestContext) RegisterModuleType(name string, factory ModuleFactory) {
  377. ctx.Context.RegisterModuleType(name, ModuleFactoryAdaptor(factory))
  378. }
  379. func (ctx *TestContext) RegisterSingletonModuleType(name string, factory SingletonModuleFactory) {
  380. s, m := SingletonModuleFactoryAdaptor(name, factory)
  381. ctx.RegisterSingletonType(name, s)
  382. ctx.RegisterModuleType(name, m)
  383. }
  384. func (ctx *TestContext) RegisterSingletonType(name string, factory SingletonFactory) {
  385. ctx.singletons = append(ctx.singletons, newSingleton(name, factory))
  386. }
  387. func (ctx *TestContext) RegisterPreSingletonType(name string, factory SingletonFactory) {
  388. ctx.preSingletons = append(ctx.preSingletons, newPreSingleton(name, factory))
  389. }
  390. func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
  391. var module Module
  392. ctx.VisitAllModules(func(m blueprint.Module) {
  393. if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
  394. module = m.(Module)
  395. }
  396. })
  397. if module == nil {
  398. // find all the modules that do exist
  399. var allModuleNames []string
  400. var allVariants []string
  401. ctx.VisitAllModules(func(m blueprint.Module) {
  402. allModuleNames = append(allModuleNames, ctx.ModuleName(m))
  403. if ctx.ModuleName(m) == name {
  404. allVariants = append(allVariants, ctx.ModuleSubDir(m))
  405. }
  406. })
  407. sort.Strings(allModuleNames)
  408. sort.Strings(allVariants)
  409. if len(allVariants) == 0 {
  410. panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
  411. name, strings.Join(allModuleNames, "\n ")))
  412. } else {
  413. panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s",
  414. name, variant, strings.Join(allVariants, "\n ")))
  415. }
  416. }
  417. return newTestingModule(ctx.config, module)
  418. }
  419. func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
  420. var variants []string
  421. ctx.VisitAllModules(func(m blueprint.Module) {
  422. if ctx.ModuleName(m) == name {
  423. variants = append(variants, ctx.ModuleSubDir(m))
  424. }
  425. })
  426. return variants
  427. }
  428. // SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
  429. func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
  430. allSingletonNames := []string{}
  431. for _, s := range ctx.Singletons() {
  432. n := ctx.SingletonName(s)
  433. if n == name {
  434. return TestingSingleton{
  435. baseTestingComponent: newBaseTestingComponent(ctx.config, s.(testBuildProvider)),
  436. singleton: s.(*singletonAdaptor).Singleton,
  437. }
  438. }
  439. allSingletonNames = append(allSingletonNames, n)
  440. }
  441. panic(fmt.Errorf("failed to find singleton %q."+
  442. "\nall singletons: %v", name, allSingletonNames))
  443. }
  444. func (ctx *TestContext) Config() Config {
  445. return ctx.config
  446. }
  447. type testBuildProvider interface {
  448. BuildParamsForTests() []BuildParams
  449. RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
  450. }
  451. type TestingBuildParams struct {
  452. BuildParams
  453. RuleParams blueprint.RuleParams
  454. config Config
  455. }
  456. // RelativeToTop creates a new instance of this which has had any usages of the current test's
  457. // temporary and test specific build directory replaced with a path relative to the notional top.
  458. //
  459. // The parts of this structure which are changed are:
  460. // * BuildParams
  461. // * Args
  462. // * All Path, Paths, WritablePath and WritablePaths fields.
  463. //
  464. // * RuleParams
  465. // * Command
  466. // * Depfile
  467. // * Rspfile
  468. // * RspfileContent
  469. // * SymlinkOutputs
  470. // * CommandDeps
  471. // * CommandOrderOnly
  472. //
  473. // See PathRelativeToTop for more details.
  474. func (p TestingBuildParams) RelativeToTop() TestingBuildParams {
  475. // If this is not a valid params then just return it back. That will make it easy to use with the
  476. // Maybe...() methods.
  477. if p.Rule == nil {
  478. return p
  479. }
  480. if p.config.config == nil {
  481. panic("cannot call RelativeToTop() on a TestingBuildParams previously returned by RelativeToTop()")
  482. }
  483. // Take a copy of the build params and replace any args that contains test specific temporary
  484. // paths with paths relative to the top.
  485. bparams := p.BuildParams
  486. bparams.Depfile = normalizeWritablePathRelativeToTop(bparams.Depfile)
  487. bparams.Output = normalizeWritablePathRelativeToTop(bparams.Output)
  488. bparams.Outputs = bparams.Outputs.RelativeToTop()
  489. bparams.SymlinkOutput = normalizeWritablePathRelativeToTop(bparams.SymlinkOutput)
  490. bparams.SymlinkOutputs = bparams.SymlinkOutputs.RelativeToTop()
  491. bparams.ImplicitOutput = normalizeWritablePathRelativeToTop(bparams.ImplicitOutput)
  492. bparams.ImplicitOutputs = bparams.ImplicitOutputs.RelativeToTop()
  493. bparams.Input = normalizePathRelativeToTop(bparams.Input)
  494. bparams.Inputs = bparams.Inputs.RelativeToTop()
  495. bparams.Implicit = normalizePathRelativeToTop(bparams.Implicit)
  496. bparams.Implicits = bparams.Implicits.RelativeToTop()
  497. bparams.OrderOnly = bparams.OrderOnly.RelativeToTop()
  498. bparams.Validation = normalizePathRelativeToTop(bparams.Validation)
  499. bparams.Validations = bparams.Validations.RelativeToTop()
  500. bparams.Args = normalizeStringMapRelativeToTop(p.config, bparams.Args)
  501. // Ditto for any fields in the RuleParams.
  502. rparams := p.RuleParams
  503. rparams.Command = normalizeStringRelativeToTop(p.config, rparams.Command)
  504. rparams.Depfile = normalizeStringRelativeToTop(p.config, rparams.Depfile)
  505. rparams.Rspfile = normalizeStringRelativeToTop(p.config, rparams.Rspfile)
  506. rparams.RspfileContent = normalizeStringRelativeToTop(p.config, rparams.RspfileContent)
  507. rparams.SymlinkOutputs = normalizeStringArrayRelativeToTop(p.config, rparams.SymlinkOutputs)
  508. rparams.CommandDeps = normalizeStringArrayRelativeToTop(p.config, rparams.CommandDeps)
  509. rparams.CommandOrderOnly = normalizeStringArrayRelativeToTop(p.config, rparams.CommandOrderOnly)
  510. return TestingBuildParams{
  511. BuildParams: bparams,
  512. RuleParams: rparams,
  513. }
  514. }
  515. func normalizeWritablePathRelativeToTop(path WritablePath) WritablePath {
  516. if path == nil {
  517. return nil
  518. }
  519. return path.RelativeToTop().(WritablePath)
  520. }
  521. func normalizePathRelativeToTop(path Path) Path {
  522. if path == nil {
  523. return nil
  524. }
  525. return path.RelativeToTop()
  526. }
  527. // baseTestingComponent provides functionality common to both TestingModule and TestingSingleton.
  528. type baseTestingComponent struct {
  529. config Config
  530. provider testBuildProvider
  531. }
  532. func newBaseTestingComponent(config Config, provider testBuildProvider) baseTestingComponent {
  533. return baseTestingComponent{config, provider}
  534. }
  535. // A function that will normalize a string containing paths, e.g. ninja command, by replacing
  536. // any references to the test specific temporary build directory that changes with each run to a
  537. // fixed path relative to a notional top directory.
  538. //
  539. // This is similar to StringPathRelativeToTop except that assumes the string is a single path
  540. // containing at most one instance of the temporary build directory at the start of the path while
  541. // this assumes that there can be any number at any position.
  542. func normalizeStringRelativeToTop(config Config, s string) string {
  543. // The buildDir usually looks something like: /tmp/testFoo2345/001
  544. //
  545. // Replace any usage of the buildDir with out/soong, e.g. replace "/tmp/testFoo2345/001" with
  546. // "out/soong".
  547. outSoongDir := filepath.Clean(config.buildDir)
  548. re := regexp.MustCompile(`\Q` + outSoongDir + `\E\b`)
  549. s = re.ReplaceAllString(s, "out/soong")
  550. // Replace any usage of the buildDir/.. with out, e.g. replace "/tmp/testFoo2345" with
  551. // "out". This must come after the previous replacement otherwise this would replace
  552. // "/tmp/testFoo2345/001" with "out/001" instead of "out/soong".
  553. outDir := filepath.Dir(outSoongDir)
  554. re = regexp.MustCompile(`\Q` + outDir + `\E\b`)
  555. s = re.ReplaceAllString(s, "out")
  556. return s
  557. }
  558. // normalizeStringArrayRelativeToTop creates a new slice constructed by applying
  559. // normalizeStringRelativeToTop to each item in the slice.
  560. func normalizeStringArrayRelativeToTop(config Config, slice []string) []string {
  561. newSlice := make([]string, len(slice))
  562. for i, s := range slice {
  563. newSlice[i] = normalizeStringRelativeToTop(config, s)
  564. }
  565. return newSlice
  566. }
  567. // normalizeStringMapRelativeToTop creates a new map constructed by applying
  568. // normalizeStringRelativeToTop to each value in the map.
  569. func normalizeStringMapRelativeToTop(config Config, m map[string]string) map[string]string {
  570. newMap := map[string]string{}
  571. for k, v := range m {
  572. newMap[k] = normalizeStringRelativeToTop(config, v)
  573. }
  574. return newMap
  575. }
  576. func (b baseTestingComponent) newTestingBuildParams(bparams BuildParams) TestingBuildParams {
  577. return TestingBuildParams{
  578. config: b.config,
  579. BuildParams: bparams,
  580. RuleParams: b.provider.RuleParamsForTests()[bparams.Rule],
  581. }
  582. }
  583. func (b baseTestingComponent) maybeBuildParamsFromRule(rule string) (TestingBuildParams, []string) {
  584. var searchedRules []string
  585. for _, p := range b.provider.BuildParamsForTests() {
  586. searchedRules = append(searchedRules, p.Rule.String())
  587. if strings.Contains(p.Rule.String(), rule) {
  588. return b.newTestingBuildParams(p), searchedRules
  589. }
  590. }
  591. return TestingBuildParams{}, searchedRules
  592. }
  593. func (b baseTestingComponent) buildParamsFromRule(rule string) TestingBuildParams {
  594. p, searchRules := b.maybeBuildParamsFromRule(rule)
  595. if p.Rule == nil {
  596. panic(fmt.Errorf("couldn't find rule %q.\nall rules: %v", rule, searchRules))
  597. }
  598. return p
  599. }
  600. func (b baseTestingComponent) maybeBuildParamsFromDescription(desc string) TestingBuildParams {
  601. for _, p := range b.provider.BuildParamsForTests() {
  602. if strings.Contains(p.Description, desc) {
  603. return b.newTestingBuildParams(p)
  604. }
  605. }
  606. return TestingBuildParams{}
  607. }
  608. func (b baseTestingComponent) buildParamsFromDescription(desc string) TestingBuildParams {
  609. p := b.maybeBuildParamsFromDescription(desc)
  610. if p.Rule == nil {
  611. panic(fmt.Errorf("couldn't find description %q", desc))
  612. }
  613. return p
  614. }
  615. func (b baseTestingComponent) maybeBuildParamsFromOutput(file string) (TestingBuildParams, []string) {
  616. var searchedOutputs []string
  617. for _, p := range b.provider.BuildParamsForTests() {
  618. outputs := append(WritablePaths(nil), p.Outputs...)
  619. outputs = append(outputs, p.ImplicitOutputs...)
  620. if p.Output != nil {
  621. outputs = append(outputs, p.Output)
  622. }
  623. for _, f := range outputs {
  624. if f.String() == file || f.Rel() == file || PathRelativeToTop(f) == file {
  625. return b.newTestingBuildParams(p), nil
  626. }
  627. searchedOutputs = append(searchedOutputs, f.Rel())
  628. }
  629. }
  630. return TestingBuildParams{}, searchedOutputs
  631. }
  632. func (b baseTestingComponent) buildParamsFromOutput(file string) TestingBuildParams {
  633. p, searchedOutputs := b.maybeBuildParamsFromOutput(file)
  634. if p.Rule == nil {
  635. panic(fmt.Errorf("couldn't find output %q.\nall outputs:\n %s\n",
  636. file, strings.Join(searchedOutputs, "\n ")))
  637. }
  638. return p
  639. }
  640. func (b baseTestingComponent) allOutputs() []string {
  641. var outputFullPaths []string
  642. for _, p := range b.provider.BuildParamsForTests() {
  643. outputs := append(WritablePaths(nil), p.Outputs...)
  644. outputs = append(outputs, p.ImplicitOutputs...)
  645. if p.Output != nil {
  646. outputs = append(outputs, p.Output)
  647. }
  648. outputFullPaths = append(outputFullPaths, outputs.Strings()...)
  649. }
  650. return outputFullPaths
  651. }
  652. // MaybeRule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Returns an empty
  653. // BuildParams if no rule is found.
  654. func (b baseTestingComponent) MaybeRule(rule string) TestingBuildParams {
  655. r, _ := b.maybeBuildParamsFromRule(rule)
  656. return r
  657. }
  658. // Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
  659. func (b baseTestingComponent) Rule(rule string) TestingBuildParams {
  660. return b.buildParamsFromRule(rule)
  661. }
  662. // MaybeDescription finds a call to ctx.Build with BuildParams.Description set to a the given string. Returns an empty
  663. // BuildParams if no rule is found.
  664. func (b baseTestingComponent) MaybeDescription(desc string) TestingBuildParams {
  665. return b.maybeBuildParamsFromDescription(desc)
  666. }
  667. // Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
  668. // found.
  669. func (b baseTestingComponent) Description(desc string) TestingBuildParams {
  670. return b.buildParamsFromDescription(desc)
  671. }
  672. // MaybeOutput finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
  673. // value matches the provided string. Returns an empty BuildParams if no rule is found.
  674. func (b baseTestingComponent) MaybeOutput(file string) TestingBuildParams {
  675. p, _ := b.maybeBuildParamsFromOutput(file)
  676. return p
  677. }
  678. // Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
  679. // value matches the provided string. Panics if no rule is found.
  680. func (b baseTestingComponent) Output(file string) TestingBuildParams {
  681. return b.buildParamsFromOutput(file)
  682. }
  683. // AllOutputs returns all 'BuildParams.Output's and 'BuildParams.Outputs's in their full path string forms.
  684. func (b baseTestingComponent) AllOutputs() []string {
  685. return b.allOutputs()
  686. }
  687. // TestingModule is wrapper around an android.Module that provides methods to find information about individual
  688. // ctx.Build parameters for verification in tests.
  689. type TestingModule struct {
  690. baseTestingComponent
  691. module Module
  692. }
  693. func newTestingModule(config Config, module Module) TestingModule {
  694. return TestingModule{
  695. newBaseTestingComponent(config, module),
  696. module,
  697. }
  698. }
  699. // Module returns the Module wrapped by the TestingModule.
  700. func (m TestingModule) Module() Module {
  701. return m.module
  702. }
  703. // VariablesForTestsRelativeToTop returns a copy of the Module.VariablesForTests() with every value
  704. // having any temporary build dir usages replaced with paths relative to a notional top.
  705. func (m TestingModule) VariablesForTestsRelativeToTop() map[string]string {
  706. return normalizeStringMapRelativeToTop(m.config, m.module.VariablesForTests())
  707. }
  708. // TestingSingleton is wrapper around an android.Singleton that provides methods to find information about individual
  709. // ctx.Build parameters for verification in tests.
  710. type TestingSingleton struct {
  711. baseTestingComponent
  712. singleton Singleton
  713. }
  714. // Singleton returns the Singleton wrapped by the TestingSingleton.
  715. func (s TestingSingleton) Singleton() Singleton {
  716. return s.singleton
  717. }
  718. func FailIfErrored(t *testing.T, errs []error) {
  719. t.Helper()
  720. if len(errs) > 0 {
  721. for _, err := range errs {
  722. t.Error(err)
  723. }
  724. t.FailNow()
  725. }
  726. }
  727. // Fail if no errors that matched the regular expression were found.
  728. //
  729. // Returns true if a matching error was found, false otherwise.
  730. func FailIfNoMatchingErrors(t *testing.T, pattern string, errs []error) bool {
  731. t.Helper()
  732. matcher, err := regexp.Compile(pattern)
  733. if err != nil {
  734. t.Fatalf("failed to compile regular expression %q because %s", pattern, err)
  735. }
  736. found := false
  737. for _, err := range errs {
  738. if matcher.FindStringIndex(err.Error()) != nil {
  739. found = true
  740. break
  741. }
  742. }
  743. if !found {
  744. t.Errorf("missing the expected error %q (checked %d error(s))", pattern, len(errs))
  745. for i, err := range errs {
  746. t.Errorf("errs[%d] = %q", i, err)
  747. }
  748. }
  749. return found
  750. }
  751. func CheckErrorsAgainstExpectations(t *testing.T, errs []error, expectedErrorPatterns []string) {
  752. t.Helper()
  753. if expectedErrorPatterns == nil {
  754. FailIfErrored(t, errs)
  755. } else {
  756. for _, expectedError := range expectedErrorPatterns {
  757. FailIfNoMatchingErrors(t, expectedError, errs)
  758. }
  759. if len(errs) > len(expectedErrorPatterns) {
  760. t.Errorf("additional errors found, expected %d, found %d",
  761. len(expectedErrorPatterns), len(errs))
  762. for i, expectedError := range expectedErrorPatterns {
  763. t.Errorf("expectedErrors[%d] = %s", i, expectedError)
  764. }
  765. for i, err := range errs {
  766. t.Errorf("errs[%d] = %s", i, err)
  767. }
  768. t.FailNow()
  769. }
  770. }
  771. }
  772. func SetKatiEnabledForTests(config Config) {
  773. config.katiEnabled = true
  774. }
  775. func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries {
  776. var p AndroidMkEntriesProvider
  777. var ok bool
  778. if p, ok = mod.(AndroidMkEntriesProvider); !ok {
  779. t.Errorf("module does not implement AndroidMkEntriesProvider: " + mod.Name())
  780. }
  781. entriesList := p.AndroidMkEntries()
  782. for i, _ := range entriesList {
  783. entriesList[i].fillInEntries(ctx, mod)
  784. }
  785. return entriesList
  786. }
  787. func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData {
  788. var p AndroidMkDataProvider
  789. var ok bool
  790. if p, ok = mod.(AndroidMkDataProvider); !ok {
  791. t.Errorf("module does not implement AndroidMkDataProvider: " + mod.Name())
  792. }
  793. data := p.AndroidMk()
  794. data.fillInData(ctx, mod)
  795. return data
  796. }
  797. // Normalize the path for testing.
  798. //
  799. // If the path is relative to the build directory then return the relative path
  800. // to avoid tests having to deal with the dynamically generated build directory.
  801. //
  802. // Otherwise, return the supplied path as it is almost certainly a source path
  803. // that is relative to the root of the source tree.
  804. //
  805. // The build and source paths should be distinguishable based on their contents.
  806. //
  807. // deprecated: use PathRelativeToTop instead as it handles make install paths and differentiates
  808. // between output and source properly.
  809. func NormalizePathForTesting(path Path) string {
  810. if path == nil {
  811. return "<nil path>"
  812. }
  813. p := path.String()
  814. if w, ok := path.(WritablePath); ok {
  815. rel, err := filepath.Rel(w.getBuildDir(), p)
  816. if err != nil {
  817. panic(err)
  818. }
  819. return rel
  820. }
  821. return p
  822. }
  823. // NormalizePathsForTesting creates a slice of strings where each string is the result of applying
  824. // NormalizePathForTesting to the corresponding Path in the input slice.
  825. //
  826. // deprecated: use PathsRelativeToTop instead as it handles make install paths and differentiates
  827. // between output and source properly.
  828. func NormalizePathsForTesting(paths Paths) []string {
  829. var result []string
  830. for _, path := range paths {
  831. relative := NormalizePathForTesting(path)
  832. result = append(result, relative)
  833. }
  834. return result
  835. }
  836. // PathRelativeToTop returns a string representation of the path relative to a notional top
  837. // directory.
  838. //
  839. // It return "<nil path>" if the supplied path is nil, otherwise it returns the result of calling
  840. // Path.RelativeToTop to obtain a relative Path and then calling Path.String on that to get the
  841. // string representation.
  842. func PathRelativeToTop(path Path) string {
  843. if path == nil {
  844. return "<nil path>"
  845. }
  846. return path.RelativeToTop().String()
  847. }
  848. // PathsRelativeToTop creates a slice of strings where each string is the result of applying
  849. // PathRelativeToTop to the corresponding Path in the input slice.
  850. func PathsRelativeToTop(paths Paths) []string {
  851. var result []string
  852. for _, path := range paths {
  853. relative := PathRelativeToTop(path)
  854. result = append(result, relative)
  855. }
  856. return result
  857. }
  858. // StringPathRelativeToTop returns a string representation of the path relative to a notional top
  859. // directory.
  860. //
  861. // See Path.RelativeToTop for more details as to what `relative to top` means.
  862. //
  863. // This is provided for processing paths that have already been converted into a string, e.g. paths
  864. // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against
  865. // which it can try and relativize paths. PathRelativeToTop must be used for process Path objects.
  866. func StringPathRelativeToTop(soongOutDir string, path string) string {
  867. ensureTestOnly()
  868. // A relative path must be a source path so leave it as it is.
  869. if !filepath.IsAbs(path) {
  870. return path
  871. }
  872. // Check to see if the path is relative to the soong out dir.
  873. rel, isRel, err := maybeRelErr(soongOutDir, path)
  874. if err != nil {
  875. panic(err)
  876. }
  877. if isRel {
  878. // The path is in the soong out dir so indicate that in the relative path.
  879. return filepath.Join("out/soong", rel)
  880. }
  881. // Check to see if the path is relative to the top level out dir.
  882. outDir := filepath.Dir(soongOutDir)
  883. rel, isRel, err = maybeRelErr(outDir, path)
  884. if err != nil {
  885. panic(err)
  886. }
  887. if isRel {
  888. // The path is in the out dir so indicate that in the relative path.
  889. return filepath.Join("out", rel)
  890. }
  891. // This should never happen.
  892. panic(fmt.Errorf("internal error: absolute path %s is not relative to the out dir %s", path, outDir))
  893. }
  894. // StringPathsRelativeToTop creates a slice of strings where each string is the result of applying
  895. // StringPathRelativeToTop to the corresponding string path in the input slice.
  896. //
  897. // This is provided for processing paths that have already been converted into a string, e.g. paths
  898. // in AndroidMkEntries structures. As a result it needs to be supplied the soong output dir against
  899. // which it can try and relativize paths. PathsRelativeToTop must be used for process Paths objects.
  900. func StringPathsRelativeToTop(soongOutDir string, paths []string) []string {
  901. var result []string
  902. for _, path := range paths {
  903. relative := StringPathRelativeToTop(soongOutDir, path)
  904. result = append(result, relative)
  905. }
  906. return result
  907. }