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,43 +40,98 @@ toolchain_library {
}` }`
) )
func TestCcLibraryBp2Build(t *testing.T) { func runCcLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
testCases := []struct { runBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
description string }
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) cc.RegisterCCBuildComponents(ctx)
bp string ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
expectedBazelTargets []string ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
filesystem map[string]string ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
dir string }
depsMutators []android.RegisterMutatorFunc
}{ func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
{ dir := "."
description: "cc_library - simple example", filesystem := make(map[string][]byte)
moduleTypeUnderTest: "cc_library", toParse := []string{
moduleTypeUnderTestFactory: cc.LibraryFactory, "Android.bp",
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, }
filesystem: map[string]string{ for f, content := range tc.filesystem {
"android.cpp": "", if strings.HasSuffix(f, "Android.bp") {
"darwin.cpp": "", toParse = append(toParse, f)
// Refer to cc.headerExts for the supported header extensions in Soong. }
"header.h": "", filesystem[f] = []byte(content)
"header.hh": "", }
"header.hpp": "", config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
"header.hxx": "", ctx := android.NewTestContext(config)
"header.h++": "",
"header.inl": "", registerModuleTypes(ctx)
"header.inc": "", ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
"header.ipp": "", ctx.RegisterBp2BuildConfig(bp2buildConfig)
"header.h.generic": "", for _, m := range tc.depsMutators {
"impl.cpp": "", ctx.DepsBp2BuildMutators(m)
"linux.cpp": "", }
"x86.cpp": "", ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
"x86_64.cpp": "", ctx.RegisterForBazelConversion()
"foo-dir/a.h": "",
}, _, errs := ctx.ParseFileList(dir, toParse)
bp: soongCcLibraryPreamble + ` 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,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
filesystem: map[string]string{
"android.cpp": "",
"darwin.cpp": "",
// Refer to cc.headerExts for the supported header extensions in Soong.
"header.h": "",
"header.hh": "",
"header.hpp": "",
"header.hxx": "",
"header.h++": "",
"header.inl": "",
"header.inc": "",
"header.ipp": "",
"header.h.generic": "",
"impl.cpp": "",
"linux.cpp": "",
"x86.cpp": "",
"x86_64.cpp": "",
"foo-dir/a.h": "",
},
blueprint: soongCcLibraryPreamble + `
cc_library_headers { name: "some-headers" } cc_library_headers { name: "some-headers" }
cc_library { cc_library {
name: "foo-lib", name: "foo-lib",
@@ -108,7 +163,7 @@ cc_library {
}, },
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "foo-lib", name = "foo-lib",
copts = [ copts = [
"-Wall", "-Wall",
@@ -132,21 +187,23 @@ cc_library {
"//build/bazel/platforms/os:linux": ["linux.cpp"], "//build/bazel/platforms/os:linux": ["linux.cpp"],
"//conditions:default": [], "//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,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
filesystem: map[string]string{
"ld-android.cpp": "",
"linked_list.h": "",
"linker.h": "",
"linker_block_allocator.h": "",
"linker_cfi.h": "",
}, },
{ blueprint: soongCcLibraryPreamble + `
description: "cc_library - trimmed example of //bionic/linker:ld-android",
moduleTypeUnderTest: "cc_library",
moduleTypeUnderTestFactory: cc.LibraryFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
filesystem: map[string]string{
"ld-android.cpp": "",
"linked_list.h": "",
"linker.h": "",
"linker_block_allocator.h": "",
"linker_cfi.h": "",
},
bp: soongCcLibraryPreamble + `
cc_library_headers { name: "libc_headers" } cc_library_headers { name: "libc_headers" }
cc_library { cc_library {
name: "fake-ld-android", name: "fake-ld-android",
@@ -176,7 +233,7 @@ cc_library {
}, },
} }
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "fake-ld-android", name = "fake-ld-android",
copts = [ copts = [
"-Wall", "-Wall",
@@ -201,20 +258,23 @@ cc_library {
}), }),
srcs = ["ld_android.cpp"], srcs = ["ld_android.cpp"],
)`}, )`},
}, })
{ }
description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
moduleTypeUnderTest: "cc_library", func TestCcLibraryExcludeSrcs(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
dir: "external", moduleTypeUnderTest: "cc_library",
filesystem: map[string]string{ moduleTypeUnderTestFactory: cc.LibraryFactory,
"external/math/cosf.c": "", moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"external/math/erf.c": "", dir: "external",
"external/math/erf_data.c": "", filesystem: map[string]string{
"external/math/erff.c": "", "external/math/cosf.c": "",
"external/math/erff_data.c": "", "external/math/erf.c": "",
"external/Android.bp": ` "external/math/erf_data.c": "",
"external/math/erff.c": "",
"external/math/erff_data.c": "",
"external/Android.bp": `
cc_library { cc_library {
name: "fake-libarm-optimized-routines-math", name: "fake-libarm-optimized-routines-math",
exclude_srcs: [ exclude_srcs: [
@@ -240,9 +300,9 @@ cc_library {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "fake-libarm-optimized-routines-math", name = "fake-libarm-optimized-routines-math",
copts = [ copts = [
"-Iexternal", "-Iexternal",
@@ -253,19 +313,22 @@ cc_library {
}), }),
srcs = ["math/cosf.c"], srcs = ["math/cosf.c"],
)`}, )`},
}, })
{ }
description: "cc_library shared/static props",
moduleTypeUnderTest: "cc_library", func TestCcLibrarySharedStaticProps(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library shared/static props",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library",
dir: "foo/bar", moduleTypeUnderTestFactory: cc.LibraryFactory,
filesystem: map[string]string{ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"foo/bar/both.cpp": "", depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
"foo/bar/sharedonly.cpp": "", dir: "foo/bar",
"foo/bar/staticonly.cpp": "", filesystem: map[string]string{
"foo/bar/Android.bp": ` "foo/bar/both.cpp": "",
"foo/bar/sharedonly.cpp": "",
"foo/bar/staticonly.cpp": "",
"foo/bar/Android.bp": `
cc_library { cc_library {
name: "a", name: "a",
srcs: ["both.cpp"], srcs: ["both.cpp"],
@@ -308,9 +371,9 @@ cc_library { name: "shared_dep_for_static" }
cc_library { name: "shared_dep_for_both" } cc_library { name: "shared_dep_for_both" }
`, `,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = [ copts = [
"bothflag", "bothflag",
@@ -332,16 +395,19 @@ cc_library { name: "shared_dep_for_both" }
whole_archive_deps_for_shared = [":whole_static_lib_for_shared"], whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
whole_archive_deps_for_static = [":whole_static_lib_for_static"], whole_archive_deps_for_static = [":whole_static_lib_for_static"],
)`}, )`},
}, })
{ }
description: "cc_library non-configured version script",
moduleTypeUnderTest: "cc_library", func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library non-configured version script",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library",
dir: "foo/bar", moduleTypeUnderTestFactory: cc.LibraryFactory,
filesystem: map[string]string{ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"foo/bar/Android.bp": ` depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar",
filesystem: map[string]string{
"foo/bar/Android.bp": `
cc_library { cc_library {
name: "a", name: "a",
srcs: ["a.cpp"], srcs: ["a.cpp"],
@@ -349,9 +415,9 @@ cc_library {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = [ copts = [
"-Ifoo/bar", "-Ifoo/bar",
@@ -360,16 +426,19 @@ cc_library {
srcs = ["a.cpp"], srcs = ["a.cpp"],
version_script = "v.map", version_script = "v.map",
)`}, )`},
}, })
{ }
description: "cc_library configured version script",
moduleTypeUnderTest: "cc_library", func TestCcLibraryConfiguredVersionScript(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library configured version script",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library",
dir: "foo/bar", moduleTypeUnderTestFactory: cc.LibraryFactory,
filesystem: map[string]string{ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"foo/bar/Android.bp": ` depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar",
filesystem: map[string]string{
"foo/bar/Android.bp": `
cc_library { cc_library {
name: "a", name: "a",
srcs: ["a.cpp"], srcs: ["a.cpp"],
@@ -385,9 +454,9 @@ cc_library {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = [ copts = [
"-Ifoo/bar", "-Ifoo/bar",
@@ -400,16 +469,19 @@ cc_library {
"//conditions:default": None, "//conditions:default": None,
}), }),
)`}, )`},
}, })
{ }
description: "cc_library shared_libs",
moduleTypeUnderTest: "cc_library", func TestCcLibrarySharedLibs(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library shared_libs",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library",
dir: "foo/bar", moduleTypeUnderTestFactory: cc.LibraryFactory,
filesystem: map[string]string{ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"foo/bar/Android.bp": ` depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar",
filesystem: map[string]string{
"foo/bar/Android.bp": `
cc_library { cc_library {
name: "mylib", name: "mylib",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
@@ -421,9 +493,9 @@ cc_library {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = [ copts = [
"-Ifoo/bar", "-Ifoo/bar",
@@ -437,16 +509,19 @@ cc_library {
"-I$(BINDIR)/foo/bar", "-I$(BINDIR)/foo/bar",
], ],
)`}, )`},
}, })
{ }
description: "cc_library pack_relocations test",
moduleTypeUnderTest: "cc_library", func TestCcLibraryPackRelocations(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library pack_relocations test",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library",
dir: "foo/bar", moduleTypeUnderTestFactory: cc.LibraryFactory,
filesystem: map[string]string{ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"foo/bar/Android.bp": ` depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar",
filesystem: map[string]string{
"foo/bar/Android.bp": `
cc_library { cc_library {
name: "a", name: "a",
srcs: ["a.cpp"], srcs: ["a.cpp"],
@@ -475,9 +550,9 @@ cc_library {
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = [ copts = [
"-Ifoo/bar", "-Ifoo/bar",
@@ -508,25 +583,28 @@ cc_library {
}), }),
srcs = ["c.cpp"], srcs = ["c.cpp"],
)`}, )`},
}, })
{ }
description: "cc_library spaces in copts",
moduleTypeUnderTest: "cc_library", func TestCcLibrarySpacesInCopts(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library spaces in copts",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library",
dir: "foo/bar", moduleTypeUnderTestFactory: cc.LibraryFactory,
filesystem: map[string]string{ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"foo/bar/Android.bp": ` depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar",
filesystem: map[string]string{
"foo/bar/Android.bp": `
cc_library { cc_library {
name: "a", name: "a",
cflags: ["-include header.h",], cflags: ["-include header.h",],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = [ copts = [
"-include", "-include",
@@ -535,16 +613,19 @@ cc_library {
"-I$(BINDIR)/foo/bar", "-I$(BINDIR)/foo/bar",
], ],
)`}, )`},
}, })
{ }
description: "cc_library cppflags goes into copts",
moduleTypeUnderTest: "cc_library", func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryFactory, runCcLibraryTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, description: "cc_library cppflags goes into copts",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library",
dir: "foo/bar", moduleTypeUnderTestFactory: cc.LibraryFactory,
filesystem: map[string]string{ moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
"foo/bar/Android.bp": `cc_library { depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
dir: "foo/bar",
filesystem: map[string]string{
"foo/bar/Android.bp": `cc_library {
name: "a", name: "a",
srcs: ["a.cpp"], srcs: ["a.cpp"],
cflags: [ cflags: [
@@ -567,9 +648,9 @@ cc_library {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
}, },
bp: soongCcLibraryPreamble, blueprint: soongCcLibraryPreamble,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "a", name = "a",
copts = [ copts = [
"-Wall", "-Wall",
@@ -586,64 +667,5 @@ cc_library {
}), }),
srcs = ["a.cpp"], 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 package bp2build
import ( import (
"testing"
"android/soong/android" "android/soong/android"
"android/soong/cc" "android/soong/cc"
"strings"
"testing"
) )
const ( 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) { func TestCcLibraryHeadersLoadStatement(t *testing.T) {
testCases := []struct { testCases := []struct {
bazelTargets BazelTargets bazelTargets BazelTargets
@@ -64,41 +76,37 @@ func TestCcLibraryHeadersLoadStatement(t *testing.T) {
t.Fatalf("Expected load statements to be %s, got %s", expected, actual) t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
} }
} }
} }
func TestCcLibraryHeadersBp2Build(t *testing.T) { func registerCcLibraryHeadersModuleTypes(ctx android.RegistrationContext) {
testCases := []struct { cc.RegisterCCBuildComponents(ctx)
description string ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
moduleTypeUnderTest string }
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) func runCcLibraryHeadersTestCase(t *testing.T, tc bp2buildTestCase) {
preArchMutators []android.RegisterMutatorFunc runBp2BuildTestCase(t, registerCcLibraryHeadersModuleTypes, tc)
depsMutators []android.RegisterMutatorFunc }
bp string
expectedBazelTargets []string func TestCcLibraryHeadersSimple(t *testing.T) {
filesystem map[string]string runCcLibraryHeadersTestCase(t, bp2buildTestCase{
dir string description: "cc_library_headers test",
}{ moduleTypeUnderTest: "cc_library_headers",
{ moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
description: "cc_library_headers test", moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
moduleTypeUnderTest: "cc_library_headers", filesystem: map[string]string{
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory, "lib-1/lib1a.h": "",
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build, "lib-1/lib1b.h": "",
filesystem: map[string]string{ "lib-2/lib2a.h": "",
"lib-1/lib1a.h": "", "lib-2/lib2b.h": "",
"lib-1/lib1b.h": "", "dir-1/dir1a.h": "",
"lib-2/lib2a.h": "", "dir-1/dir1b.h": "",
"lib-2/lib2b.h": "", "dir-2/dir2a.h": "",
"dir-1/dir1a.h": "", "dir-2/dir2b.h": "",
"dir-1/dir1b.h": "", "arch_arm64_exported_include_dir/a.h": "",
"dir-2/dir2a.h": "", "arch_x86_exported_include_dir/b.h": "",
"dir-2/dir2b.h": "", "arch_x86_64_exported_include_dir/c.h": "",
"arch_arm64_exported_include_dir/a.h": "", },
"arch_x86_exported_include_dir/b.h": "", blueprint: soongCcLibraryHeadersPreamble + `
"arch_x86_64_exported_include_dir/c.h": "",
},
bp: soongCcLibraryHeadersPreamble + `
cc_library_headers { cc_library_headers {
name: "lib-1", name: "lib-1",
export_include_dirs: ["lib-1"], export_include_dirs: ["lib-1"],
@@ -129,7 +137,7 @@ cc_library_headers {
// TODO: Also support export_header_lib_headers // TODO: Also support export_header_lib_headers
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{`cc_library_headers(
name = "foo_headers", name = "foo_headers",
copts = [ copts = [
"-I.", "-I.",
@@ -163,15 +171,18 @@ cc_library_headers {
], ],
includes = ["lib-2"], includes = ["lib-2"],
)`}, )`},
}, })
{ }
description: "cc_library_headers test with os-specific header_libs props",
moduleTypeUnderTest: "cc_library_headers", func TestCcLibraryHeadersOSSpecificHeader(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory, runCcLibraryHeadersTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build, description: "cc_library_headers test with os-specific header_libs props",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library_headers",
filesystem: map[string]string{}, moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
bp: soongCcLibraryPreamble + ` moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
blueprint: soongCcLibraryPreamble + `
cc_library_headers { name: "android-lib" } cc_library_headers { name: "android-lib" }
cc_library_headers { name: "base-lib" } cc_library_headers { name: "base-lib" }
cc_library_headers { name: "darwin-lib" } cc_library_headers { name: "darwin-lib" }
@@ -192,7 +203,7 @@ cc_library_headers {
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{`cc_library_headers(
name = "android-lib", name = "android-lib",
copts = [ copts = [
"-I.", "-I.",
@@ -250,15 +261,18 @@ cc_library_headers {
"-I$(BINDIR)/.", "-I$(BINDIR)/.",
], ],
)`}, )`},
}, })
{ }
description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
moduleTypeUnderTest: "cc_library_headers", func TestCcLibraryHeadersOsSpecficHeaderLibsExportHeaderLibHeaders(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory, runCcLibraryHeadersTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build, description: "cc_library_headers test with os-specific header_libs and export_header_lib_headers props",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library_headers",
filesystem: map[string]string{}, moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
bp: soongCcLibraryPreamble + ` moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
blueprint: soongCcLibraryPreamble + `
cc_library_headers { name: "android-lib" } cc_library_headers { name: "android-lib" }
cc_library_headers { name: "exported-lib" } cc_library_headers { name: "exported-lib" }
cc_library_headers { cc_library_headers {
@@ -267,7 +281,7 @@ cc_library_headers {
android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] }, android: { header_libs: ["android-lib"], export_header_lib_headers: ["exported-lib"] },
}, },
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{`cc_library_headers(
name = "android-lib", name = "android-lib",
copts = [ copts = [
"-I.", "-I.",
@@ -294,15 +308,18 @@ cc_library_headers {
"//conditions:default": [], "//conditions:default": [],
}), }),
)`}, )`},
}, })
{ }
description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
moduleTypeUnderTest: "cc_library_headers", func TestCcLibraryHeadersArchAndTargetExportSystemIncludes(t *testing.T) {
moduleTypeUnderTestFactory: cc.LibraryHeaderFactory, runCcLibraryHeadersTestCase(t, bp2buildTestCase{
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build, description: "cc_library_headers test with arch-specific and target-specific export_system_include_dirs props",
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, moduleTypeUnderTest: "cc_library_headers",
filesystem: map[string]string{}, moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
bp: soongCcLibraryPreamble + `cc_library_headers { moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
blueprint: soongCcLibraryPreamble + `cc_library_headers {
name: "foo_headers", name: "foo_headers",
export_system_include_dirs: [ export_system_include_dirs: [
"shared_include_dir", "shared_include_dir",
@@ -337,7 +354,7 @@ cc_library_headers {
}, },
}, },
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{`cc_library_headers(
name = "foo_headers", name = "foo_headers",
copts = [ copts = [
"-I.", "-I.",
@@ -354,65 +371,5 @@ cc_library_headers {
"//conditions:default": [], "//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,
)
}
}
}
}
} }

File diff suppressed because it is too large Load Diff

View File

@@ -15,35 +15,34 @@
package bp2build package bp2build
import ( import (
"testing"
"android/soong/android" "android/soong/android"
"android/soong/cc" "android/soong/cc"
"fmt"
"strings"
"testing"
) )
func TestCcObjectBp2Build(t *testing.T) { func registerCcObjectModuleTypes(ctx android.RegistrationContext) {
testCases := []struct { // Always register cc_defaults module factory
description string ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
moduleTypeUnderTest string }
moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) func runCcObjectTestCase(t *testing.T, tc bp2buildTestCase) {
blueprint string runBp2BuildTestCase(t, registerCcObjectModuleTypes, tc)
expectedBazelTargets []string }
filesystem map[string]string
}{ func TestCcObjectSimple(t *testing.T) {
{ runCcObjectTestCase(t, bp2buildTestCase{
description: "simple cc_object generates cc_object with include header dep", description: "simple cc_object generates cc_object with include header dep",
moduleTypeUnderTest: "cc_object", moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory, moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
filesystem: map[string]string{ filesystem: map[string]string{
"a/b/foo.h": "", "a/b/foo.h": "",
"a/b/bar.h": "", "a/b/bar.h": "",
"a/b/exclude.c": "", "a/b/exclude.c": "",
"a/b/c.c": "", "a/b/c.c": "",
}, },
blueprint: `cc_object { blueprint: `cc_object {
name: "foo", name: "foo",
local_include_dirs: ["include"], local_include_dirs: ["include"],
cflags: [ cflags: [
@@ -57,7 +56,7 @@ func TestCcObjectBp2Build(t *testing.T) {
exclude_srcs: ["a/b/exclude.c"], exclude_srcs: ["a/b/exclude.c"],
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
name = "foo", name = "foo",
copts = [ copts = [
"-fno-addrsig", "-fno-addrsig",
@@ -71,14 +70,17 @@ func TestCcObjectBp2Build(t *testing.T) {
], ],
srcs = ["a/b/c.c"], srcs = ["a/b/c.c"],
)`, )`,
},
}, },
{ })
description: "simple cc_object with defaults", }
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory, func TestCcObjectDefaults(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, runCcObjectTestCase(t, bp2buildTestCase{
blueprint: `cc_object { description: "simple cc_object with defaults",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo", name: "foo",
local_include_dirs: ["include"], local_include_dirs: ["include"],
srcs: [ srcs: [
@@ -103,7 +105,7 @@ cc_defaults {
], ],
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
name = "foo", name = "foo",
copts = [ copts = [
"-Wno-gcc-compat", "-Wno-gcc-compat",
@@ -117,18 +119,20 @@ cc_defaults {
], ],
srcs = ["a/b/c.c"], 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": "",
}, },
{ blueprint: `cc_object {
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 {
name: "foo", name: "foo",
srcs: ["a/b/c.c"], srcs: ["a/b/c.c"],
objs: ["bar"], objs: ["bar"],
@@ -139,7 +143,7 @@ cc_object {
srcs: ["x/y/z.c"], srcs: ["x/y/z.c"],
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
name = "bar", name = "bar",
copts = [ copts = [
"-fno-addrsig", "-fno-addrsig",
@@ -157,36 +161,42 @@ cc_object {
deps = [":bar"], deps = [":bar"],
srcs = ["a/b/c.c"], srcs = ["a/b/c.c"],
)`, )`,
},
}, },
{ })
description: "cc_object with include_build_dir: false", }
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory, func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, runCcObjectTestCase(t, bp2buildTestCase{
filesystem: map[string]string{ description: "cc_object with include_build_dir: false",
"a/b/c.c": "", moduleTypeUnderTest: "cc_object",
"x/y/z.c": "", moduleTypeUnderTestFactory: cc.ObjectFactory,
}, moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object { filesystem: map[string]string{
"a/b/c.c": "",
"x/y/z.c": "",
},
blueprint: `cc_object {
name: "foo", name: "foo",
srcs: ["a/b/c.c"], srcs: ["a/b/c.c"],
include_build_directory: false, include_build_directory: false,
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
name = "foo", name = "foo",
copts = ["-fno-addrsig"], copts = ["-fno-addrsig"],
srcs = ["a/b/c.c"], srcs = ["a/b/c.c"],
)`, )`,
},
}, },
{ })
description: "cc_object with product variable", }
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory, func TestCcObjectProductVariable(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, runCcObjectTestCase(t, bp2buildTestCase{
blueprint: `cc_object { description: "cc_object with product variable",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo", name: "foo",
include_build_directory: false, include_build_directory: false,
product_variables: { product_variables: {
@@ -196,82 +206,22 @@ cc_object {
}, },
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
name = "foo", name = "foo",
asflags = ["-DPLATFORM_SDK_VERSION={Platform_sdk_version}"], asflags = ["-DPLATFORM_SDK_VERSION={Platform_sdk_version}"],
copts = ["-fno-addrsig"], 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) { func TestCcObjectCflagsOneArch(t *testing.T) {
testCases := []struct { runCcObjectTestCase(t, bp2buildTestCase{
description string description: "cc_object setting cflags for one arch",
moduleTypeUnderTest string moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory android.ModuleFactory moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint string blueprint: `cc_object {
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 {
name: "foo", name: "foo",
srcs: ["a.cpp"], srcs: ["a.cpp"],
arch: { arch: {
@@ -284,8 +234,8 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
}, },
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( `cc_object(
name = "foo", name = "foo",
copts = [ copts = [
"-fno-addrsig", "-fno-addrsig",
@@ -300,14 +250,17 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
"//conditions:default": [], "//conditions:default": [],
}), }),
)`, )`,
},
}, },
{ })
description: "cc_object setting cflags for 4 architectures", }
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory, func TestCcObjectCflagsFourArch(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, runCcObjectTestCase(t, bp2buildTestCase{
blueprint: `cc_object { description: "cc_object setting cflags for 4 architectures",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo", name: "foo",
srcs: ["base.cpp"], srcs: ["base.cpp"],
arch: { arch: {
@@ -330,8 +283,8 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
}, },
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( `cc_object(
name = "foo", name = "foo",
copts = [ copts = [
"-fno-addrsig", "-fno-addrsig",
@@ -352,14 +305,17 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
"//conditions:default": [], "//conditions:default": [],
}), }),
)`, )`,
},
}, },
{ })
description: "cc_object setting cflags for multiple OSes", }
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory, func TestCcObjectCflagsMultiOs(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build, runCcObjectTestCase(t, bp2buildTestCase{
blueprint: `cc_object { description: "cc_object setting cflags for multiple OSes",
moduleTypeUnderTest: "cc_object",
moduleTypeUnderTestFactory: cc.ObjectFactory,
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
blueprint: `cc_object {
name: "foo", name: "foo",
srcs: ["base.cpp"], srcs: ["base.cpp"],
target: { target: {
@@ -375,8 +331,8 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
}, },
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( `cc_object(
name = "foo", name = "foo",
copts = [ copts = [
"-fno-addrsig", "-fno-addrsig",
@@ -390,51 +346,6 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
}), }),
srcs = ["base.cpp"], 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,36 +1,30 @@
package bp2build package bp2build
import ( import (
"testing"
"android/soong/android" "android/soong/android"
"android/soong/python" "android/soong/python"
"fmt"
"strings"
"testing"
) )
func TestPythonBinaryHost(t *testing.T) { func runPythonTestCase(t *testing.T, tc bp2buildTestCase) {
testCases := []struct { runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
description string }
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory func TestPythonBinaryHostSimple(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) runPythonTestCase(t, bp2buildTestCase{
blueprint string description: "simple python_binary_host converts to a native py_binary",
expectedBazelTargets []string moduleTypeUnderTest: "python_binary_host",
filesystem map[string]string moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
}{ moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
{ filesystem: map[string]string{
description: "simple python_binary_host converts to a native py_binary", "a.py": "",
moduleTypeUnderTest: "python_binary_host", "b/c.py": "",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, "b/d.py": "",
moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build, "b/e.py": "",
filesystem: map[string]string{ "files/data.txt": "",
"a.py": "", },
"b/c.py": "", blueprint: `python_binary_host {
"b/d.py": "",
"b/e.py": "",
"files/data.txt": "",
},
blueprint: `python_binary_host {
name: "foo", name: "foo",
main: "a.py", main: "a.py",
srcs: ["**/*.py"], srcs: ["**/*.py"],
@@ -39,7 +33,7 @@ func TestPythonBinaryHost(t *testing.T) {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`py_binary( expectedBazelTargets: []string{`py_binary(
name = "foo", name = "foo",
data = ["files/data.txt"], data = ["files/data.txt"],
main = "a.py", main = "a.py",
@@ -49,14 +43,17 @@ func TestPythonBinaryHost(t *testing.T) {
"b/d.py", "b/d.py",
], ],
)`, )`,
},
}, },
{ })
description: "py2 python_binary_host", }
moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, func TestPythonBinaryHostPy2(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build, runPythonTestCase(t, bp2buildTestCase{
blueprint: `python_binary_host { description: "py2 python_binary_host",
moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
blueprint: `python_binary_host {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -71,19 +68,22 @@ func TestPythonBinaryHost(t *testing.T) {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`py_binary( expectedBazelTargets: []string{`py_binary(
name = "foo", name = "foo",
python_version = "PY2", python_version = "PY2",
srcs = ["a.py"], srcs = ["a.py"],
)`, )`,
},
}, },
{ })
description: "py3 python_binary_host", }
moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory, func TestPythonBinaryHostPy3(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build, runPythonTestCase(t, bp2buildTestCase{
blueprint: `python_binary_host { description: "py3 python_binary_host",
moduleTypeUnderTest: "python_binary_host",
moduleTypeUnderTestFactory: python.PythonBinaryHostFactory,
moduleTypeUnderTestBp2BuildMutator: python.PythonBinaryBp2Build,
blueprint: `python_binary_host {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -98,60 +98,12 @@ func TestPythonBinaryHost(t *testing.T) {
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
// python_version is PY3 by default. // python_version is PY3 by default.
`py_binary( `py_binary(
name = "foo", name = "foo",
srcs = ["a.py"], 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 package bp2build
import ( import (
"testing"
"android/soong/android" "android/soong/android"
"android/soong/sh" "android/soong/sh"
"strings"
"testing"
) )
func TestShBinaryLoadStatement(t *testing.T) { func TestShBinaryLoadStatement(t *testing.T) {
@@ -46,88 +46,26 @@ func TestShBinaryLoadStatement(t *testing.T) {
t.Fatalf("Expected load statements to be %s, got %s", expected, actual) t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
} }
} }
} }
func TestShBinaryBp2Build(t *testing.T) { func runShBinaryTestCase(t *testing.T, tc bp2buildTestCase) {
testCases := []struct { runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
description string }
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory func TestShBinarySimple(t *testing.T) {
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) runShBinaryTestCase(t, bp2buildTestCase{
preArchMutators []android.RegisterMutatorFunc description: "sh_binary test",
depsMutators []android.RegisterMutatorFunc moduleTypeUnderTest: "sh_binary",
bp string moduleTypeUnderTestFactory: sh.ShBinaryFactory,
expectedBazelTargets []string moduleTypeUnderTestBp2BuildMutator: sh.ShBinaryBp2Build,
filesystem map[string]string blueprint: `sh_binary {
dir string
}{
{
description: "sh_binary test",
moduleTypeUnderTest: "sh_binary",
moduleTypeUnderTestFactory: sh.ShBinaryFactory,
moduleTypeUnderTestBp2BuildMutator: sh.ShBinaryBp2Build,
bp: `sh_binary {
name: "foo", name: "foo",
src: "foo.sh", src: "foo.sh",
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`sh_binary( expectedBazelTargets: []string{`sh_binary(
name = "foo", name = "foo",
srcs = ["foo.sh"], 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,
)
}
}
}
}
} }