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)
}),
)
ctx.Register() config := result.Config
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
hostFoo := ctx.ModuleForTests("foo", config.BuildOSCommonTarget.String()).Description("install") hostFoo := result.ModuleForTests("foo", config.BuildOSCommonTarget.String()).Description("install")
hostInstallDep := ctx.ModuleForTests("install_dep", config.BuildOSCommonTarget.String()).Description("install") hostInstallDep := result.ModuleForTests("install_dep", config.BuildOSCommonTarget.String()).Description("install")
hostTransitive := ctx.ModuleForTests("transitive", config.BuildOSCommonTarget.String()).Description("install") hostTransitive := result.ModuleForTests("transitive", config.BuildOSCommonTarget.String()).Description("install")
hostDep := ctx.ModuleForTests("dep", config.BuildOSCommonTarget.String()).Description("install") hostDep := result.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 := NewTestContext(config)
ctx.SetAllowMissingDependencies(true)
ctx.RegisterModuleType("test", mutatorTestModuleFactory) ctx.RegisterModuleType("test", mutatorTestModuleFactory)
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator) ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator)
}) })
}),
FixtureWithRootAndroidBp(bp),
)
ctx.Register() foo := result.ModuleForTests("foo", "").Module().(*mutatorTestModule)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
FailIfErrored(t, errs)
foo := ctx.ModuleForTests("foo", "").Module().(*mutatorTestModule) AssertDeepEquals(t, "foo missing deps", []string{"added_missing_dep", "regular_missing_dep"}, foo.missingDeps)
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,12 +88,11 @@ func TestModuleString(t *testing.T) {
} }
` `
config := TestConfig(buildDir, nil, bp, nil)
ctx := NewTestContext(config)
var moduleStrings []string var moduleStrings []string
emptyTestFixtureFactory.RunTest(t,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.PreArchMutators(func(ctx RegisterMutatorsContext) { ctx.PreArchMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) { ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) {
moduleStrings = append(moduleStrings, ctx.Module().String()) moduleStrings = append(moduleStrings, ctx.Module().String())
@@ -137,13 +126,9 @@ func TestModuleString(t *testing.T) {
}) })
ctx.RegisterModuleType("test", mutatorTestModuleFactory) ctx.RegisterModuleType("test", mutatorTestModuleFactory)
}),
ctx.Register() FixtureWithRootAndroidBp(bp),
)
_, errs := ctx.ParseFileList(".", []string{"Android.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,12 +185,10 @@ func TestFinalDepsPhase(t *testing.T) {
} }
` `
config := TestConfig(buildDir, nil, bp, nil)
ctx := NewTestContext(config)
finalGot := map[string]int{} finalGot := map[string]int{}
emptyTestFixtureFactory.RunTest(t,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
dep1Tag := struct { dep1Tag := struct {
blueprint.BaseDependencyTag blueprint.BaseDependencyTag
}{} }{}
@@ -241,13 +222,9 @@ func TestFinalDepsPhase(t *testing.T) {
}) })
ctx.RegisterModuleType("test", mutatorTestModuleFactory) ctx.RegisterModuleType("test", mutatorTestModuleFactory)
}),
ctx.Register() FixtureWithRootAndroidBp(bp),
)
_, errs := ctx.ParseFileList(".", []string{"Android.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,21 +239,18 @@ 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")
} }
} }
emptyTestFixtureFactory.RunTest(t,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("vars", func(ctx BottomUpMutatorContext) { ctx.BottomUp("vars", func(ctx BottomUpMutatorContext) {
defer checkErr() defer checkErr()
@@ -289,10 +263,7 @@ func TestNoCreateVariationsInFinalDeps(t *testing.T) {
}) })
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,39 +285,34 @@ var neverallowTests = []struct {
}, },
} }
func TestNeverallow(t *testing.T) { var prepareForNeverAllowTest = GroupFixturePreparers(
for _, test := range neverallowTests { FixtureRegisterWithContext(func(ctx RegistrationContext) {
// 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) {
// If the test has its own rules then use them instead of the default ones.
if test.rules != nil {
SetTestNeverallowRules(config, test.rules)
}
_, errs := testNeverallow(config)
CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
})
}
}
func testNeverallow(config Config) (*TestContext, []error) {
ctx := NewTestContext(config)
ctx.RegisterModuleType("cc_library", newMockCcLibraryModule) ctx.RegisterModuleType("cc_library", newMockCcLibraryModule)
ctx.RegisterModuleType("java_library", newMockJavaLibraryModule) ctx.RegisterModuleType("java_library", newMockJavaLibraryModule)
ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule) ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule) ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
ctx.RegisterModuleType("makefile_goal", newMockMakefileGoalModule) ctx.RegisterModuleType("makefile_goal", newMockMakefileGoalModule)
ctx.PostDepsMutators(RegisterNeverallowMutator) ctx.PostDepsMutators(RegisterNeverallowMutator)
ctx.Register() }),
)
_, errs := ctx.ParseBlueprintsFiles("Android.bp") func TestNeverallow(t *testing.T) {
if len(errs) > 0 { for _, test := range neverallowTests {
return ctx, errs t.Run(test.name, func(t *testing.T) {
emptyTestFixtureFactory.
ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
RunTest(t,
prepareForNeverAllowTest,
FixtureModifyConfig(func(config Config) {
// 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(),
)
})
} }
_, errs = ctx.PrepareBuildActions(config)
return ctx, errs
} }
type mockCcLibraryProperties struct { type mockCcLibraryProperties struct {

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,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterSingletonType("test_ninja_deps_singleton", testNinjaDepsSingletonFactory) ctx.RegisterSingletonType("test_ninja_deps_singleton", testNinjaDepsSingletonFactory)
ctx.RegisterSingletonType("ninja_deps_singleton", ninjaDepsSingletonFactory) ctx.RegisterSingletonType("ninja_deps_singleton", ninjaDepsSingletonFactory)
ctx.Register() }),
fs.AddToFixture(),
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) )
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{
`\QGenerateAndroidBuildActions already called for variant\E`,
})).
RunTest(t,
prepareForSingletonModuleTest,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("test_singleton_module_mutator", testVariantSingletonModuleMutator) ctx.BottomUp("test_singleton_module_mutator", testVariantSingletonModuleMutator)
}) })
ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory) }),
ctx.Register() FixtureWithRootAndroidBp(bp),
)
_, errs := ctx.ParseBlueprintsFiles("Android.bp")
if len(errs) == 0 {
_, errs = ctx.PrepareBuildActions(config)
}
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",
@@ -255,7 +260,7 @@ 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) {
result := emptyTestFixtureFactory.RunTest(t,
tc.preparer,
PrepareForTestWithDefaults,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory) ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory) ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory) ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory) ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory) ctx.RegisterModuleType("test_defaults", soongConfigTestDefaultsModuleFactory)
ctx.RegisterModuleType("test", soongConfigTestModuleFactory) ctx.RegisterModuleType("test", soongConfigTestModuleFactory)
ctx.PreArchMutators(RegisterDefaultsPreArchMutators) }),
ctx.Register() 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,10 +181,12 @@ 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,
FixtureModifyProductVariables(func(variables FixtureProductVariables) {
variables.Eng = proptools.BoolPtr(true)
}),
FixtureRegisterWithContext(func(ctx RegistrationContext) {
// A module type that has a srcs property but not a cflags property. // A module type that has a srcs property but not a cflags property.
ctx.RegisterModuleType("module1", testProductVariableModuleFactoryFactory(&struct { ctx.RegisterModuleType("module1", testProductVariableModuleFactoryFactory(&struct {
Srcs []string Srcs []string
@@ -200,13 +202,9 @@ func TestProductVariables(t *testing.T) {
ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
ctx.BottomUp("variable", VariableMutator).Parallel() ctx.BottomUp("variable", VariableMutator).Parallel()
}) })
}),
ctx.Register() FixtureWithRootAndroidBp(bp),
)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
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)
ctx := NewTestContext(config) }),
PrepareForTestWithDefaults,
PrepareForTestWithVariables,
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.RegisterModuleType("test", productVariablesDefaultsTestModuleFactory) ctx.RegisterModuleType("test", productVariablesDefaultsTestModuleFactory)
ctx.RegisterModuleType("defaults", productVariablesDefaultsTestDefaultsFactory) ctx.RegisterModuleType("defaults", productVariablesDefaultsTestDefaultsFactory)
}),
FixtureWithRootAndroidBp(bp),
)
ctx.PreArchMutators(RegisterDefaultsPreArchMutators) foo := result.ModuleForTests("foo", "").Module().(*productVariablesDefaultsTestModule)
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) {