Add arch mutator test

Add a test for the arch mutator in preparation for splitting it
into two mutators.

Test: m checkbuild
Change-Id: Ifd0a146a085901d33db50e783bdfad31edd7b7a6
This commit is contained in:
Colin Cross 2019-10-16 13:06:07 -07:00
parent 0f7d2ef3ac
commit 158eeb7701
1 changed files with 137 additions and 0 deletions

View File

@ -16,6 +16,7 @@ package android
import (
"reflect"
"runtime"
"testing"
"github.com/google/blueprint/proptools"
@ -232,3 +233,139 @@ func TestFilterArchStruct(t *testing.T) {
})
}
}
type archTestModule struct {
ModuleBase
props struct {
Deps []string
}
}
func (m *archTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
}
func (m *archTestModule) DepsMutator(ctx BottomUpMutatorContext) {
ctx.AddDependency(ctx.Module(), nil, m.props.Deps...)
}
func archTestModuleFactory() Module {
m := &archTestModule{}
m.AddProperties(&m.props)
InitAndroidArchModule(m, HostAndDeviceSupported, MultilibBoth)
return m
}
func TestArchMutator(t *testing.T) {
var buildOSVariants []string
var buildOS32Variants []string
switch runtime.GOOS {
case "linux":
buildOSVariants = []string{"linux_glibc_x86_64", "linux_glibc_x86"}
buildOS32Variants = []string{"linux_glibc_x86"}
case "darwin":
buildOSVariants = []string{"darwin_x86_64"}
buildOS32Variants = nil
}
bp := `
module {
name: "foo",
}
module {
name: "bar",
host_supported: true,
}
module {
name: "baz",
device_supported: false,
}
module {
name: "qux",
host_supported: true,
compile_multilib: "32",
}
`
mockFS := map[string][]byte{
"Android.bp": []byte(bp),
}
testCases := []struct {
name string
config func(Config)
fooVariants []string
barVariants []string
bazVariants []string
quxVariants []string
}{
{
name: "normal",
config: nil,
fooVariants: []string{"android_arm64_armv8-a", "android_arm_armv7-a-neon"},
barVariants: append(buildOSVariants, "android_arm64_armv8-a", "android_arm_armv7-a-neon"),
bazVariants: nil,
quxVariants: append(buildOS32Variants, "android_arm_armv7-a-neon"),
},
{
name: "host-only",
config: func(config Config) {
config.BuildOSTarget = Target{}
config.BuildOSCommonTarget = Target{}
config.Targets[Android] = nil
},
fooVariants: nil,
barVariants: buildOSVariants,
bazVariants: nil,
quxVariants: buildOS32Variants,
},
}
enabledVariants := func(ctx *TestContext, name string) []string {
var ret []string
variants := ctx.ModuleVariantsForTests(name)
for _, variant := range variants {
m := ctx.ModuleForTests(name, variant)
if m.Module().Enabled() {
ret = append(ret, variant)
}
}
return ret
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
ctx := NewTestArchContext()
ctx.RegisterModuleType("module", ModuleFactoryAdaptor(archTestModuleFactory))
ctx.MockFileSystem(mockFS)
ctx.Register()
config := TestArchConfig(buildDir, nil)
if tt.config != nil {
tt.config(config)
}
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
if g, w := enabledVariants(ctx, "foo"), tt.fooVariants; !reflect.DeepEqual(w, g) {
t.Errorf("want foo variants:\n%q\ngot:\n%q\n", w, g)
}
if g, w := enabledVariants(ctx, "bar"), tt.barVariants; !reflect.DeepEqual(w, g) {
t.Errorf("want bar variants:\n%q\ngot:\n%q\n", w, g)
}
if g, w := enabledVariants(ctx, "baz"), tt.bazVariants; !reflect.DeepEqual(w, g) {
t.Errorf("want baz variants:\n%q\ngot:\n%q\n", w, g)
}
if g, w := enabledVariants(ctx, "qux"), tt.quxVariants; !reflect.DeepEqual(w, g) {
t.Errorf("want qux variants:\n%q\ngot:\n%q\n", w, g)
}
})
}
}