Make bp2buildMutators registration local to TestContext.

The previous implementation relied on the implicit registration of Bp2Build mutators, resulting in test non-hermeticity. Refactor bp2build tests to explicitly specify the bp2build mutators under test.

Test: Soong tests

Test: TH
Change-Id: I9b9674bad1ea533b3bd31b07077a9e02c99b4c1d
This commit is contained in:
Jingwen Chen 2021-01-26 21:57:27 -05:00
parent 801a669566
commit a42d6417b3
4 changed files with 41 additions and 32 deletions

View File

@ -23,7 +23,7 @@ import (
func init() { func init() {
RegisterModuleType("filegroup", FileGroupFactory) RegisterModuleType("filegroup", FileGroupFactory)
RegisterBp2BuildMutator("filegroup", bp2buildMutator) RegisterBp2BuildMutator("filegroup", FilegroupBp2Build)
} }
// https://docs.bazel.build/versions/master/be/general.html#filegroup // https://docs.bazel.build/versions/master/be/general.html#filegroup
@ -51,7 +51,7 @@ func (bfg *bazelFilegroup) Name() string {
func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {} func (bfg *bazelFilegroup) GenerateAndroidBuildActions(ctx ModuleContext) {}
// TODO: Create helper functions to avoid this boilerplate. // TODO: Create helper functions to avoid this boilerplate.
func bp2buildMutator(ctx TopDownMutatorContext) { func FilegroupBp2Build(ctx TopDownMutatorContext) {
if m, ok := ctx.Module().(*fileGroup); ok { if m, ok := ctx.Module().(*fileGroup); ok {
name := "__bp2build__" + m.base().BaseModuleName() name := "__bp2build__" + m.base().BaseModuleName()
ctx.CreateModule(BazelFileGroupFactory, &bazelFilegroupAttributes{ ctx.CreateModule(BazelFileGroupFactory, &bazelFilegroupAttributes{

View File

@ -89,7 +89,7 @@ func (ctx *TestContext) RegisterBp2BuildMutator(moduleType string, m func(TopDow
f := func(ctx RegisterMutatorsContext) { f := func(ctx RegisterMutatorsContext) {
ctx.TopDown(mutatorName, m) ctx.TopDown(mutatorName, m)
} }
bp2buildMutators = append(bp2buildMutators, f) ctx.bp2buildMutators = append(ctx.bp2buildMutators, f)
} }
func (ctx *TestContext) Register() { func (ctx *TestContext) Register() {
@ -100,7 +100,7 @@ func (ctx *TestContext) Register() {
// RegisterForBazelConversion prepares a test context for bp2build conversion. // RegisterForBazelConversion prepares a test context for bp2build conversion.
func (ctx *TestContext) RegisterForBazelConversion() { func (ctx *TestContext) RegisterForBazelConversion() {
RegisterMutatorsForBazelConversion(ctx.Context.Context, bp2buildMutators) RegisterMutatorsForBazelConversion(ctx.Context.Context, ctx.bp2buildMutators)
} }
func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) { func (ctx *TestContext) ParseFileList(rootDir string, filePaths []string) (deps []string, errs []error) {

View File

@ -272,6 +272,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
testCases := []struct { testCases := []struct {
moduleTypeUnderTest string moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory moduleTypeUnderTestFactory android.ModuleFactory
moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
bp string bp string
expectedBazelTarget string expectedBazelTarget string
description string description string
@ -280,6 +281,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
description: "filegroup with no srcs", description: "filegroup with no srcs",
moduleTypeUnderTest: "filegroup", moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
bp: `filegroup { bp: `filegroup {
name: "foo", name: "foo",
srcs: [], srcs: [],
@ -294,6 +296,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
description: "filegroup with srcs", description: "filegroup with srcs",
moduleTypeUnderTest: "filegroup", moduleTypeUnderTest: "filegroup",
moduleTypeUnderTestFactory: android.FileGroupFactory, moduleTypeUnderTestFactory: android.FileGroupFactory,
moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
bp: `filegroup { bp: `filegroup {
name: "foo", name: "foo",
srcs: ["a", "b"], srcs: ["a", "b"],
@ -310,6 +313,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
description: "genrule with command line variable replacements", description: "genrule with command line variable replacements",
moduleTypeUnderTest: "genrule", moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory, moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
bp: `genrule { bp: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@ -335,6 +339,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
description: "genrule using $(locations :label)", description: "genrule using $(locations :label)",
moduleTypeUnderTest: "genrule", moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory, moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
bp: `genrule { bp: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@ -360,6 +365,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
description: "genrule using $(location) label should substitute first tool label automatically", description: "genrule using $(location) label should substitute first tool label automatically",
moduleTypeUnderTest: "genrule", moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory, moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
bp: `genrule { bp: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@ -386,6 +392,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
description: "genrule using $(locations) label should substitute first tool label automatically", description: "genrule using $(locations) label should substitute first tool label automatically",
moduleTypeUnderTest: "genrule", moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory, moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
bp: `genrule { bp: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@ -412,6 +419,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
description: "genrule without tools or tool_files can convert successfully", description: "genrule without tools or tool_files can convert successfully",
moduleTypeUnderTest: "genrule", moduleTypeUnderTest: "genrule",
moduleTypeUnderTestFactory: genrule.GenRuleFactory, moduleTypeUnderTestFactory: genrule.GenRuleFactory,
moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
bp: `genrule { bp: `genrule {
name: "foo", name: "foo",
out: ["foo.out"], out: ["foo.out"],
@ -436,6 +444,7 @@ func TestModuleTypeBp2Build(t *testing.T) {
config := android.TestConfig(buildDir, nil, testCase.bp, nil) config := android.TestConfig(buildDir, nil, testCase.bp, nil)
ctx := android.NewTestContext(config) ctx := android.NewTestContext(config)
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterForBazelConversion() ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})

View File

@ -47,7 +47,7 @@ func RegisterGenruleBuildComponents(ctx android.RegistrationContext) {
ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel() ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel()
}) })
android.RegisterBp2BuildMutator("genrule", bp2buildMutator) android.RegisterBp2BuildMutator("genrule", GenruleBp2Build)
} }
var ( var (
@ -794,7 +794,7 @@ func BazelGenruleFactory() android.Module {
return module return module
} }
func bp2buildMutator(ctx android.TopDownMutatorContext) { func GenruleBp2Build(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*Module); ok { if m, ok := ctx.Module().(*Module); ok {
name := "__bp2build__" + m.Name() name := "__bp2build__" + m.Name()
// Bazel only has the "tools" attribute. // Bazel only has the "tools" attribute.