Split each test case to a different test function.

This is so that they can be individually debugged.

Test: Presubmits.
Change-Id: I7d929c4126bba7470aaa1c0def85bad65429ffdc
This commit is contained in:
Lukacs T. Berki
2021-05-21 09:37:00 +02:00
parent 54c98f5b4a
commit c1cc3b9678
6 changed files with 873 additions and 1099 deletions

View File

@@ -40,19 +40,74 @@ toolchain_library {
}`
)
func TestCcLibraryBp2Build(t *testing.T) {
testCases := []struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
bp string
expectedBazelTargets []string
filesystem map[string]string
dir string
depsMutators []android.RegisterMutatorFunc
}{
{
func runCcLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
runBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
}
func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
}
func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
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)
for _, m := range tc.depsMutators {
ctx.DepsBp2BuildMutators(m)
}
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",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -76,7 +131,7 @@ func TestCcLibraryBp2Build(t *testing.T) {
"x86_64.cpp": "",
"foo-dir/a.h": "",
},
bp: soongCcLibraryPreamble + `
blueprint: soongCcLibraryPreamble + `
cc_library_headers { name: "some-headers" }
cc_library {
name: "foo-lib",
@@ -132,9 +187,11 @@ cc_library {
"//build/bazel/platforms/os:linux": ["linux.cpp"],
"//conditions:default": [],
}),
)`},
},
{
)`}})
}
func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library - trimmed example of //bionic/linker:ld-android",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -146,7 +203,7 @@ cc_library {
"linker_block_allocator.h": "",
"linker_cfi.h": "",
},
bp: soongCcLibraryPreamble + `
blueprint: soongCcLibraryPreamble + `
cc_library_headers { name: "libc_headers" }
cc_library {
name: "fake-ld-android",
@@ -201,8 +258,11 @@ cc_library {
}),
srcs = ["ld_android.cpp"],
)`},
},
{
})
}
func TestCcLibraryExcludeSrcs(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -241,7 +301,7 @@ cc_library {
}
`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "fake-libarm-optimized-routines-math",
copts = [
@@ -253,8 +313,11 @@ cc_library {
}),
srcs = ["math/cosf.c"],
)`},
},
{
})
}
func TestCcLibrarySharedStaticProps(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library shared/static props",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -309,7 +372,7 @@ cc_library { name: "shared_dep_for_static" }
cc_library { name: "shared_dep_for_both" }
`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
@@ -332,8 +395,11 @@ cc_library { name: "shared_dep_for_both" }
whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
whole_archive_deps_for_static = [":whole_static_lib_for_static"],
)`},
},
{
})
}
func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library non-configured version script",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -350,7 +416,7 @@ cc_library {
}
`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
@@ -360,8 +426,11 @@ cc_library {
srcs = ["a.cpp"],
version_script = "v.map",
)`},
},
{
})
}
func TestCcLibraryConfiguredVersionScript(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library configured version script",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -386,7 +455,7 @@ cc_library {
}
`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
@@ -400,8 +469,11 @@ cc_library {
"//conditions:default": None,
}),
)`},
},
{
})
}
func TestCcLibrarySharedLibs(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library shared_libs",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -422,7 +494,7 @@ cc_library {
}
`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
@@ -437,8 +509,11 @@ cc_library {
"-I$(BINDIR)/foo/bar",
],
)`},
},
{
})
}
func TestCcLibraryPackRelocations(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library pack_relocations test",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -476,7 +551,7 @@ cc_library {
bazel_module: { bp2build_available: true },
}`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
@@ -508,8 +583,11 @@ cc_library {
}),
srcs = ["c.cpp"],
)`},
},
{
})
}
func TestCcLibrarySpacesInCopts(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library spaces in copts",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -525,7 +603,7 @@ cc_library {
}
`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
@@ -535,8 +613,11 @@ cc_library {
"-I$(BINDIR)/foo/bar",
],
)`},
},
{
})
}
func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
runCcLibraryTestCase(t, bp2buildTestCase{
description: "cc_library cppflags goes into copts",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
@@ -568,7 +649,7 @@ cc_library {
}
`,
},
bp: soongCcLibraryPreamble,
blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library(
name = "a",
copts = [
@@ -586,64 +667,5 @@ cc_library {
}),
srcs = ["a.cpp"],
)`},
},
}
dir := "."
for _, testCase := range testCases {
filesystem := make(map[string][]byte)
toParse := []string{
"Android.bp",
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
filesystem[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
ctx := android.NewTestContext(config)
cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterBp2BuildConfig(bp2buildConfig) // TODO(jingwen): make this the default for all tests
for _, m := range testCase.depsMutators {
ctx.DepsBp2BuildMutators(m)
}
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase.description, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase.description, errs) {
continue
}
checkDir := dir
if testCase.dir != "" {
checkDir = testCase.dir
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
}
})
}

