Merge changes Iccba8caf,I1eb49f25,Id77575f8,I5679cb51,I7f9f3eb6, ...

* changes:
  Convert android/ninja_deps_test.go to test fixtures
  Add NinjaDeps to TestResult
  Convert android/singleton_module_test.go to test fixtures
  Convert android/neverallow_test.go to test fixtures
  Convert android/singleton_module_test.go to test fixtures
  Convert android/variable_test.go to test fixtures
  Convert android/soong_config_modules_test.go to test fixtures
  Convert android/mutator_test.go to test fixtures
  Convert android/deptag_test.go to test fixtures
This commit is contained in:
Paul Duffin
2021-03-18 18:18:38 +00:00
committed by Gerrit Code Review
9 changed files with 280 additions and 367 deletions

View File

@@ -80,21 +80,20 @@ func TestInstallDependencyTag(t *testing.T) {
} }
` `
config := TestArchConfig(buildDir, nil, bp, nil) result := emptyTestFixtureFactory.RunTest(t,
ctx := NewTestArchContext(config) PrepareForTestWithArchMutator,
FixtureWithRootAndroidBp(bp),
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("test_module", testInstallDependencyTagModuleFactory)
}),
)
ctx.RegisterModuleType("test_module", testInstallDependencyTagModuleFactory) config := result.Config
ctx.Register() hostFoo := result.ModuleForTests("foo", config.BuildOSCommonTarget.String()).Description("install")
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) hostInstallDep := result.ModuleForTests("install_dep", config.BuildOSCommonTarget.String()).Description("install")
FailIfErrored(t, errs) hostTransitive := result.ModuleForTests("transitive", config.BuildOSCommonTarget.String()).Description("install")
_, errs = ctx.PrepareBuildActions(config) hostDep := result.ModuleForTests("dep", config.BuildOSCommonTarget.String()).Description("install")
FailIfErrored(t, errs)
hostFoo := ctx.ModuleForTests("foo", config.BuildOSCommonTarget.String()).Description("install")
hostInstallDep := ctx.ModuleForTests("install_dep", config.BuildOSCommonTarget.String()).Description("install")
hostTransitive := ctx.ModuleForTests("transitive", config.BuildOSCommonTarget.String()).Description("install")
hostDep := ctx.ModuleForTests("dep", config.BuildOSCommonTarget.String()).Description("install")
if g, w := hostFoo.Implicits.Strings(), hostInstallDep.Output.String(); !InList(w, g) { if g, w := hostFoo.Implicits.Strings(), hostInstallDep.Output.String(); !InList(w, g) {
t.Errorf("expected host dependency %q, got %q", w, g) t.Errorf("expected host dependency %q, got %q", w, g)
@@ -112,10 +111,10 @@ func TestInstallDependencyTag(t *testing.T) {
t.Errorf("expected no host dependency %q, got %q", w, g) t.Errorf("expected no host dependency %q, got %q", w, g)
} }
deviceFoo := ctx.ModuleForTests("foo", "android_common").Description("install") deviceFoo := result.ModuleForTests("foo", "android_common").Description("install")
deviceInstallDep := ctx.ModuleForTests("install_dep", "android_common").Description("install") deviceInstallDep := result.ModuleForTests("install_dep", "android_common").Description("install")
deviceTransitive := ctx.ModuleForTests("transitive", "android_common").Description("install") deviceTransitive := result.ModuleForTests("transitive", "android_common").Description("install")
deviceDep := ctx.ModuleForTests("dep", "android_common").Description("install") deviceDep := result.ModuleForTests("dep", "android_common").Description("install")
if g, w := deviceFoo.OrderOnly.Strings(), deviceInstallDep.Output.String(); !InList(w, g) { if g, w := deviceFoo.OrderOnly.Strings(), deviceInstallDep.Output.String(); !InList(w, g) {
t.Errorf("expected device dependency %q, got %q", w, g) t.Errorf("expected device dependency %q, got %q", w, g)

View File

@@ -582,6 +582,10 @@ type TestResult struct {
// The errors that were reported during the test. // The errors that were reported during the test.
Errs []error Errs []error
// The ninja deps is a list of the ninja files dependencies that were added by the modules and
// singletons via the *.AddNinjaFileDeps() methods.
NinjaDeps []string
} }
var _ FixtureFactory = (*fixtureFactory)(nil) var _ FixtureFactory = (*fixtureFactory)(nil)
@@ -722,9 +726,14 @@ func (f *fixture) RunTest() *TestResult {
} }
ctx.Register() ctx.Register()
_, errs := ctx.ParseBlueprintsFiles("ignored") var ninjaDeps []string
extraNinjaDeps, errs := ctx.ParseBlueprintsFiles("ignored")
if len(errs) == 0 { if len(errs) == 0 {
_, errs = ctx.PrepareBuildActions(f.config) ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
extraNinjaDeps, errs = ctx.PrepareBuildActions(f.config)
if len(errs) == 0 {
ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
}
} }
result := &TestResult{ result := &TestResult{
@@ -732,6 +741,7 @@ func (f *fixture) RunTest() *TestResult {
fixture: f, fixture: f,
Config: f.config, Config: f.config,
Errs: errs, Errs: errs,
NinjaDeps: ninjaDeps,
} }
f.errorHandler.CheckErrors(f.t, result) f.errorHandler.CheckErrors(f.t, result)

View File

@@ -16,12 +16,10 @@ package android
import ( import (
"fmt" "fmt"
"reflect"
"strings" "strings"
"testing" "testing"
"github.com/google/blueprint" "github.com/google/blueprint"
"github.com/google/blueprint/proptools"
) )
type mutatorTestModule struct { type mutatorTestModule struct {
@@ -67,28 +65,20 @@ func TestMutatorAddMissingDependencies(t *testing.T) {
} }
` `
config := TestConfig(buildDir, nil, bp, nil) result := emptyTestFixtureFactory.RunTest(t,
config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true) PrepareForTestWithAllowMissingDependencies,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("test", mutatorTestModuleFactory)
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator)
})
}),
FixtureWithRootAndroidBp(bp),
)
ctx := NewTestContext(config) foo := result.ModuleForTests("foo", "").Module().(*mutatorTestModule)
ctx.SetAllowMissingDependencies(true)
ctx.RegisterModuleType("test", mutatorTestModuleFactory) AssertDeepEquals(t, "foo missing deps", []string{"added_missing_dep", "regular_missing_dep"}, foo.missingDeps)
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator)
})
ctx.Register()
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
foo := ctx.ModuleForTests("foo", "").Module().(*mutatorTestModule)
if g, w := foo.missingDeps, []string{"added_missing_dep", "regular_missing_dep"}; !reflect.DeepEqual(g, w) {
t.Errorf("want foo missing deps %q, got %q", w, g)
}
} }
func TestModuleString(t *testing.T) { func TestModuleString(t *testing.T) {
@@ -98,52 +88,47 @@ func TestModuleString(t *testing.T) {
} }
` `
config := TestConfig(buildDir, nil, bp, nil)
ctx := NewTestContext(config)
var moduleStrings []string var moduleStrings []string
ctx.PreArchMutators(func(ctx RegisterMutatorsContext) { emptyTestFixtureFactory.RunTest(t,
ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) { FixtureRegisterWithContext(func(ctx RegistrationContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.CreateVariations("a", "b")
})
ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.Rename(ctx.Module().base().Name() + "_renamed1")
})
})
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { ctx.PreArchMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("pre_deps", func(ctx BottomUpMutatorContext) { ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String()) moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.CreateVariations("c", "d") ctx.CreateVariations("a", "b")
}) })
}) ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.Rename(ctx.Module().base().Name() + "_renamed1")
})
})
ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("post_deps", func(ctx BottomUpMutatorContext) { ctx.BottomUp("pre_deps", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String()) moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.CreateLocalVariations("e", "f") ctx.CreateVariations("c", "d")
}) })
ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) { })
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.Rename(ctx.Module().base().Name() + "_renamed2")
})
ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
})
})
ctx.RegisterModuleType("test", mutatorTestModuleFactory) ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("post_deps", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.CreateLocalVariations("e", "f")
})
ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
ctx.Rename(ctx.Module().base().Name() + "_renamed2")
})
ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String())
})
})
ctx.Register() ctx.RegisterModuleType("test", mutatorTestModuleFactory)
}),
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) FixtureWithRootAndroidBp(bp),
FailIfErrored(t, errs) )
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
want := []string{ want := []string{
// Initial name. // Initial name.
@@ -184,9 +169,7 @@ func TestModuleString(t *testing.T) {
"foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}", "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}",
} }
if !reflect.DeepEqual(moduleStrings, want) { AssertDeepEquals(t, "module String() values", want, moduleStrings)
t.Errorf("want module String() values:\n%q\ngot:\n%q", want, moduleStrings)
}
} }
func TestFinalDepsPhase(t *testing.T) { func TestFinalDepsPhase(t *testing.T) {
@@ -202,52 +185,46 @@ func TestFinalDepsPhase(t *testing.T) {
} }
` `
config := TestConfig(buildDir, nil, bp, nil)
ctx := NewTestContext(config)
finalGot := map[string]int{} finalGot := map[string]int{}
dep1Tag := struct { emptyTestFixtureFactory.RunTest(t,
blueprint.BaseDependencyTag FixtureRegisterWithContext(func(ctx RegistrationContext) {
}{} dep1Tag := struct {
dep2Tag := struct { blueprint.BaseDependencyTag
blueprint.BaseDependencyTag }{}
}{} dep2Tag := struct {
blueprint.BaseDependencyTag
}{}
ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("far_deps_1", func(ctx BottomUpMutatorContext) { ctx.BottomUp("far_deps_1", func(ctx BottomUpMutatorContext) {
if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { if !strings.HasPrefix(ctx.ModuleName(), "common_dep") {
ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1") ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1")
} }
}) })
ctx.BottomUp("variant", func(ctx BottomUpMutatorContext) { ctx.BottomUp("variant", func(ctx BottomUpMutatorContext) {
ctx.CreateLocalVariations("a", "b") ctx.CreateLocalVariations("a", "b")
}) })
})
ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("far_deps_2", func(ctx BottomUpMutatorContext) {
if !strings.HasPrefix(ctx.ModuleName(), "common_dep") {
ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2")
}
})
ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
finalGot[ctx.Module().String()] += 1
ctx.VisitDirectDeps(func(mod Module) {
finalGot[fmt.Sprintf("%s -> %s", ctx.Module().String(), mod)] += 1
}) })
})
})
ctx.RegisterModuleType("test", mutatorTestModuleFactory) ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("far_deps_2", func(ctx BottomUpMutatorContext) {
if !strings.HasPrefix(ctx.ModuleName(), "common_dep") {
ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2")
}
})
ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
finalGot[ctx.Module().String()] += 1
ctx.VisitDirectDeps(func(mod Module) {
finalGot[fmt.Sprintf("%s -> %s", ctx.Module().String(), mod)] += 1
})
})
})
ctx.Register() ctx.RegisterModuleType("test", mutatorTestModuleFactory)
}),
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) FixtureWithRootAndroidBp(bp),
FailIfErrored(t, errs) )
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
finalWant := map[string]int{ finalWant := map[string]int{
"common_dep_1{variant:a}": 1, "common_dep_1{variant:a}": 1,
@@ -262,37 +239,31 @@ func TestFinalDepsPhase(t *testing.T) {
"foo{variant:b} -> common_dep_2{variant:a}": 1, "foo{variant:b} -> common_dep_2{variant:a}": 1,
} }
if !reflect.DeepEqual(finalWant, finalGot) { AssertDeepEquals(t, "final", finalWant, finalGot)
t.Errorf("want:\n%q\ngot:\n%q", finalWant, finalGot)
}
} }
func TestNoCreateVariationsInFinalDeps(t *testing.T) { func TestNoCreateVariationsInFinalDeps(t *testing.T) {
config := TestConfig(buildDir, nil, `test {name: "foo"}`, nil)
ctx := NewTestContext(config)
checkErr := func() { checkErr := func() {
if err := recover(); err == nil || !strings.Contains(fmt.Sprintf("%s", err), "not allowed in FinalDepsMutators") { if err := recover(); err == nil || !strings.Contains(fmt.Sprintf("%s", err), "not allowed in FinalDepsMutators") {
panic("Expected FinalDepsMutators consistency check to fail") panic("Expected FinalDepsMutators consistency check to fail")
} }
} }
ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { emptyTestFixtureFactory.RunTest(t,
ctx.BottomUp("vars", func(ctx BottomUpMutatorContext) { FixtureRegisterWithContext(func(ctx RegistrationContext) {
defer checkErr() ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.CreateVariations("a", "b") ctx.BottomUp("vars", func(ctx BottomUpMutatorContext) {
}) defer checkErr()
ctx.BottomUp("local_vars", func(ctx BottomUpMutatorContext) { ctx.CreateVariations("a", "b")
defer checkErr() })
ctx.CreateLocalVariations("a", "b") ctx.BottomUp("local_vars", func(ctx BottomUpMutatorContext) {
}) defer checkErr()
}) ctx.CreateLocalVariations("a", "b")
})
})
ctx.RegisterModuleType("test", mutatorTestModuleFactory) ctx.RegisterModuleType("test", mutatorTestModuleFactory)
ctx.Register() }),
FixtureWithRootAndroidBp(`test {name: "foo"}`),
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) )
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
} }

