Convert android/singleton_module_test.go to test fixtures

Bug: 182885307
Test: m nothing
Change-Id: I7f9f3eb66279fe5a2447aefa9b636144c32ce92a
This commit is contained in:
Paul Duffin
2021-03-16 23:36:24 +00:00
parent 4bb2b219d6
commit d65970072d

View File

@@ -15,8 +15,6 @@
package android package android
import ( import (
"reflect"
"strings"
"testing" "testing"
) )
@@ -43,23 +41,14 @@ func testSingletonModuleFactory() SingletonModule {
return tsm return tsm
} }
func runSingletonModuleTest(bp string) (*TestContext, []error) { var prepareForSingletonModuleTest = GroupFixturePreparers(
config := TestConfig(buildDir, nil, bp, nil)
// Enable Kati output to test SingletonModules with MakeVars. // Enable Kati output to test SingletonModules with MakeVars.
config.katiEnabled = true PrepareForTestWithAndroidMk,
ctx := NewTestContext(config) FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory) ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory)
ctx.RegisterSingletonType("makevars", makeVarsSingletonFunc) ctx.RegisterSingletonType("makevars", makeVarsSingletonFunc)
ctx.Register() }),
)
_, errs := ctx.ParseBlueprintsFiles("Android.bp")
if len(errs) > 0 {
return ctx, errs
}
_, errs = ctx.PrepareBuildActions(config)
return ctx, errs
}
func TestSingletonModule(t *testing.T) { func TestSingletonModule(t *testing.T) {
bp := ` bp := `
@@ -67,16 +56,15 @@ func TestSingletonModule(t *testing.T) {
name: "test_singleton_module", name: "test_singleton_module",
} }
` `
ctx, errs := runSingletonModuleTest(bp) result := emptyTestFixtureFactory.
if len(errs) > 0 { RunTest(t,
t.Fatal(errs) prepareForSingletonModuleTest,
} FixtureWithRootAndroidBp(bp),
)
ops := ctx.ModuleForTests("test_singleton_module", "").Module().(*testSingletonModule).ops ops := result.ModuleForTests("test_singleton_module", "").Module().(*testSingletonModule).ops
wantOps := []string{"GenerateAndroidBuildActions", "GenerateSingletonBuildActions", "MakeVars"} wantOps := []string{"GenerateAndroidBuildActions", "GenerateSingletonBuildActions", "MakeVars"}
if !reflect.DeepEqual(ops, wantOps) { AssertDeepEquals(t, "operations", wantOps, ops)
t.Errorf("Expected operations %q, got %q", wantOps, ops)
}
} }
func TestDuplicateSingletonModule(t *testing.T) { func TestDuplicateSingletonModule(t *testing.T) {
@@ -89,23 +77,22 @@ func TestDuplicateSingletonModule(t *testing.T) {
name: "test_singleton_module2", name: "test_singleton_module2",
} }
` `
_, errs := runSingletonModuleTest(bp)
if len(errs) == 0 { emptyTestFixtureFactory.
t.Fatal("expected duplicate SingletonModule error") ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{
} `\QDuplicate SingletonModule "test_singleton_module", previously used in\E`,
if len(errs) != 1 || !strings.Contains(errs[0].Error(), `Duplicate SingletonModule "test_singleton_module", previously used in`) { })).RunTest(t,
t.Fatalf("expected duplicate SingletonModule error, got %q", errs) prepareForSingletonModuleTest,
} FixtureWithRootAndroidBp(bp),
)
} }
func TestUnusedSingletonModule(t *testing.T) { func TestUnusedSingletonModule(t *testing.T) {
bp := `` result := emptyTestFixtureFactory.RunTest(t,
ctx, errs := runSingletonModuleTest(bp) prepareForSingletonModuleTest,
if len(errs) > 0 { )
t.Fatal(errs)
}
singleton := ctx.SingletonForTests("test_singleton_module").Singleton() singleton := result.SingletonForTests("test_singleton_module").Singleton()
sm := singleton.(*singletonModuleSingletonAdaptor).sm sm := singleton.(*singletonModuleSingletonAdaptor).sm
ops := sm.(*testSingletonModule).ops ops := sm.(*testSingletonModule).ops
if ops != nil { if ops != nil {
@@ -126,24 +113,17 @@ func TestVariantSingletonModule(t *testing.T) {
} }
` `
config := TestConfig(buildDir, nil, bp, nil) emptyTestFixtureFactory.
ctx := NewTestContext(config) ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { `\QGenerateAndroidBuildActions already called for variant\E`,
ctx.BottomUp("test_singleton_module_mutator", testVariantSingletonModuleMutator) })).
}) RunTest(t,
ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory) prepareForSingletonModuleTest,
ctx.Register() FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
_, errs := ctx.ParseBlueprintsFiles("Android.bp") ctx.BottomUp("test_singleton_module_mutator", testVariantSingletonModuleMutator)
})
if len(errs) == 0 { }),
_, errs = ctx.PrepareBuildActions(config) FixtureWithRootAndroidBp(bp),
} )
if len(errs) == 0 {
t.Fatal("expected duplicate SingletonModule error")
}
if len(errs) != 1 || !strings.Contains(errs[0].Error(), `GenerateAndroidBuildActions already called for variant`) {
t.Fatalf("expected duplicate SingletonModule error, got %q", errs)
}
} }