diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go index a64d474d3..f652a3517 100644 --- a/bp2build/build_conversion.go +++ b/bp2build/build_conversion.go @@ -14,14 +14,20 @@ package bp2build +/* +For shareable/common functionality for conversion from soong-module to build files +for queryview/bp2build +*/ + import ( - "android/soong/android" - "android/soong/bazel" "fmt" "reflect" "sort" "strings" + "android/soong/android" + "android/soong/bazel" + "github.com/google/blueprint" "github.com/google/blueprint/proptools" ) 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..cf4632233 100644 --- a/bp2build/python_binary_conversion_test.go +++ b/bp2build/python_binary_conversion_test.go @@ -3,17 +3,11 @@ 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{ + runBp2BuildTestCaseSimple(t, bp2buildTestCase{ description: "simple python_binary_host converts to a native py_binary", moduleTypeUnderTest: "python_binary_host", moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, @@ -49,7 +43,7 @@ func TestPythonBinaryHostSimple(t *testing.T) { } func TestPythonBinaryHostPy2(t *testing.T) { - runPythonTestCase(t, bp2buildTestCase{ + runBp2BuildTestCaseSimple(t, bp2buildTestCase{ description: "py2 python_binary_host", moduleTypeUnderTest: "python_binary_host", moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, @@ -79,7 +73,7 @@ func TestPythonBinaryHostPy2(t *testing.T) { } func TestPythonBinaryHostPy3(t *testing.T) { - runPythonTestCase(t, bp2buildTestCase{ + runBp2BuildTestCaseSimple(t, bp2buildTestCase{ description: "py3 python_binary_host", moduleTypeUnderTest: "python_binary_host", moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, diff --git a/bp2build/testing.go b/bp2build/testing.go index 266b81780..3a77d0e23 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -1,6 +1,26 @@ +// Copyright 2021 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package bp2build +/* +For shareable/common bp2build testing functionality and dumping ground for +specific-but-shared functionality among tests in package +*/ + import ( + "strings" "testing" "android/soong/android" @@ -16,6 +36,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 runBp2BuildTestCaseSimple(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 +144,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) {