Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c1cc3b96 authored by Lukacs T. Berki's avatar Lukacs T. Berki
Browse files

Split each test case to a different test function.

This is so that they can be individually debugged.

Test: Presubmits.
Change-Id: I7d929c4126bba7470aaa1c0def85bad65429ffdc
parent 54c98f5b
Loading
Loading
Loading
Loading
+246 −224
Original line number Diff line number Diff line
@@ -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,
					)
				}
			}
		}
	}
	})
}
+83 −126
Original line number Diff line number Diff line
@@ -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 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 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 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,
					)
				}
			}
		}
	}
	})
}
+371 −377

File changed.

Preview size limit exceeded, changes collapsed.

+111 −200
Original line number Diff line number Diff line
@@ -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,
					)
				}
			}
		}
	}
	})
}
+46 −94

File changed.

Preview size limit exceeded, changes collapsed.

Loading