summaryrefslogtreecommitdiff
path: root/android/testing.go
diff options
context:
space:
mode:
Diffstat (limited to 'android/testing.go')
-rw-r--r--android/testing.go92
1 files changed, 54 insertions, 38 deletions
diff --git a/android/testing.go b/android/testing.go
index 6c4f4f8de..1962fdea5 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -530,7 +530,8 @@ func (ctx *TestContext) RegisterParallelSingletonType(name string, factory Singl
// both have the same value. Both the module and the map are allowed to have
// extra variations that the other doesn't have. Panics if not exactly one
// module variant matches.
-func (ctx *TestContext) ModuleVariantForTests(name string, matchVariations map[string]string) TestingModule {
+func (ctx *TestContext) ModuleVariantForTests(t *testing.T, name string, matchVariations map[string]string) TestingModule {
+ t.Helper()
modules := []Module{}
ctx.VisitAllModules(func(m blueprint.Module) {
if ctx.ModuleName(m) == name {
@@ -562,12 +563,12 @@ func (ctx *TestContext) ModuleVariantForTests(name string, matchVariations map[s
})
if len(allVariants) == 0 {
- panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
- name, strings.Join(SortedUniqueStrings(allModuleNames), "\n ")))
+ t.Fatalf("failed to find module %q. All modules:\n %s",
+ name, strings.Join(SortedUniqueStrings(allModuleNames), "\n "))
} else {
sort.Strings(allVariants)
- panic(fmt.Errorf("failed to find module %q matching %v. All variants:\n %s",
- name, matchVariations, strings.Join(allVariants, "\n ")))
+ t.Fatalf("failed to find module %q matching %v. All variants:\n %s",
+ name, matchVariations, strings.Join(allVariants, "\n "))
}
}
@@ -577,14 +578,15 @@ func (ctx *TestContext) ModuleVariantForTests(name string, matchVariations map[s
moduleStrings = append(moduleStrings, m.String())
}
sort.Strings(moduleStrings)
- panic(fmt.Errorf("module %q has more than one variant that match %v:\n %s",
- name, matchVariations, strings.Join(moduleStrings, "\n ")))
+ t.Fatalf("module %q has more than one variant that match %v:\n %s",
+ name, matchVariations, strings.Join(moduleStrings, "\n "))
}
- return newTestingModule(ctx.config, modules[0])
+ return newTestingModule(t, ctx.config, modules[0])
}
-func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
+func (ctx *TestContext) ModuleForTests(t *testing.T, name, variant string) TestingModule {
+ t.Helper()
var module Module
ctx.VisitAllModules(func(m blueprint.Module) {
if ctx.ModuleName(m) == name && ctx.ModuleSubDir(m) == variant {
@@ -605,15 +607,15 @@ func (ctx *TestContext) ModuleForTests(name, variant string) TestingModule {
sort.Strings(allVariants)
if len(allVariants) == 0 {
- panic(fmt.Errorf("failed to find module %q. All modules:\n %s",
- name, strings.Join(SortedUniqueStrings(allModuleNames), "\n ")))
+ t.Fatalf("failed to find module %q. All modules:\n %s",
+ name, strings.Join(SortedUniqueStrings(allModuleNames), "\n "))
} else {
- panic(fmt.Errorf("failed to find module %q variant %q. All variants:\n %s",
- name, variant, strings.Join(allVariants, "\n ")))
+ t.Fatalf("failed to find module %q variant %q. All variants:\n %s",
+ name, variant, strings.Join(allVariants, "\n "))
}
}
- return newTestingModule(ctx.config, module)
+ return newTestingModule(t, ctx.config, module)
}
func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
@@ -627,21 +629,24 @@ func (ctx *TestContext) ModuleVariantsForTests(name string) []string {
}
// SingletonForTests returns a TestingSingleton for the singleton registered with the given name.
-func (ctx *TestContext) SingletonForTests(name string) TestingSingleton {
+func (ctx *TestContext) SingletonForTests(t *testing.T, name string) TestingSingleton {
+ t.Helper()
allSingletonNames := []string{}
for _, s := range ctx.Singletons() {
n := ctx.SingletonName(s)
if n == name {
return TestingSingleton{
- baseTestingComponent: newBaseTestingComponent(ctx.config, s.(testBuildProvider)),
+ baseTestingComponent: newBaseTestingComponent(t, ctx.config, s.(testBuildProvider)),
singleton: s.(*singletonAdaptor).Singleton,
}
}
allSingletonNames = append(allSingletonNames, n)
}
- panic(fmt.Errorf("failed to find singleton %q."+
- "\nall singletons: %v", name, allSingletonNames))
+ t.Fatalf("failed to find singleton %q."+
+ "\nall singletons: %v", name, allSingletonNames)
+
+ return TestingSingleton{}
}
type InstallMakeRule struct {
@@ -651,6 +656,7 @@ type InstallMakeRule struct {
}
func parseMkRules(t *testing.T, config Config, nodes []mkparser.Node) []InstallMakeRule {
+ t.Helper()
var rules []InstallMakeRule
for _, node := range nodes {
if mkParserRule, ok := node.(*mkparser.Rule); ok {
@@ -688,7 +694,8 @@ func parseMkRules(t *testing.T, config Config, nodes []mkparser.Node) []InstallM
}
func (ctx *TestContext) InstallMakeRulesForTesting(t *testing.T) []InstallMakeRule {
- installs := ctx.SingletonForTests("makevars").Singleton().(*makeVarsSingleton).installsForTesting
+ t.Helper()
+ installs := ctx.SingletonForTests(t, "makevars").Singleton().(*makeVarsSingleton).installsForTesting
buf := bytes.NewBuffer(append([]byte(nil), installs...))
parser := mkparser.NewParser("makevars", buf)
@@ -728,8 +735,9 @@ var PrepareForTestAccessingMakeVars = GroupFixturePreparers(
//
// It is necessary to use PrepareForTestAccessingMakeVars in tests that want to call this function.
// Along with any other preparers needed to add the make vars.
-func (ctx *TestContext) MakeVarsForTesting(filter func(variable MakeVarVariable) bool) []MakeVarVariable {
- vars := ctx.SingletonForTests("makevars").Singleton().(*makeVarsSingleton).varsForTesting
+func (ctx *TestContext) MakeVarsForTesting(t *testing.T, filter func(variable MakeVarVariable) bool) []MakeVarVariable {
+ t.Helper()
+ vars := ctx.SingletonForTests(t, "makevars").Singleton().(*makeVarsSingleton).varsForTesting
result := make([]MakeVarVariable, 0, len(vars))
for _, v := range vars {
if filter(v) {
@@ -846,12 +854,13 @@ func (p TestingBuildParams) AllOutputs() []string {
// baseTestingComponent provides functionality common to both TestingModule and TestingSingleton.
type baseTestingComponent struct {
+ t *testing.T
config Config
provider testBuildProvider
}
-func newBaseTestingComponent(config Config, provider testBuildProvider) baseTestingComponent {
- return baseTestingComponent{config, provider}
+func newBaseTestingComponent(t *testing.T, config Config, provider testBuildProvider) baseTestingComponent {
+ return baseTestingComponent{t, config, provider}
}
// A function that will normalize a string containing paths, e.g. ninja command, by replacing
@@ -922,9 +931,10 @@ func (b baseTestingComponent) maybeBuildParamsFromRule(rule string) (TestingBuil
}
func (b baseTestingComponent) buildParamsFromRule(rule string) TestingBuildParams {
+ b.t.Helper()
p, searchRules := b.maybeBuildParamsFromRule(rule)
if p.Rule == nil {
- panic(fmt.Errorf("couldn't find rule %q.\nall rules:\n%s", rule, strings.Join(searchRules, "\n")))
+ b.t.Fatalf("couldn't find rule %q.\nall rules:\n%s", rule, strings.Join(searchRules, "\n"))
}
return p
}
@@ -941,9 +951,10 @@ func (b baseTestingComponent) maybeBuildParamsFromDescription(desc string) (Test
}
func (b baseTestingComponent) buildParamsFromDescription(desc string) TestingBuildParams {
+ b.t.Helper()
p, searchedDescriptions := b.maybeBuildParamsFromDescription(desc)
if p.Rule == nil {
- panic(fmt.Errorf("couldn't find description %q\nall descriptions:\n%s", desc, strings.Join(searchedDescriptions, "\n")))
+ b.t.Fatalf("couldn't find description %q\nall descriptions:\n%s", desc, strings.Join(searchedDescriptions, "\n"))
}
return p
}
@@ -974,10 +985,11 @@ func (b baseTestingComponent) maybeBuildParamsFromOutput(file string) (TestingBu
}
func (b baseTestingComponent) buildParamsFromOutput(file string) TestingBuildParams {
+ b.t.Helper()
p, searchedOutputs := b.maybeBuildParamsFromOutput(file)
if p.Rule == nil {
- panic(fmt.Errorf("couldn't find output %q.\nall outputs:\n %s\n",
- file, strings.Join(searchedOutputs, "\n ")))
+ b.t.Fatalf("couldn't find output %q.\nall outputs:\n %s\n",
+ file, strings.Join(searchedOutputs, "\n "))
}
return p
}
@@ -999,6 +1011,7 @@ func (b baseTestingComponent) MaybeRule(rule string) TestingBuildParams {
// Rule finds a call to ctx.Build with BuildParams.Rule set to a rule with the given name. Panics if no rule is found.
func (b baseTestingComponent) Rule(rule string) TestingBuildParams {
+ b.t.Helper()
return b.buildParamsFromRule(rule)
}
@@ -1012,6 +1025,7 @@ func (b baseTestingComponent) MaybeDescription(desc string) TestingBuildParams {
// Description finds a call to ctx.Build with BuildParams.Description set to a the given string. Panics if no rule is
// found.
func (b baseTestingComponent) Description(desc string) TestingBuildParams {
+ b.t.Helper()
return b.buildParamsFromDescription(desc)
}
@@ -1025,6 +1039,7 @@ func (b baseTestingComponent) MaybeOutput(file string) TestingBuildParams {
// Output finds a call to ctx.Build with a BuildParams.Output or BuildParams.Outputs whose String() or Rel()
// value matches the provided string. Panics if no rule is found.
func (b baseTestingComponent) Output(file string) TestingBuildParams {
+ b.t.Helper()
return b.buildParamsFromOutput(file)
}
@@ -1040,9 +1055,9 @@ type TestingModule struct {
module Module
}
-func newTestingModule(config Config, module Module) TestingModule {
+func newTestingModule(t *testing.T, config Config, module Module) TestingModule {
return TestingModule{
- newBaseTestingComponent(config, module),
+ newBaseTestingComponent(t, config, module),
module,
}
}
@@ -1152,7 +1167,7 @@ func SetKatiEnabledForTests(config Config) {
config.katiEnabled = true
}
-func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) []AndroidMkEntries {
+func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod Module) []AndroidMkEntries {
t.Helper()
var p AndroidMkEntriesProvider
var ok bool
@@ -1161,15 +1176,15 @@ func AndroidMkEntriesForTest(t *testing.T, ctx *TestContext, mod blueprint.Modul
}
entriesList := p.AndroidMkEntries()
- aconfigUpdateAndroidMkEntries(ctx, mod.(Module), &entriesList)
+ aconfigUpdateAndroidMkEntries(ctx, mod, &entriesList)
for i := range entriesList {
entriesList[i].fillInEntries(ctx, mod)
}
return entriesList
}
-func AndroidMkInfoForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) *AndroidMkProviderInfo {
- if runtime.GOOS == "darwin" && mod.(Module).base().Os() != Darwin {
+func AndroidMkInfoForTest(t *testing.T, ctx *TestContext, mod Module) *AndroidMkProviderInfo {
+ if runtime.GOOS == "darwin" && mod.base().Os() != Darwin {
// The AndroidMkInfo provider is not set in this case.
t.Skip("AndroidMkInfo provider is not set on darwin")
}
@@ -1181,18 +1196,19 @@ func AndroidMkInfoForTest(t *testing.T, ctx *TestContext, mod blueprint.Module)
}
info := OtherModuleProviderOrDefault(ctx, mod, AndroidMkInfoProvider)
- aconfigUpdateAndroidMkInfos(ctx, mod.(Module), info)
- info.PrimaryInfo.fillInEntries(ctx, mod)
+ aconfigUpdateAndroidMkInfos(ctx, mod, info)
+ commonInfo, _ := OtherModuleProvider(ctx, mod, CommonModuleInfoKey)
+ info.PrimaryInfo.fillInEntries(ctx, mod, &commonInfo)
if len(info.ExtraInfo) > 0 {
for _, ei := range info.ExtraInfo {
- ei.fillInEntries(ctx, mod)
+ ei.fillInEntries(ctx, mod, &commonInfo)
}
}
return info
}
-func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module) AndroidMkData {
+func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod Module) AndroidMkData {
t.Helper()
var p AndroidMkDataProvider
var ok bool
@@ -1201,7 +1217,7 @@ func AndroidMkDataForTest(t *testing.T, ctx *TestContext, mod blueprint.Module)
}
data := p.AndroidMk()
data.fillInData(ctx, mod)
- aconfigUpdateAndroidMkData(ctx, mod.(Module), &data)
+ aconfigUpdateAndroidMkData(ctx, mod, &data)
return data
}