diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index bff192f53..c8400168c 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -15,10 +15,10 @@ package bp2build import ( + "testing" + "android/soong/android" "android/soong/cc" - "strings" - "testing" ) const ( @@ -54,59 +54,6 @@ func registerCcLibraryModuleTypes(ctx android.RegistrationContext) { ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory) } -func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) { - t.Helper() - dir := "." - filesystem := make(map[string][]byte) - toParse := []string{ - "Android.bp", - } - for f, content := range tc.filesystem { - if strings.HasSuffix(f, "Android.bp") { - toParse = append(toParse, f) - } - filesystem[f] = []byte(content) - } - config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem) - ctx := android.NewTestContext(config) - - registerModuleTypes(ctx) - ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory) - ctx.RegisterBp2BuildConfig(bp2buildConfig) - ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator) - ctx.RegisterForBazelConversion() - - _, errs := ctx.ParseFileList(dir, toParse) - if errored(t, tc.description, errs) { - return - } - _, errs = ctx.ResolveDependencies(config) - if errored(t, tc.description, errs) { - return - } - - checkDir := dir - if tc.dir != "" { - checkDir = tc.dir - } - codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) - bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) - if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount { - t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount) - } else { - for i, target := range bazelTargets { - if w, g := tc.expectedBazelTargets[i], target.content; w != g { - t.Errorf( - "%s: Expected generated Bazel target to be '%s', got '%s'", - tc.description, - w, - g, - ) - } - } - } -} - func TestCcLibrarySimple(t *testing.T) { runCcLibraryTestCase(t, bp2buildTestCase{ description: "cc_library - simple example", diff --git a/bp2build/cc_library_headers_conversion_test.go b/bp2build/cc_library_headers_conversion_test.go index 712d0bd5a..ea2c10a97 100644 --- a/bp2build/cc_library_headers_conversion_test.go +++ b/bp2build/cc_library_headers_conversion_test.go @@ -40,17 +40,6 @@ toolchain_library { }` ) -type bp2buildTestCase struct { - description string - moduleTypeUnderTest string - moduleTypeUnderTestFactory android.ModuleFactory - moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) - blueprint string - expectedBazelTargets []string - filesystem map[string]string - dir string -} - func TestCcLibraryHeadersLoadStatement(t *testing.T) { testCases := []struct { bazelTargets BazelTargets diff --git a/bp2build/python_binary_conversion_test.go b/bp2build/python_binary_conversion_test.go index 7bedf7120..cf29ac17b 100644 --- a/bp2build/python_binary_conversion_test.go +++ b/bp2build/python_binary_conversion_test.go @@ -3,15 +3,9 @@ package bp2build import ( "testing" - "android/soong/android" "android/soong/python" ) -func runPythonTestCase(t *testing.T, tc bp2buildTestCase) { - t.Helper() - runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) -} - func TestPythonBinaryHostSimple(t *testing.T) { runPythonTestCase(t, bp2buildTestCase{ description: "simple python_binary_host converts to a native py_binary", diff --git a/bp2build/testing.go b/bp2build/testing.go index 266b81780..d8e572d66 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -1,6 +1,7 @@ package bp2build import ( + "strings" "testing" "android/soong/android" @@ -16,6 +17,86 @@ var ( buildDir string ) +func errored(t *testing.T, desc string, errs []error) bool { + t.Helper() + if len(errs) > 0 { + for _, err := range errs { + t.Errorf("%s: %s", desc, err) + } + return true + } + return false +} + +func runPythonTestCase(t *testing.T, tc bp2buildTestCase) { + t.Helper() + runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) +} + +type bp2buildTestCase struct { + description string + moduleTypeUnderTest string + moduleTypeUnderTestFactory android.ModuleFactory + moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) + blueprint string + expectedBazelTargets []string + filesystem map[string]string + dir string +} + +func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) { + t.Helper() + dir := "." + filesystem := make(map[string][]byte) + toParse := []string{ + "Android.bp", + } + for f, content := range tc.filesystem { + if strings.HasSuffix(f, "Android.bp") { + toParse = append(toParse, f) + } + filesystem[f] = []byte(content) + } + config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem) + ctx := android.NewTestContext(config) + + registerModuleTypes(ctx) + ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory) + ctx.RegisterBp2BuildConfig(bp2buildConfig) + ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator) + ctx.RegisterForBazelConversion() + + _, errs := ctx.ParseFileList(dir, toParse) + if errored(t, tc.description, errs) { + return + } + _, errs = ctx.ResolveDependencies(config) + if errored(t, tc.description, errs) { + return + } + + checkDir := dir + if tc.dir != "" { + checkDir = tc.dir + } + codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) + bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) + if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount { + t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount) + } else { + for i, target := range bazelTargets { + if w, g := tc.expectedBazelTargets[i], target.content; w != g { + t.Errorf( + "%s: Expected generated Bazel target to be '%s', got '%s'", + tc.description, + w, + g, + ) + } + } + } +} + type nestedProps struct { Nested_prop string } @@ -44,17 +125,6 @@ type customModule struct { props customProps } -func errored(t *testing.T, desc string, errs []error) bool { - t.Helper() - if len(errs) > 0 { - for _, err := range errs { - t.Errorf("%s: %s", desc, err) - } - return true - } - return false -} - // OutputFiles is needed because some instances of this module use dist with a // tag property which requires the module implements OutputFileProducer. func (m *customModule) OutputFiles(tag string) (android.Paths, error) {