diff options
author | 2025-02-11 14:58:07 -0800 | |
---|---|---|
committer | 2025-02-12 10:36:48 -0800 | |
commit | 90607e9056f6ff4cec2447fdd7a8b252d67ffde7 (patch) | |
tree | d4e244bc76e8e13a8438bcd921fd9003bce46730 /android/testing.go | |
parent | d8db8faba62a3cb77f75294e96deda9e53c15786 (diff) |
Don't panic in ModuleForTests and friends
Panicking in ModuleForTests and similar test helper functions was
a mistake. Go's test runner stops running tests as soon as any
test panics, which means debugging multiple tests panicking requires
rerunning all the tests after fixing each panic to find the next
one. Pass the *testing.T into ModuleForTests and friends so that
it can call t.Fatalf instead.
Test: all soong tests pass
Change-Id: I5d0f2424eaf04fb795079e6d1e4b9469d8c7033c
Diffstat (limited to 'android/testing.go')
-rw-r--r-- | android/testing.go | 67 |
1 files changed, 38 insertions, 29 deletions
diff --git a/android/testing.go b/android/testing.go index 6c4f4f8de..8e38b3b1c 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 @@ -924,7 +933,7 @@ func (b baseTestingComponent) maybeBuildParamsFromRule(rule string) (TestingBuil func (b baseTestingComponent) buildParamsFromRule(rule string) TestingBuildParams { 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 } @@ -943,7 +952,7 @@ func (b baseTestingComponent) maybeBuildParamsFromDescription(desc string) (Test func (b baseTestingComponent) buildParamsFromDescription(desc string) TestingBuildParams { 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 } @@ -976,8 +985,8 @@ func (b baseTestingComponent) maybeBuildParamsFromOutput(file string) (TestingBu func (b baseTestingComponent) buildParamsFromOutput(file string) TestingBuildParams { 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 } @@ -1040,9 +1049,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, } } |