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

@@ -15,35 +15,34 @@
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
}{
{
description: "simple cc_object generates cc_object with include header dep",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{
"a/b/foo.h": "",
"a/b/bar.h": "",
"a/b/exclude.c": "",
"a/b/c.c": "",
},
blueprint: `cc_object {
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,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{
"a/b/foo.h": "",
"a/b/bar.h": "",
"a/b/exclude.c": "",
"a/b/c.c": "",
},
blueprint: `cc_object {
name: "foo",
local_include_dirs: ["include"],
cflags: [
@@ -57,7 +56,7 @@ func TestCcObjectBp2Build(t *testing.T) {
exclude_srcs: ["a/b/exclude.c"],
}
`,
expectedBazelTargets: []string{`cc_object(
expectedBazelTargets: []string{`cc_object(
name = "foo",
copts = [
"-fno-addrsig",
@@ -71,14 +70,17 @@ func TestCcObjectBp2Build(t *testing.T) {
],
srcs = ["a/b/c.c"],
)`,
},
},
{
description: "simple cc_object with defaults",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
})
}
func TestCcObjectDefaults(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "simple cc_object with defaults",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
local_include_dirs: ["include"],
srcs: [
@@ -103,7 +105,7 @@ cc_defaults {
],
}
`,
expectedBazelTargets: []string{`cc_object(
expectedBazelTargets: []string{`cc_object(
name = "foo",
copts = [
"-Wno-gcc-compat",
@@ -117,18 +119,20 @@ 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,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{
"a/b/c.c": "",
"x/y/z.c": "",
},
{
description: "cc_object with cc_object deps in objs props",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{
"a/b/c.c": "",
"x/y/z.c": "",
},
blueprint: `cc_object {
blueprint: `cc_object {
name: "foo",
srcs: ["a/b/c.c"],
objs: ["bar"],
@@ -139,7 +143,7 @@ cc_object {
srcs: ["x/y/z.c"],
}
`,
expectedBazelTargets: []string{`cc_object(
expectedBazelTargets: []string{`cc_object(
name = "bar",
copts = [
"-fno-addrsig",
@@ -157,36 +161,42 @@ cc_object {
deps = [":bar"],
srcs = ["a/b/c.c"],
)`,
},
},
{
description: "cc_object with include_build_dir: false",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{
"a/b/c.c": "",
"x/y/z.c": "",
},
blueprint: `cc_object {
})
}
func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with include_build_dir: false",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{
"a/b/c.c": "",
"x/y/z.c": "",
},
blueprint: `cc_object {
name: "foo",
srcs: ["a/b/c.c"],
include_build_directory: false,
}
`,
expectedBazelTargets: []string{`cc_object(
expectedBazelTargets: []string{`cc_object(
name = "foo",
copts = ["-fno-addrsig"],
srcs = ["a/b/c.c"],
)`,
},
},
{
description: "cc_object with product variable",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
})
}
func TestCcObjectProductVariable(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object with product variable",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
include_build_directory: false,
product_variables: {
@@ -196,82 +206,22 @@ cc_object {
},
}
`,
expectedBazelTargets: []string{`cc_object(
expectedBazelTargets: []string{`cc_object(
name = "foo",
asflags = ["-DPLATFORM_SDK_VERSION={Platform_sdk_version}"],
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
}{
{
description: "cc_object setting cflags for one arch",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
func TestCcObjectCflagsOneArch(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for one arch",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
srcs: ["a.cpp"],
arch: {
@@ -284,8 +234,8 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
},
}
`,
expectedBazelTargets: []string{
`cc_object(
expectedBazelTargets: []string{
`cc_object(
name = "foo",
copts = [
"-fno-addrsig",
@@ -300,14 +250,17 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
"//conditions:default": [],
}),
)`,
},
},
{
description: "cc_object setting cflags for 4 architectures",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
})
}
func TestCcObjectCflagsFourArch(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for 4 architectures",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
srcs: ["base.cpp"],
arch: {
@@ -330,8 +283,8 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
},
}
`,
expectedBazelTargets: []string{
`cc_object(
expectedBazelTargets: []string{
`cc_object(
name = "foo",
copts = [
"-fno-addrsig",
@@ -352,14 +305,17 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
"//conditions:default": [],
}),
)`,
},
},
{
description: "cc_object setting cflags for multiple OSes",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
})
}
func TestCcObjectCflagsMultiOs(t *testing.T) {
runCcObjectTestCase(t, bp2buildTestCase{
description: "cc_object setting cflags for multiple OSes",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo",
srcs: ["base.cpp"],
target: {
@@ -375,8 +331,8 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
},
}
`,
expectedBazelTargets: []string{
`cc_object(
expectedBazelTargets: []string{
`cc_object(
name = "foo",
copts = [
"-fno-addrsig",
@@ -390,51 +346,6 @@ 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,
)
}
}
}
}
})
}