View File

@@ -15,10 +15,10 @@
package bp2build
import (
"testing"
"android/soong/android"
"android/soong/cc"
"strings"
"testing"
)
const (
@@ -40,6 +40,18 @@ toolchain_library {
}`
)
type bp2buildTestCase struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
depsMutators []android.RegisterMutatorFunc
blueprint string
expectedBazelTargets []string
filesystem map[string]string
dir string
}
func TestCcLibraryHeadersLoadStatement(t *testing.T) {
testCases := []struct {
bazelTargets BazelTargets
@@ -64,23 +76,19 @@ func TestCcLibraryHeadersLoadStatement(t *testing.T) {
t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
}
}
}
func TestCcLibraryHeadersBp2Build(t *testing.T) {
testCases := []struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
preArchMutators []android.RegisterMutatorFunc
depsMutators []android.RegisterMutatorFunc
bp string
expectedBazelTargets []string
filesystem map[string]string
dir string
}{
{
func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) {
cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
}
func runCcLibraryHeadersTestCase(t *testing.T, tc bp2buildTestCase) {
runBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc)
}
func TestCcLibraryHeadersSimple(t *testing.T) {
runCcLibraryHeadersTestCase(t, bp2buildTestCase{
description: "cc_library_headers test",
moduleTypeUnderTest: "cc_library_headers",
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
@@ -98,7 +106,7 @@ func TestCcLibraryHeadersBp2Build(t *testing.T) {
"arch_x86_exported_include_dir/b.h": "",
"arch_x86_64_exported_include_dir/c.h": "",
},
bp: soongCcLibraryHeadersPreamble + `
blueprint: soongCcLibraryHeadersPreamble + `
cc_library_headers {
name: "lib-1",
export_include_dirs: ["lib-1"],
@@ -163,15 +171,18 @@ cc_library_headers {
],
includes = ["lib-2"],
)`},
},
{
})
}
func TestCcLibraryHeadersOSSpecificHeader(t *testing.T) {
runCcLibraryHeadersTestCase(t, bp2buildTestCase{
description: "cc_library_headers test with os-specific header_libs props",
moduleTypeUnderTest: "cc_library_headers",
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryPreamble + `
blueprint: soongCcLibraryPreamble + `
cc_library_headers { name: "android-lib" }
cc_library_headers { name: "base-lib" }
cc_library_headers { name: "darwin-lib" }
@@ -250,15 +261,18 @@ cc_library_headers {
"-I$(BINDIR)/.",
],
)`},
},
{
})
}
func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
runCcLibraryHeadersTestCase(t, bp2buildTestCase{
description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
moduleTypeUnderTest: "cc_library_headers",
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryPreamble + `
blueprint: soongCcLibraryPreamble + `
cc_library_headers { name: "android-lib" }
cc_library_headers { name: "exported-lib" }
cc_library_headers {
@@ -294,15 +308,18 @@ cc_library_headers {
"//conditions:default": [],
}),
)`},
},
{
})
}
func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
runCcLibraryHeadersTestCase(t, bp2buildTestCase{
description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
moduleTypeUnderTest: "cc_library_headers",
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryPreamble + `cc_library_headers {
blueprint: soongCcLibraryPreamble + `cc_library_headers {
name: "foo_headers",
export_system_include_dirs: [
"shared_include_dir",
@@ -354,65 +371,5 @@ cc_library_headers {
"//conditions:default": [],
}),
)`},
},
}
dir := "."
for _, testCase := range testCases {
filesystem := make(map[string][]byte)
toParse := []string{
"Android.bp",
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
filesystem[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
ctx := android.NewTestContext(config)
// TODO(jingwen): make this default for all bp2build tests
ctx.RegisterBp2BuildConfig(bp2buildConfig)
cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
for _, m := range testCase.depsMutators {
ctx.DepsBp2BuildMutators(m)
}
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase.description, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase.description, errs) {
continue
}
checkDir := dir
if testCase.dir != "" {
checkDir = testCase.dir
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
}
})
}

View File

@@ -19,7 +19,6 @@ import (
"android/soong/cc"
"android/soong/genrule"
"strings"
"testing"
)
@@ -69,21 +68,20 @@ func TestCcLibraryStaticLoadStatement(t *testing.T) {
}
func TestCcLibraryStaticBp2Build(t *testing.T) {
testCases := []struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
preArchMutators []android.RegisterMutatorFunc
depsMutators []android.RegisterMutatorFunc
bp string
expectedBazelTargets []string
filesystem map[string]string
dir string
}{
{
description: "cc_library_static simple test",
func registerCcLibraryStaticModuleTypes(ctx android.RegistrationContext) {
cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
}
func runCcLibraryStaticTestCase(t *testing.T, tc bp2buildTestCase) {
runBp2BuildTestCase(t, registerCcLibraryStaticModuleTypes, tc)
}
func TestCcLibraryStaticSimple(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static test",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
@@ -107,7 +105,7 @@ func TestCcLibraryStaticBp2Build(t *testing.T) {
"implicit_include_1.h": "",
"implicit_include_2.h": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_headers {
name: "header_lib_1",
export_include_dirs: ["header_lib_1"],
@@ -243,8 +241,11 @@ cc_library_static {
linkstatic = True,
srcs = ["whole_static_lib_2.cc"],
)`},
},
{
})
}
func TestCcLibraryStaticSubpackage(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static subpackage test",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -263,7 +264,7 @@ cc_library_static {
"subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "",
"subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: [
@@ -282,8 +283,11 @@ cc_library_static {
],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticExportIncludeDir(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static export include dir",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -294,7 +298,7 @@ cc_library_static {
"subpackage/subpackage_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
export_include_dirs: ["subpackage"],
@@ -308,8 +312,11 @@ cc_library_static {
includes = ["subpackage"],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticExportSystemIncludeDir(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static export system include dir",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -320,7 +327,7 @@ cc_library_static {
"subpackage/subpackage_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
export_system_include_dirs: ["subpackage"],
@@ -334,8 +341,11 @@ cc_library_static {
includes = ["subpackage"],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticManyIncludeDirs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -363,7 +373,7 @@ cc_library_static {
"subpackage2/header.h": "",
"subpackage3/subsubpackage/header.h": "",
},
bp: soongCcLibraryStaticPreamble,
blueprint: soongCcLibraryStaticPreamble,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = [
@@ -381,8 +391,11 @@ cc_library_static {
includes = ["./exported_subsubpackage"],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticIncludeBuildDirectoryDisabled(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static include_build_directory disabled",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -393,7 +406,7 @@ cc_library_static {
"subpackage/subpackage_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
@@ -410,8 +423,11 @@ cc_library_static {
],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticIncludeBuildDirectoryEnabled(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static include_build_directory enabled",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -424,7 +440,7 @@ cc_library_static {
"subpackage2/subpackage2_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
@@ -443,15 +459,18 @@ cc_library_static {
],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticArchSpecificStaticLib(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static arch-specific static_libs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static { name: "static_dep" }
cc_library_static { name: "static_dep2" }
cc_library_static {
@@ -488,15 +507,18 @@ cc_library_static {
],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticOsSpecificStaticLib(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static os-specific static_libs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static { name: "static_dep" }
cc_library_static { name: "static_dep2" }
cc_library_static {
@@ -533,15 +555,18 @@ cc_library_static {
],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticBaseArchOsSpecificStaticLib(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static base, arch and os-specific static_libs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static { name: "static_dep" }
cc_library_static { name: "static_dep2" }
cc_library_static { name: "static_dep3" }
@@ -597,8 +622,11 @@ cc_library_static {
],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticSimpleExcludeSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static simple exclude_srcs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -609,7 +637,7 @@ cc_library_static {
"foo-a.c": "",
"foo-excluded.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: ["common.c", "foo-*.c"],
@@ -627,8 +655,11 @@ cc_library_static {
"foo-a.c",
],
)`},
},
{
})
}
func TestCcLibraryStaticOneArchSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static one arch specific srcs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -638,7 +669,7 @@ cc_library_static {
"common.c": "",
"foo-arm.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: ["common.c"],
@@ -656,8 +687,11 @@ cc_library_static {
"//conditions:default": [],
}),
)`},
},
{
})
}
func TestCcLibraryStaticOneArchSrcsExcludeSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static one arch specific srcs and exclude_srcs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -669,7 +703,7 @@ cc_library_static {
"not-for-arm.c": "",
"not-for-anything.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: ["common.c", "not-for-*.c"],
@@ -690,8 +724,11 @@ cc_library_static {
"//conditions:default": ["not-for-arm.c"],
}),
)`},
},
{
})
}
func TestCcLibraryStaticTwoArchExcludeSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static arch specific exclude_srcs for 2 architectures",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -704,7 +741,7 @@ cc_library_static {
"not-for-arm.c": "",
"not-for-x86.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: ["common.c", "not-for-*.c"],
@@ -736,8 +773,10 @@ cc_library_static {
],
}),
)`},
},
{
})
}
func TestCcLibraryStaticFourArchExcludeSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static arch specific exclude_srcs for 4 architectures",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -755,7 +794,7 @@ cc_library_static {
"not-for-x86_64.c": "",
"not-for-everything.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: ["common.c", "not-for-*.c"],
@@ -807,15 +846,18 @@ cc_library_static {
],
}),
)`},
},
{
})
}
func TestCcLibraryStaticMultipleDepSameName(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static multiple dep same name panic",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static { name: "static_dep" }
cc_library_static {
name: "foo_static",
@@ -837,8 +879,11 @@ cc_library_static {
],
linkstatic = True,
)`},
},
{
})
}
func TestCcLibraryStaticOneMultilibSrcsExcludeSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static 1 multilib srcs and exclude_srcs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -849,7 +894,7 @@ cc_library_static {
"for-lib32.c": "",
"not-for-lib32.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
srcs: ["common.c", "not-for-*.c"],
@@ -870,8 +915,11 @@ cc_library_static {
"//conditions:default": ["not-for-lib32.c"],
}),
)`},
},
{
})
}
func TestCcLibraryStaticTwoMultilibSrcsExcludeSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static 2 multilib srcs and exclude_srcs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -884,7 +932,7 @@ cc_library_static {
"not-for-lib32.c": "",
"not-for-lib64.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static2",
srcs: ["common.c", "not-for-*.c"],
@@ -923,8 +971,11 @@ cc_library_static {
],
}),
)`},
},
{
})
}
func TestCcLibrarySTaticArchMultilibSrcsExcludeSrcs(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static arch and multilib srcs and exclude_srcs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -946,7 +997,7 @@ cc_library_static {
"not-for-lib64.c": "",
"not-for-everything.c": "",
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static3",
srcs: ["common.c", "not-for-*.c"],
@@ -1012,8 +1063,11 @@ cc_library_static {
],
}),
)`},
},
{
})
}
func TestCcLibraryStaticArchSrcsExcludeSrcsGeneratedFiles(t *testing.T) {
runCcLibraryStaticTestCase(t, bp2buildTestCase{
description: "cc_library_static arch srcs/exclude_srcs with generated files",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
@@ -1043,7 +1097,7 @@ genrule {
cmd: "nothing to see here",
}`,
},
bp: soongCcLibraryStaticPreamble + `
blueprint: soongCcLibraryStaticPreamble + `
genrule {
name: "generated_src",
out: ["generated_src.cpp"],
@@ -1100,65 +1154,5 @@ cc_library_static {
"//conditions:default": ["not-for-x86.c"],
}),
)`},
},
}
dir := "."
for _, testCase := range testCases {
filesystem := make(map[string][]byte)
toParse := []string{
"Android.bp",
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
filesystem[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
ctx := android.NewTestContext(config)
cc.RegisterCCBuildComponents(ctx)
ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
for _, m := range testCase.depsMutators {
ctx.DepsBp2BuildMutators(m)
}
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterBp2BuildConfig(bp2buildConfig)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase.description, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase.description, errs) {
continue
}
checkDir := dir
if testCase.dir != "" {
checkDir = testCase.dir
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
}
})
}

View File

@@ -15,24 +15,23 @@
package bp2build
import (
"testing"
"android/soong/android"
"android/soong/cc"
"fmt"
"strings"
"testing"
)
func TestCcObjectBp2Build(t *testing.T) {
testCases := []struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
blueprint string
expectedBazelTargets []string
filesystem map[string]string
}{
{
func registerCcObjectModuleTypes(ctx android.RegistrationContext) {
// Always register cc_defaults module factory
ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
}
func runCcObjectTestCase(t *testing.T, tc bp2buildTestCase) {
runBp2BuildTestCase(t, registerCcObjectModuleTypes, tc)
}
func TestCcObjectSimple(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "simple cc_object generates cc_object with include header dep",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -72,8 +71,11 @@ func TestCcObjectBp2Build(t *testing.T) {
srcs = ["a/b/c.c"],
)`,
},
},
{
})
}
func TestCcObjectDefaults(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "simple cc_object with defaults",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -117,9 +119,11 @@ cc_defaults {
],
srcs = ["a/b/c.c"],
)`,
},
},
{
}})
}
func TestCcObjectCcObjetDepsInObjs(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with cc_object deps in objs props",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -158,8 +162,11 @@ cc_object {
srcs = ["a/b/c.c"],
)`,
},
},
{
})
}
func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with include_build_dir: false",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -180,8 +187,11 @@ cc_object {
srcs = ["a/b/c.c"],
)`,
},
},
{
})
}
func TestCcObjectProductVariable(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with product variable",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -202,71 +212,11 @@ cc_object {
copts = ["-fno-addrsig"],
)`,
},
},
}
dir := "."
for _, testCase := range testCases {
filesystem := make(map[string][]byte)
toParse := []string{
"Android.bp",
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
filesystem[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.blueprint, filesystem)
ctx := android.NewTestContext(config)
// Always register cc_defaults module factory
ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterBp2BuildConfig(bp2buildConfig)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase.description, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase.description, errs) {
continue
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
fmt.Println(bazelTargets)
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
}
})
}
func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
testCases := []struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
blueprint string
expectedBazelTargets []string
filesystem map[string]string
}{
{
func TestCcObjectCflagsOneArch(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for one arch",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -301,8 +251,11 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
}),
)`,
},
},
{
})
}
func TestCcObjectCflagsFourArch(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for 4 architectures",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -353,8 +306,11 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
}),
)`,
},
},
{
})
}
func TestCcObjectCflagsMultiOs(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for multiple OSes",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
@@ -391,50 +347,5 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
srcs = ["base.cpp"],
)`,
},
},
}
dir := "."
for _, testCase := range testCases {
filesystem := make(map[string][]byte)
toParse := []string{
"Android.bp",
}
config := android.TestConfig(buildDir, nil, testCase.blueprint, filesystem)
ctx := android.NewTestContext(config)
// Always register cc_defaults module factory
ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterBp2BuildConfig(bp2buildConfig)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase.description, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase.description, errs) {
continue
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
fmt.Println(bazelTargets)
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
}
})
}

View File

@@ -1,24 +1,18 @@
package bp2build
import (
"testing"
"android/soong/android"
"android/soong/python"
"fmt"
"strings"
"testing"
)
func TestPythonBinaryHost(t *testing.T) {
testCases := []struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
blueprint string
expectedBazelTargets []string
filesystem map[string]string
}{
{
func runPythonTestCase(t *testing.T, tc bp2buildTestCase) {
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",
moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
@@ -50,8 +44,11 @@ func TestPythonBinaryHost(t *testing.T) {
],
)`,
},
},
{
})
}
func TestPythonBinaryHostPy2(t *testing.T) {
runPythonTestCase(t, bp2buildTestCase{
description: "py2 python_binary_host",
moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
@@ -77,8 +74,11 @@ func TestPythonBinaryHost(t *testing.T) {
srcs = ["a.py"],
)`,
},
},
{
})
}
func TestPythonBinaryHostPy3(t *testing.T) {
runPythonTestCase(t, bp2buildTestCase{
description: "py3 python_binary_host",
moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
@@ -105,53 +105,5 @@ func TestPythonBinaryHost(t *testing.T) {
srcs = ["a.py"],
)`,
},
},
}
dir := "."
for _, testCase := range testCases {
filesystem := make(map[string][]byte)
toParse := []string{
"Android.bp",
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
filesystem[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.blueprint, filesystem)
ctx := android.NewTestContext(config)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase.description, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase.description, errs) {
continue
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
fmt.Println(bazelTargets)
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
}
})
}