View File

@@ -28,7 +28,7 @@ var neverallowTests = []struct {
rules []Rule rules []Rule
// Additional contents to add to the virtual filesystem used by the tests. // Additional contents to add to the virtual filesystem used by the tests.
fs map[string][]byte fs MockFS
// The expected error patterns. If empty then no errors are expected, otherwise each error // The expected error patterns. If empty then no errors are expected, otherwise each error
// reported must be matched by at least one of these patterns. A pattern matches if the error // reported must be matched by at least one of these patterns. A pattern matches if the error
@@ -285,41 +285,36 @@ var neverallowTests = []struct {
}, },
} }
var prepareForNeverAllowTest = GroupFixturePreparers(
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("cc_library", newMockCcLibraryModule)
ctx.RegisterModuleType("java_library", newMockJavaLibraryModule)
ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
ctx.RegisterModuleType("makefile_goal", newMockMakefileGoalModule)
ctx.PostDepsMutators(RegisterNeverallowMutator)
}),
)
func TestNeverallow(t *testing.T) { func TestNeverallow(t *testing.T) {
for _, test := range neverallowTests { for _, test := range neverallowTests {
// Create a test per config to allow for test specific config, e.g. test rules.
config := TestConfig(buildDir, nil, "", test.fs)
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
// If the test has its own rules then use them instead of the default ones. emptyTestFixtureFactory.
if test.rules != nil { ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
SetTestNeverallowRules(config, test.rules) RunTest(t,
} prepareForNeverAllowTest,
_, errs := testNeverallow(config) FixtureModifyConfig(func(config Config) {
CheckErrorsAgainstExpectations(t, errs, test.expectedErrors) // If the test has its own rules then use them instead of the default ones.
if test.rules != nil {
SetTestNeverallowRules(config, test.rules)
}
}),
test.fs.AddToFixture(),
)
}) })
} }
} }
func testNeverallow(config Config) (*TestContext, []error) {
ctx := NewTestContext(config)
ctx.RegisterModuleType("cc_library", newMockCcLibraryModule)
ctx.RegisterModuleType("java_library", newMockJavaLibraryModule)
ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
ctx.RegisterModuleType("makefile_goal", newMockMakefileGoalModule)
ctx.PostDepsMutators(RegisterNeverallowMutator)
ctx.Register()
_, errs := ctx.ParseBlueprintsFiles("Android.bp")
if len(errs) > 0 {
return ctx, errs
}
_, errs = ctx.PrepareBuildActions(config)
return ctx, errs
}
type mockCcLibraryProperties struct { type mockCcLibraryProperties struct {
Include_dirs []string Include_dirs []string
Vendor_available *bool Vendor_available *bool

View File

@@ -53,23 +53,20 @@ func (testNinjaDepsSingleton) GenerateBuildActions(ctx SingletonContext) {
} }
func TestNinjaDeps(t *testing.T) { func TestNinjaDeps(t *testing.T) {
fs := map[string][]byte{ fs := MockFS{
"test_ninja_deps/exists": nil, "test_ninja_deps/exists": nil,
} }
config := TestConfig(buildDir, nil, "", fs)
ctx := NewTestContext(config) result := emptyTestFixtureFactory.RunTest(t,
ctx.RegisterSingletonType("test_ninja_deps_singleton", testNinjaDepsSingletonFactory) FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterSingletonType("ninja_deps_singleton", ninjaDepsSingletonFactory) ctx.RegisterSingletonType("test_ninja_deps_singleton", testNinjaDepsSingletonFactory)
ctx.Register() ctx.RegisterSingletonType("ninja_deps_singleton", ninjaDepsSingletonFactory)
}),
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) fs.AddToFixture(),
FailIfErrored(t, errs) )
ninjaDeps, errs := ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
// Verify that the ninja file has a dependency on the test_ninja_deps directory. // Verify that the ninja file has a dependency on the test_ninja_deps directory.
if g, w := ninjaDeps, "test_ninja_deps"; !InList(w, g) { if g, w := result.NinjaDeps, "test_ninja_deps"; !InList(w, g) {
t.Errorf("expected %q in %q", w, g) t.Errorf("expected %q in %q", w, g)
} }
} }

