Add unit test for parsing build files in bp2build
This involves some minor changes to testing infrastructure.
This is a rollforward of aosp/2628496 with a couple of minor changes:
- In ParseBuildFiles, filter out all build files that are kept due
to ShouldKeepExistingBuildFileForDir
- Add some minor test infrastructure for StubbedBuildDefinitions, with a
couple of proof of concept tests used to demonstrate its usage. This
pattern will become immensely more common as we implement allowlist v2
(as we will need to update all tests which today simulate build
definitions that have missing deps)
Bug: 285631638
Fixes: 286545783
Test: bp2build.sh
Test: m nothing
Change-Id: I7c3a03b02098e39dd8e51d327482b440f294478f
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 997df64..7333c0b 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -21,6 +21,7 @@
import (
"fmt"
+ "path/filepath"
"sort"
"strings"
"testing"
@@ -82,7 +83,16 @@
// ExpectedBazelTargets compares the BazelTargets generated in `Dir` (if not empty).
// Otherwise, it checks the BazelTargets generated by `Blueprint` in the root directory.
ExpectedBazelTargets []string
- Filesystem map[string]string
+ // AlreadyExistingBuildContents, if non-empty, simulates an already-present source BUILD file
+ // in the directory under test. The BUILD file has the given contents. This BUILD file
+ // will also be treated as "BUILD file to keep" by the simulated bp2build environment.
+ AlreadyExistingBuildContents string
+ // StubbedBuildDefinitions, if non-empty, adds stub definitions to an already-present source
+ // BUILD file in the directory under test, for each target name given. These stub definitions
+ // are added to the BUILD file given in AlreadyExistingBuildContents, if it is set.
+ StubbedBuildDefinitions []string
+
+ Filesystem map[string]string
// Dir sets the directory which will be compared against the targets in ExpectedBazelTargets.
// This should used in conjunction with the Filesystem property to check for targets
// generated from a directory that is not the root.
@@ -110,11 +120,31 @@
func runBp2BuildTestCaseWithSetup(t *testing.T, extraPreparer android.FixturePreparer, tc Bp2buildTestCase) {
t.Helper()
- dir := "."
+ checkDir := "."
+ if tc.Dir != "" {
+ checkDir = tc.Dir
+ }
+ keepExistingBuildDirs := tc.KeepBuildFileForDirs
+ buildFilesToParse := []string{}
filesystem := make(map[string][]byte)
for f, content := range tc.Filesystem {
filesystem[f] = []byte(content)
}
+ alreadyExistingBuildContents := tc.AlreadyExistingBuildContents
+ if len(tc.StubbedBuildDefinitions) > 0 {
+ for _, targetName := range tc.StubbedBuildDefinitions {
+ alreadyExistingBuildContents += MakeBazelTarget(
+ "bp2build_test_stub",
+ targetName,
+ AttrNameToString{})
+ }
+ }
+ if len(alreadyExistingBuildContents) > 0 {
+ buildFilePath := filepath.Join(checkDir, "BUILD")
+ filesystem[buildFilePath] = []byte(alreadyExistingBuildContents)
+ keepExistingBuildDirs = append(keepExistingBuildDirs, checkDir)
+ buildFilesToParse = append(buildFilesToParse, buildFilePath)
+ }
preparers := []android.FixturePreparer{
extraPreparer,
@@ -123,7 +153,7 @@
android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
}),
- android.FixtureModifyContext(func(ctx *android.TestContext) {
+ android.FixtureModifyContextWithMockFs(func(ctx *android.TestContext) {
// A default configuration for tests to not have to specify bp2build_available on top level
// targets.
bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig(
@@ -131,7 +161,7 @@
android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
},
)
- for _, f := range tc.KeepBuildFileForDirs {
+ for _, f := range keepExistingBuildDirs {
bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{
f: /*recursive=*/ false,
})
@@ -141,6 +171,10 @@
// from cloning modules to their original state after mutators run. This
// would lose some data intentionally set by these mutators.
ctx.SkipCloneModulesAfterMutators = true
+ err := ctx.RegisterExistingBazelTargets(".", buildFilesToParse)
+ if err != nil {
+ t.Errorf("error parsing build files in test setup: %s", err)
+ }
}),
android.FixtureModifyEnv(func(env map[string]string) {
if tc.UnconvertedDepsMode == errorModulesUnconvertedDeps {
@@ -159,10 +193,6 @@
return
}
- checkDir := dir
- if tc.Dir != "" {
- checkDir = tc.Dir
- }
expectedTargets := map[string][]string{
checkDir: tc.ExpectedBazelTargets,
}