View File

@@ -15,10 +15,10 @@
package bp2build
import (
"testing"
"android/soong/android"
"android/soong/sh"
"strings"
"testing"
)
func TestShBinaryLoadStatement(t *testing.T) {
@@ -46,28 +46,19 @@ func TestShBinaryLoadStatement(t *testing.T) {
t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
}
}
}
func TestShBinaryBp2Build(t *testing.T) {
testCases := []struct {
description string
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
preArchMutators []android.RegisterMutatorFunc
depsMutators []android.RegisterMutatorFunc
bp string
expectedBazelTargets []string
filesystem map[string]string
dir string
}{
{
func runShBinaryTestCase(t *testing.T, tc bp2buildTestCase) {
runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
}
func TestShBinarySimple(t *testing.T) {
runShBinaryTestCase(t, bp2buildTestCase{
description: "sh_binary test",
moduleTypeUnderTest: "sh_binary",
moduleTypeUnderTestFactory: sh.ShBinaryFactory,
moduleTypeUnderTestBp2BuildMutator: sh.ShBinaryBp2Build,
bp: `sh_binary {
blueprint: `sh_binary {
name: "foo",
src: "foo.sh",
bazel_module: { bp2build_available: true },
@@ -76,58 +67,5 @@ func TestShBinaryBp2Build(t *testing.T) {
name = "foo",
srcs = ["foo.sh"],
)`},
},
}
dir := "."
for _, testCase := range testCases {
filesystem := make(map[string][]byte)
toParse := []string{
"Android.bp",
}
for f, content := range testCase.filesystem {
if strings.HasSuffix(f, "Android.bp") {
toParse = append(toParse, f)
}
filesystem[f] = []byte(content)
}
config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
ctx := android.NewTestContext(config)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
for _, m := range testCase.depsMutators {
ctx.DepsBp2BuildMutators(m)
}
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse)
if errored(t, testCase.description, errs) {
continue
}
_, errs = ctx.ResolveDependencies(config)
if errored(t, testCase.description, errs) {
continue
}
checkDir := dir
if testCase.dir != "" {
checkDir = testCase.dir
}
codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
} else {
for i, target := range bazelTargets {
if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
t.Errorf(
"%s: Expected generated Bazel target to be '%s', got '%s'",
testCase.description,
w,
g,
)
}
}
}
}
})
}