View File

@@ -6,7 +6,7 @@ import (
var packageTests = []struct { var packageTests = []struct {
name string name string
fs map[string][]byte fs MockFS
expectedErrors []string expectedErrors []string
}{ }{
// Package default_visibility handling is tested in visibility_test.go // Package default_visibility handling is tested in visibility_test.go
@@ -61,43 +61,13 @@ var packageTests = []struct {
func TestPackage(t *testing.T) { func TestPackage(t *testing.T) {
for _, test := range packageTests { for _, test := range packageTests {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
_, errs := testPackage(test.fs) emptyTestFixtureFactory.
ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
expectedErrors := test.expectedErrors RunTest(t,
if expectedErrors == nil { PrepareForTestWithArchMutator,
FailIfErrored(t, errs) PrepareForTestWithPackageModule,
} else { test.fs.AddToFixture(),
for _, expectedError := range expectedErrors { )
FailIfNoMatchingErrors(t, expectedError, errs)
}
if len(errs) > len(expectedErrors) {
t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
for i, expectedError := range expectedErrors {
t.Errorf("expectedErrors[%d] = %s", i, expectedError)
}
for i, err := range errs {
t.Errorf("errs[%d] = %s", i, err)
}
}
}
}) })
} }
} }
func testPackage(fs map[string][]byte) (*TestContext, []error) {
// Create a new config per test as visibility information is stored in the config.
config := TestArchConfig(buildDir, nil, "", fs)
ctx := NewTestArchContext(config)
RegisterPackageBuildComponents(ctx)
ctx.Register()
_, errs := ctx.ParseBlueprintsFiles(".")
if len(errs) > 0 {
return ctx, errs
}
_, errs = ctx.PrepareBuildActions(config)
return ctx, errs
}

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)
}
} }

