From 1c92aeff4e74601a11e37ca5793891e964a80a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20M=C3=A1rquez=20P=C3=A9rez=20Mu=C3=B1=C3=ADz=20D?= =?UTF-8?q?=C3=ADaz=20P=C3=BAras=20Thaureaux?= Date: Mon, 23 Aug 2021 16:10:00 +0000 Subject: [PATCH 1/4] Share runBp2BuildTestCase & runPythonTestCase Pull along bp2buildTestCase as well Because none are specific to their current hosts Move `errored` up to the "generic" block vs. the "custom" block Test: Existing tests pass Change-Id: Ie8d5322a5554ebd88f0c142a4c541791687be326 --- bp2build/cc_library_conversion_test.go | 57 +----------- .../cc_library_headers_conversion_test.go | 11 --- bp2build/python_binary_conversion_test.go | 6 -- bp2build/testing.go | 92 ++++++++++++++++--- 4 files changed, 83 insertions(+), 83 deletions(-) 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) { From ce0a07e249d37eb7f305bb92a044ab36d0f7f962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20M=C3=A1rquez=20P=C3=A9rez=20Mu=C3=B1=C3=ADz=20D?= =?UTF-8?q?=C3=ADaz=20P=C3=BAras=20Thaureaux?= Date: Mon, 23 Aug 2021 16:17:32 +0000 Subject: [PATCH 2/4] Rename runPythonTestCase To runBp2BuildTestCaseSimple because it's not Python-specific but rather Bp2Build-specific Name is suboptimal but given current functionality and use we couldn't come up with a better one. Test: Existing tests pass Change-Id: I641cd051af741836b21ec6a7d6639516fb13028f --- bp2build/python_binary_conversion_test.go | 6 +++--- bp2build/testing.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bp2build/python_binary_conversion_test.go b/bp2build/python_binary_conversion_test.go index cf29ac17b..cf4632233 100644 --- a/bp2build/python_binary_conversion_test.go +++ b/bp2build/python_binary_conversion_test.go @@ -7,7 +7,7 @@ import ( ) 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, @@ -43,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, @@ -73,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 d8e572d66..376bf49d2 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -28,7 +28,7 @@ func errored(t *testing.T, desc string, errs []error) bool { return false } -func runPythonTestCase(t *testing.T, tc bp2buildTestCase) { +func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) { t.Helper() runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) } From 0da7ce6f393c737b8439d209fa178bc6ec76fe8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20M=C3=A1rquez=20P=C3=A9rez=20Mu=C3=B1=C3=ADz=20D?= =?UTF-8?q?=C3=ADaz=20P=C3=BAras=20Thaureaux?= Date: Mon, 23 Aug 2021 17:04:20 +0000 Subject: [PATCH 3/4] Document testing & build_conversion purposes Formatter also relocated some imports Test: Existing tests pass Change-Id: I48b1cb65400e463a6b4e91fd9fe76e13d8d81cd6 --- bp2build/build_conversion.go | 10 ++++++++-- bp2build/testing.go | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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/testing.go b/bp2build/testing.go index 376bf49d2..ba328e941 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -1,5 +1,10 @@ package bp2build +/* +For shareable/common bp2build testing functionality and dumping ground for +specific-but-shared functionality among tests in package +*/ + import ( "strings" "testing" From f5a3eacc072b2fca2b96f32287b85080f29c9746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20M=C3=A1rquez=20P=C3=A9rez=20Mu=C3=B1=C3=ADz=20D?= =?UTF-8?q?=C3=ADaz=20P=C3=BAras=20Thaureaux?= Date: Mon, 23 Aug 2021 17:05:17 +0000 Subject: [PATCH 4/4] Add License decl to testing.go Test: Existing tests pass Change-Id: I0d8b77d87801cb361036c8788f30bd80070fd7ff --- bp2build/testing.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bp2build/testing.go b/bp2build/testing.go index ba328e941..3a77d0e23 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -1,3 +1,17 @@ +// 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 /*