Add unit test for parsing build files in bp2build
This involves some minor changes to testing infrastructure.
Bug: 285631638
Fixes: 286545783
Test: unit test
Change-Id: If64ba29308d99e63a1cc5526feaf077f2cb14478
diff --git a/android/fixture.go b/android/fixture.go
index dbc3bc5..03abb7f 100644
--- a/android/fixture.go
+++ b/android/fixture.go
@@ -275,6 +275,15 @@
})
}
+// Sync the mock filesystem with the current config, then modify the context,
+// This allows context modification that requires filesystem access.
+func FixtureModifyContextWithMockFs(mutator func(ctx *TestContext)) FixturePreparer {
+ return newSimpleFixturePreparer(func(f *fixture) {
+ f.config.mockFileSystem("", f.mockFS)
+ mutator(f.ctx)
+ })
+}
+
func FixtureRegisterWithContext(registeringFunc func(ctx RegistrationContext)) FixturePreparer {
return FixtureModifyContext(func(ctx *TestContext) { registeringFunc(ctx) })
}
diff --git a/android/register.go b/android/register.go
index 64b0207..5d277e1 100644
--- a/android/register.go
+++ b/android/register.go
@@ -15,9 +15,13 @@
package android
import (
+ "bufio"
"fmt"
+ "path/filepath"
"reflect"
+ "regexp"
+ "android/soong/shared"
"github.com/google/blueprint"
)
@@ -197,6 +201,49 @@
RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators)
}
+func (c *Context) ParseBuildFiles(topDir string, existingBazelFiles []string) error {
+ result := map[string][]string{}
+
+ // Search for instances of `name = "$NAME"` (with arbitrary spacing).
+ targetNameRegex := regexp.MustCompile(`(?m)^\s*name\s*=\s*\"([^\"]+)\"`)
+
+ parseBuildFile := func(path string) error {
+ fullPath := shared.JoinPath(topDir, path)
+ sourceDir := filepath.Dir(path)
+
+ fileInfo, err := c.Config().fs.Stat(fullPath)
+ if err != nil {
+ return fmt.Errorf("Error accessing Bazel file '%s': %s", path, err)
+ }
+ if !fileInfo.IsDir() &&
+ (fileInfo.Name() == "BUILD" || fileInfo.Name() == "BUILD.bazel") {
+ f, err := c.Config().fs.Open(fullPath)
+ if err != nil {
+ return fmt.Errorf("Error reading Bazel file '%s': %s", path, err)
+ }
+ defer f.Close()
+ scanner := bufio.NewScanner(f)
+ for scanner.Scan() {
+ line := scanner.Text()
+ matches := targetNameRegex.FindAllStringSubmatch(line, -1)
+ for _, match := range matches {
+ result[sourceDir] = append(result[sourceDir], match[1])
+ }
+ }
+ }
+ return nil
+ }
+
+ for _, path := range existingBazelFiles {
+ err := parseBuildFile(path)
+ if err != nil {
+ return err
+ }
+ }
+ c.Config().SetBazelBuildFileTargets(result)
+ return nil
+}
+
// RegisterForApiBazelConversion is similar to RegisterForBazelConversion except that
// it only generates API targets in the generated workspace
func (ctx *Context) RegisterForApiBazelConversion() {