View File

@@ -15,7 +15,6 @@
package android package android
import ( import (
"reflect"
"testing" "testing"
) )
@@ -181,17 +180,23 @@ func TestSoongConfigModule(t *testing.T) {
} }
` `
run := func(t *testing.T, bp string, fs map[string][]byte) { fixtureForVendorVars := func(vars map[string]map[string]string) FixturePreparer {
return FixtureModifyProductVariables(func(variables FixtureProductVariables) {
variables.VendorVars = vars
})
}
run := func(t *testing.T, bp string, fs MockFS) {
testCases := []struct { testCases := []struct {
name string name string
config Config preparer FixturePreparer
fooExpectedFlags []string fooExpectedFlags []string
fooDefaultsExpectedFlags []string fooDefaultsExpectedFlags []string
}{ }{
{ {
name: "withValues", name: "withValues",
config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{ preparer: fixtureForVendorVars(map[string]map[string]string{
"acme": map[string]string{ "acme": {
"board": "soc_a", "board": "soc_a",
"size": "42", "size": "42",
"feature1": "true", "feature1": "true",
@@ -221,8 +226,8 @@ func TestSoongConfigModule(t *testing.T) {
}, },
{ {
name: "empty_prop_for_string_var", name: "empty_prop_for_string_var",
config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{ preparer: fixtureForVendorVars(map[string]map[string]string{
"acme": map[string]string{"board": "soc_c"}}), "acme": {"board": "soc_c"}}),
fooExpectedFlags: []string{ fooExpectedFlags: []string{
"DEFAULT", "DEFAULT",
"-DGENERIC", "-DGENERIC",
@@ -237,8 +242,8 @@ func TestSoongConfigModule(t *testing.T) {
}, },
{ {
name: "unused_string_var", name: "unused_string_var",
config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{ preparer: fixtureForVendorVars(map[string]map[string]string{
"acme": map[string]string{"board": "soc_d"}}), "acme": {"board": "soc_d"}}),
fooExpectedFlags: []string{ fooExpectedFlags: []string{
"DEFAULT", "DEFAULT",
"-DGENERIC", "-DGENERIC",
@@ -254,8 +259,8 @@ func TestSoongConfigModule(t *testing.T) {
}, },
{ {
name: "conditions_default", name: "conditions_default",
config: testConfigWithVendorVars(buildDir, bp, fs, map[string]map[string]string{}), preparer: fixtureForVendorVars(map[string]map[string]string{}),
fooExpectedFlags: []string{ fooExpectedFlags: []string{
"DEFAULT", "DEFAULT",
"-DGENERIC", "-DGENERIC",
@@ -272,32 +277,29 @@ func TestSoongConfigModule(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
ctx := NewTestContext(tc.config) t.Run(tc.name, func(t *testing.T) {
ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory) result := emptyTestFixtureFactory.RunTest(t,
ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory) tc.preparer,
ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory) PrepareForTestWithDefaults,
ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory) FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory) ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
ctx.RegisterModuleType("test", soongConfigTestModuleFactory) ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
ctx.PreArchMutators(RegisterDefaultsPreArchMutators) ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
ctx.Register() ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory)
ctx.RegisterModuleType("test", soongConfigTestModuleFactory)
}),
fs.AddToFixture(),
FixtureWithRootAndroidBp(bp),
)
_, errs := ctx.ParseBlueprintsFiles("Android.bp") foo := result.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
FailIfErrored(t, errs) AssertDeepEquals(t, "foo cflags", tc.fooExpectedFlags, foo.props.Cflags)
_, errs = ctx.PrepareBuildActions(tc.config)
FailIfErrored(t, errs)
foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule) fooDefaults := result.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule)
if g, w := foo.props.Cflags, tc.fooExpectedFlags; !reflect.DeepEqual(g, w) { AssertDeepEquals(t, "foo_with_defaults cflags", tc.fooDefaultsExpectedFlags, fooDefaults.props.Cflags)
t.Errorf("%s: wanted foo cflags %q, got %q", tc.name, w, g) })
}
fooDefaults := ctx.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule)
if g, w := fooDefaults.props.Cflags, tc.fooDefaultsExpectedFlags; !reflect.DeepEqual(g, w) {
t.Errorf("%s: wanted foo_with_defaults cflags %q, got %q", tc.name, w, g)
}
} }
} }
t.Run("single file", func(t *testing.T) { t.Run("single file", func(t *testing.T) {

View File

@@ -181,32 +181,30 @@ func TestProductVariables(t *testing.T) {
name: "baz", name: "baz",
} }
` `
config := TestConfig(buildDir, nil, bp, nil)
config.TestProductVariables.Eng = proptools.BoolPtr(true)
ctx := NewTestContext(config) emptyTestFixtureFactory.RunTest(t,
// A module type that has a srcs property but not a cflags property. FixtureModifyProductVariables(func(variables FixtureProductVariables) {
ctx.RegisterModuleType("module1", testProductVariableModuleFactoryFactory(&struct { variables.Eng = proptools.BoolPtr(true)
Srcs []string }),
}{})) FixtureRegisterWithContext(func(ctx RegistrationContext) {
// A module type that has a cflags property but not a srcs property. // A module type that has a srcs property but not a cflags property.
ctx.RegisterModuleType("module2", testProductVariableModuleFactoryFactory(&struct { ctx.RegisterModuleType("module1", testProductVariableModuleFactoryFactory(&struct {
Cflags []string Srcs []string
}{})) }{}))
// A module type that does not have any properties that match product_variables. // A module type that has a cflags property but not a srcs property.
ctx.RegisterModuleType("module3", testProductVariableModuleFactoryFactory(&struct { ctx.RegisterModuleType("module2", testProductVariableModuleFactoryFactory(&struct {
Foo []string Cflags []string
}{})) }{}))
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { // A module type that does not have any properties that match product_variables.
ctx.BottomUp("variable", VariableMutator).Parallel() ctx.RegisterModuleType("module3", testProductVariableModuleFactoryFactory(&struct {
}) Foo []string
}{}))
ctx.Register() ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("variable", VariableMutator).Parallel()
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) })
FailIfErrored(t, errs) }),
_, errs = ctx.PrepareBuildActions(config) FixtureWithRootAndroidBp(bp),
FailIfErrored(t, errs) )
} }
var testProductVariableDefaultsProperties = struct { var testProductVariableDefaultsProperties = struct {
@@ -290,32 +288,23 @@ func TestProductVariablesDefaults(t *testing.T) {
} }
` `
config := TestConfig(buildDir, nil, bp, nil) result := emptyTestFixtureFactory.RunTest(t,
config.TestProductVariables.Eng = boolPtr(true) FixtureModifyProductVariables(func(variables FixtureProductVariables) {
variables.Eng = boolPtr(true)
}),
PrepareForTestWithDefaults,
PrepareForTestWithVariables,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("test", productVariablesDefaultsTestModuleFactory)
ctx.RegisterModuleType("defaults", productVariablesDefaultsTestDefaultsFactory)
}),
FixtureWithRootAndroidBp(bp),
)
ctx := NewTestContext(config) foo := result.ModuleForTests("foo", "").Module().(*productVariablesDefaultsTestModule)
ctx.RegisterModuleType("test", productVariablesDefaultsTestModuleFactory)
ctx.RegisterModuleType("defaults", productVariablesDefaultsTestDefaultsFactory)
ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("variable", VariableMutator).Parallel()
})
ctx.Register()
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
foo := ctx.ModuleForTests("foo", "").Module().(*productVariablesDefaultsTestModule)
want := []string{"defaults", "module", "product_variable_defaults", "product_variable_module"} want := []string{"defaults", "module", "product_variable_defaults", "product_variable_module"}
if g, w := foo.properties.Foo, want; !reflect.DeepEqual(g, w) { AssertDeepEquals(t, "foo", want, foo.properties.Foo)
t.Errorf("expected foo %q, got %q", w, g)
}
} }
func BenchmarkSliceToTypeArray(b *testing.B) { func BenchmarkSliceToTypeArray(b *testing.B) {