bp2build: don't generate filegroups (or error) if it contains a file
with the same name.
Also add capability to test for errors raised in bp2build mutators /
contexts.
This CL does two things to filegroups:
1) If the filegroup has only 1 source file with the same name as itself,
don't generate a filegroup target. Instead, dependents will depend
directly on the Bazel file target instead.
2) If the filegroup has more than 1 source file and 1 of them has the
same name as itself, the bp2build mutator will error out. If bp2build
on CI passes, it means that the source tree / product we're testing
against does not have such a case (which seems to be true for most
source trees).
Either way, this will allow us to be unblocked for most of the errant
filegroups (case 1) in the tree.
Test: CI
Test: New test cases in filegroup_conversion_test.go
Fixes: 194762573
Change-Id: I830c53efc8808569afe3c5f9f08436855bcdafed
diff --git a/bp2build/testing.go b/bp2build/testing.go
index a549a93..4995b09 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -36,14 +36,35 @@
buildDir string
)
-func errored(t *testing.T, desc string, errs []error) bool {
+func checkError(t *testing.T, errs []error, expectedErr error) bool {
t.Helper()
+
+ // expectedErr is not nil, find it in the list of errors
+ if len(errs) != 1 {
+ t.Errorf("Expected only 1 error, got %d: %q", len(errs), errs)
+ }
+ if errs[0].Error() == expectedErr.Error() {
+ return true
+ }
+
+ return false
+}
+
+func errored(t *testing.T, tc bp2buildTestCase, errs []error) bool {
+ t.Helper()
+ if tc.expectedErr != nil {
+ // Rely on checkErrors, as this test case is expected to have an error.
+ return false
+ }
+
if len(errs) > 0 {
for _, err := range errs {
- t.Errorf("%s: %s", desc, err)
+ t.Errorf("%s: %s", tc.description, err)
}
return true
}
+
+ // All good, continue execution.
return false
}
@@ -61,6 +82,7 @@
expectedBazelTargets []string
filesystem map[string]string
dir string
+ expectedErr error
}
func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
@@ -85,12 +107,17 @@
ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion()
- _, errs := ctx.ParseFileList(dir, toParse)
- if errored(t, tc.description, errs) {
+ _, parseErrs := ctx.ParseFileList(dir, toParse)
+ if errored(t, tc, parseErrs) {
return
}
- _, errs = ctx.ResolveDependencies(config)
- if errored(t, tc.description, errs) {
+ _, resolveDepsErrs := ctx.ResolveDependencies(config)
+ if errored(t, tc, resolveDepsErrs) {
+ return
+ }
+
+ errs := append(parseErrs, resolveDepsErrs...)
+ if tc.expectedErr != nil && checkError(t, errs, tc.expectedErr) {
return
}