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
diff --git a/android/all_teams_test.go b/android/all_teams_test.go
index fa8c048..3b200f6 100644
--- a/android/all_teams_test.go
+++ b/android/all_teams_test.go
@@ -131,7 +131,7 @@
func getTeamProtoOutput(t *testing.T, ctx *TestResult) *team_proto.AllTeams {
teams := new(team_proto.AllTeams)
- config := ctx.SingletonForTests("all_teams")
+ config := ctx.SingletonForTests(t, "all_teams")
allOutputs := config.AllOutputs()
protoPath := allOutputs[0]
diff --git a/android/androidmk_test.go b/android/androidmk_test.go
index f63b227..0a81fb8 100644
--- a/android/androidmk_test.go
+++ b/android/androidmk_test.go
@@ -144,7 +144,7 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- module := result.ModuleForTests("foo", "").Module().(*customModule)
+ module := result.ModuleForTests(t, "foo", "").Module().(*customModule)
return result.TestContext, module
}
diff --git a/android/arch_test.go b/android/arch_test.go
index 7914884..adb655f 100644
--- a/android/arch_test.go
+++ b/android/arch_test.go
@@ -432,7 +432,7 @@
var ret []string
variants := ctx.ModuleVariantsForTests(name)
for _, variant := range variants {
- m := ctx.ModuleForTests(name, variant)
+ m := ctx.ModuleForTests(t, name, variant)
if m.Module().Enabled(PanickingConfigAndErrorContext(ctx)) {
ret = append(ret, variant)
}
@@ -442,7 +442,7 @@
moduleMultiTargets := func(ctx *TestContext, name string, variant string) []string {
var ret []string
- targets := ctx.ModuleForTests(name, variant).Module().MultiTargets()
+ targets := ctx.ModuleForTests(t, name, variant).Module().MultiTargets()
for _, t := range targets {
ret = append(ret, t.String())
}
@@ -546,7 +546,7 @@
var ret []string
variants := ctx.ModuleVariantsForTests(name)
for _, variant := range variants {
- m := ctx.ModuleForTests(name, variant)
+ m := ctx.ModuleForTests(t, name, variant)
if m.Module().Enabled(PanickingConfigAndErrorContext(ctx)) {
ret = append(ret, variant)
}
@@ -758,7 +758,7 @@
for _, want := range tt.results {
t.Run(want.module+"_"+want.variant, func(t *testing.T) {
- got := result.ModuleForTests(want.module, want.variant).Module().(*testArchPropertiesModule).properties.A
+ got := result.ModuleForTests(t, want.module, want.variant).Module().(*testArchPropertiesModule).properties.A
AssertArrayString(t, "arch mutator property", want.property, got)
})
}
diff --git a/android/build_prop_test.go b/android/build_prop_test.go
index e75975a..e136a1a 100644
--- a/android/build_prop_test.go
+++ b/android/build_prop_test.go
@@ -36,6 +36,6 @@
res := GroupFixturePreparers(
FixtureRegisterWithContext(registerBuildPropComponents),
).RunTestWithBp(t, bp)
- buildPropCmd := res.ModuleForTests("vendor-build.prop", "").Rule("vendor-build.prop_.vendor-build.prop").RuleParams.Command
+ buildPropCmd := res.ModuleForTests(t, "vendor-build.prop", "").Rule("vendor-build.prop_.vendor-build.prop").RuleParams.Command
AssertStringDoesContain(t, "Could not find android-info in prop files of vendor build.prop", buildPropCmd, "--prop-files=out/soong/.intermediates/board-info/android-info.prop")
}
diff --git a/android/csuite_config_test.go b/android/csuite_config_test.go
index b8a176e..7e25aac 100644
--- a/android/csuite_config_test.go
+++ b/android/csuite_config_test.go
@@ -32,7 +32,6 @@
if len(variants) > 1 {
t.Errorf("expected 1, got %d", len(variants))
}
- outputFilename := result.ModuleForTests(
- "plain", variants[0]).Module().(*CSuiteConfig).OutputFilePath.Base()
+ outputFilename := result.ModuleForTests(t, "plain", variants[0]).Module().(*CSuiteConfig).OutputFilePath.Base()
AssertStringEquals(t, "output file name", "plain", outputFilename)
}
diff --git a/android/defaults_test.go b/android/defaults_test.go
index 0ad0fb8..24f1461 100644
--- a/android/defaults_test.go
+++ b/android/defaults_test.go
@@ -123,8 +123,8 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out")
- missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out")
+ missingDefaults := result.ModuleForTests(t, "missing_defaults", "").Output("out")
+ missingTransitiveDefaults := result.ModuleForTests(t, "missing_transitive_defaults", "").Output("out")
AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule)
diff --git a/android/deptag_test.go b/android/deptag_test.go
index eb4fa89..9037871 100644
--- a/android/deptag_test.go
+++ b/android/deptag_test.go
@@ -90,10 +90,10 @@
config := result.Config
- hostFoo := result.ModuleForTests("foo", config.BuildOSCommonTarget.String()).Description("install")
- hostInstallDep := result.ModuleForTests("install_dep", config.BuildOSCommonTarget.String()).Description("install")
- hostTransitive := result.ModuleForTests("transitive", config.BuildOSCommonTarget.String()).Description("install")
- hostDep := result.ModuleForTests("dep", config.BuildOSCommonTarget.String()).Description("install")
+ hostFoo := result.ModuleForTests(t, "foo", config.BuildOSCommonTarget.String()).Description("install")
+ hostInstallDep := result.ModuleForTests(t, "install_dep", config.BuildOSCommonTarget.String()).Description("install")
+ hostTransitive := result.ModuleForTests(t, "transitive", config.BuildOSCommonTarget.String()).Description("install")
+ hostDep := result.ModuleForTests(t, "dep", config.BuildOSCommonTarget.String()).Description("install")
if g, w := hostFoo.Implicits.Strings(), hostInstallDep.Output.String(); !InList(w, g) {
t.Errorf("expected host dependency %q, got %q", w, g)
@@ -111,10 +111,10 @@
t.Errorf("expected no host dependency %q, got %q", w, g)
}
- deviceFoo := result.ModuleForTests("foo", "android_common").Description("install")
- deviceInstallDep := result.ModuleForTests("install_dep", "android_common").Description("install")
- deviceTransitive := result.ModuleForTests("transitive", "android_common").Description("install")
- deviceDep := result.ModuleForTests("dep", "android_common").Description("install")
+ deviceFoo := result.ModuleForTests(t, "foo", "android_common").Description("install")
+ deviceInstallDep := result.ModuleForTests(t, "install_dep", "android_common").Description("install")
+ deviceTransitive := result.ModuleForTests(t, "transitive", "android_common").Description("install")
+ deviceDep := result.ModuleForTests(t, "dep", "android_common").Description("install")
if g, w := deviceFoo.OrderOnly.Strings(), deviceInstallDep.Output.String(); !InList(w, g) {
t.Errorf("expected device dependency %q, got %q", w, g)
diff --git a/android/fixture.go b/android/fixture.go
index 5ad47e8..ea52b95 100644
--- a/android/fixture.go
+++ b/android/fixture.go
@@ -1048,7 +1048,7 @@
// Module returns the module with the specific name and of the specified variant.
func (r *TestResult) Module(name string, variant string) Module {
- return r.ModuleForTests(name, variant).Module()
+ return r.ModuleForTests(r.fixture.t, name, variant).Module()
}
// CollateErrs adds additional errors to the result and returns true if there is more than one
diff --git a/android/makevars_test.go b/android/makevars_test.go
index 95e4b59..387d457 100644
--- a/android/makevars_test.go
+++ b/android/makevars_test.go
@@ -23,7 +23,7 @@
}
`)
- lateContents := string(result.SingletonForTests("makevars").Singleton().(*makeVarsSingleton).lateForTesting)
+ lateContents := string(result.SingletonForTests(t, "makevars").Singleton().(*makeVarsSingleton).lateForTesting)
matched, err := regexp.MatchString(`call dist-for-goals,my_goal,.*/my_file.txt:my_file.txt\)`, lateContents)
if err != nil || !matched {
t.Fatalf("Expected a dist of my_file.txt, but got: %s", lateContents)
diff --git a/android/module_test.go b/android/module_test.go
index 3b81ded..5331e49 100644
--- a/android/module_test.go
+++ b/android/module_test.go
@@ -321,7 +321,7 @@
if host {
variant = result.Config.BuildOSCommonTarget.String()
}
- return result.ModuleForTests(name, variant)
+ return result.ModuleForTests(t, name, variant)
}
outputRule := func(name string) TestingBuildParams { return module(name, false).Output(name) }
@@ -434,11 +434,12 @@
rules := result.InstallMakeRulesForTesting(t)
module := func(name string, host bool) TestingModule {
+ t.Helper()
variant := "android_common"
if host {
variant = result.Config.BuildOSCommonTarget.String()
}
- return result.ModuleForTests(name, variant)
+ return result.ModuleForTests(t, name, variant)
}
outputRule := func(name string) TestingBuildParams { return module(name, false).Output(name) }
@@ -743,7 +744,7 @@
FixtureWithRootAndroidBp(tc.bp),
).RunTest(t)
- foo := result.ModuleForTests("foo", "").Module().base()
+ foo := result.ModuleForTests(t, "foo", "").Module().base()
AssertDeepEquals(t, "foo ", tc.expectedProps, foo.propertiesWithValues())
})
@@ -1078,7 +1079,7 @@
PathContext: PathContextForTesting(config),
OtherModuleProviderContext: result.TestContext.OtherModuleProviderAdaptor(),
}
- got := OutputFileForModule(ctx, result.ModuleForTests("test_module", "").Module(), tt.tag)
+ got := OutputFileForModule(ctx, result.ModuleForTests(t, "test_module", "").Module(), tt.tag)
AssertPathRelativeToTopEquals(t, "expected output path", tt.expected, got)
AssertArrayString(t, "expected missing deps", tt.missingDeps, ctx.missingDeps)
})
diff --git a/android/mutator_test.go b/android/mutator_test.go
index 60a6119..f421b82 100644
--- a/android/mutator_test.go
+++ b/android/mutator_test.go
@@ -78,7 +78,7 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- foo := result.ModuleForTests("foo", "").Module().(*mutatorTestModule)
+ foo := result.ModuleForTests(t, "foo", "").Module().(*mutatorTestModule)
AssertDeepEquals(t, "foo missing deps", []string{"added_missing_dep", "regular_missing_dep"}, foo.missingDeps)
}
diff --git a/android/namespace_test.go b/android/namespace_test.go
index 0327e78..a183bbf 100644
--- a/android/namespace_test.go
+++ b/android/namespace_test.go
@@ -683,7 +683,7 @@
}
func getModule(result *TestResult, moduleName string) TestingModule {
- return result.ModuleForTests(moduleName, "")
+ return result.ModuleForTests(result.fixture.t, moduleName, "")
}
func findModuleById(result *TestResult, id string) (module TestingModule) {
@@ -691,7 +691,7 @@
testModule, ok := candidate.(*testModule)
if ok {
if testModule.properties.Id == id {
- module = newTestingModule(result.config, testModule)
+ module = newTestingModule(result.fixture.t, result.config, testModule)
}
}
}
diff --git a/android/paths_test.go b/android/paths_test.go
index 20beecc..b125c4e 100644
--- a/android/paths_test.go
+++ b/android/paths_test.go
@@ -1269,7 +1269,7 @@
ExtendWithErrorHandler(errorHandler).
RunTest(t)
- m := result.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
+ m := result.ModuleForTests(t, "foo", "").Module().(*pathForModuleSrcTestModule)
AssertStringPathsRelativeToTopEquals(t, "srcs", result.Config, test.srcs, m.srcs)
AssertStringPathsRelativeToTopEquals(t, "rels", result.Config, test.rels, m.rels)
@@ -1533,13 +1533,13 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- foo := result.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
+ foo := result.ModuleForTests(t, "foo", "").Module().(*pathForModuleSrcTestModule)
AssertArrayString(t, "foo missing deps", []string{"a", "b", "c"}, foo.missingDeps)
AssertArrayString(t, "foo srcs", []string{}, foo.srcs)
AssertStringEquals(t, "foo src", "", foo.src)
- bar := result.ModuleForTests("bar", "").Module().(*pathForModuleSrcTestModule)
+ bar := result.ModuleForTests(t, "bar", "").Module().(*pathForModuleSrcTestModule)
AssertArrayString(t, "bar missing deps", []string{"d", "e"}, bar.missingDeps)
AssertArrayString(t, "bar srcs", []string{}, bar.srcs)
diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go
index b90ef3b..27a68fb 100644
--- a/android/prebuilt_test.go
+++ b/android/prebuilt_test.go
@@ -335,7 +335,7 @@
).RunTestWithBp(t, bp)
for _, variant := range result.ModuleVariantsForTests("foo") {
- foo := result.ModuleForTests("foo", variant)
+ foo := result.ModuleForTests(t, "foo", variant)
t.Run(foo.Module().Target().Os.String(), func(t *testing.T) {
var dependsOnSourceModule, dependsOnPrebuiltModule bool
result.VisitDirectDeps(foo.Module(), func(m blueprint.Module) {
diff --git a/android/rule_builder_test.go b/android/rule_builder_test.go
index 566bafa..5f3b9be 100644
--- a/android/rule_builder_test.go
+++ b/android/rule_builder_test.go
@@ -651,7 +651,7 @@
outFile := "out/soong/.intermediates/foo/gen/foo"
rspFile := "out/soong/.intermediates/foo/rsp"
rspFile2 := "out/soong/.intermediates/foo/rsp2"
- module := result.ModuleForTests("foo", "")
+ module := result.ModuleForTests(t, "foo", "")
check(t, module.Rule("rule"), module.Output(rspFile2),
"cp in "+outFile+" @"+rspFile+" @"+rspFile2,
outFile, outFile+".d", rspFile, rspFile2, true, nil, nil)
@@ -668,7 +668,7 @@
sandboxPath := shared.TempDirForOutDir("out/soong")
cmd := sbox + ` --sandbox-path ` + sandboxPath + ` --output-dir ` + sboxOutDir + ` --manifest ` + manifest
- module := result.ModuleForTests("foo_sbox", "")
+ module := result.ModuleForTests(t, "foo_sbox", "")
check(t, module.Output("gen/foo_sbox"), module.Output(rspFile2),
cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox})
})
@@ -685,7 +685,7 @@
cmd := sbox + ` --sandbox-path ` + sandboxPath + ` --output-dir ` + sboxOutDir + ` --manifest ` + manifest
- module := result.ModuleForTests("foo_sbox_inputs", "")
+ module := result.ModuleForTests(t, "foo_sbox_inputs", "")
check(t, module.Output("gen/foo_sbox_inputs"), module.Output(rspFile2),
cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox})
})
@@ -693,7 +693,7 @@
outFile := filepath.Join("out/soong/singleton/gen/baz")
rspFile := filepath.Join("out/soong/singleton/rsp")
rspFile2 := filepath.Join("out/soong/singleton/rsp2")
- singleton := result.SingletonForTests("rule_builder_test")
+ singleton := result.SingletonForTests(t, "rule_builder_test")
check(t, singleton.Rule("rule"), singleton.Output(rspFile2),
"cp in "+outFile+" @"+rspFile+" @"+rspFile2,
outFile, outFile+".d", rspFile, rspFile2, true, nil, nil)
@@ -756,14 +756,14 @@
for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
t.Run("sbox", func(t *testing.T) {
- gen := result.ModuleForTests(test.name+"_sbox", "")
+ gen := result.ModuleForTests(t, test.name+"_sbox", "")
manifest := RuleBuilderSboxProtoForTests(t, result.TestContext, gen.Output("sbox.textproto"))
hash := manifest.Commands[0].GetInputHash()
AssertStringEquals(t, "hash", test.expectedHash, hash)
})
t.Run("", func(t *testing.T) {
- gen := result.ModuleForTests(test.name+"", "")
+ gen := result.ModuleForTests(t, test.name+"", "")
command := gen.Output("gen/" + test.name).RuleParams.Command
if g, w := command, " # hash of input list: "+test.expectedHash; !strings.HasSuffix(g, w) {
t.Errorf("Expected command line to end with %q, got %q", w, g)
diff --git a/android/selects_test.go b/android/selects_test.go
index 1397ed8..7f20a3d 100644
--- a/android/selects_test.go
+++ b/android/selects_test.go
@@ -1118,7 +1118,7 @@
for moduleName := range tc.providers {
expected := tc.providers[moduleName]
- m := result.ModuleForTests(moduleName, "android_arm64_armv8-a")
+ m := result.ModuleForTests(t, moduleName, "android_arm64_armv8-a")
p, _ := OtherModuleProvider(result.testContext.OtherModuleProviderAdaptor(), m.Module(), selectsTestProviderKey)
if !reflect.DeepEqual(p, expected) {
t.Errorf("Expected:\n %q\ngot:\n %q", expected.String(), p.String())
diff --git a/android/singleton_module_test.go b/android/singleton_module_test.go
index 3b8c6b2..6f61a3b 100644
--- a/android/singleton_module_test.go
+++ b/android/singleton_module_test.go
@@ -61,7 +61,7 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- ops := result.ModuleForTests("test_singleton_module", "").Module().(*testSingletonModule).ops
+ ops := result.ModuleForTests(t, "test_singleton_module", "").Module().(*testSingletonModule).ops
wantOps := []string{"GenerateAndroidBuildActions", "GenerateSingletonBuildActions", "MakeVars"}
AssertDeepEquals(t, "operations", wantOps, ops)
}
@@ -88,7 +88,7 @@
prepareForSingletonModuleTest,
).RunTest(t)
- singleton := result.SingletonForTests("test_singleton_module").Singleton()
+ singleton := result.SingletonForTests(t, "test_singleton_module").Singleton()
sm := singleton.(*singletonModuleSingletonAdaptor).sm
ops := sm.(*testSingletonModule).ops
if ops != nil {
diff --git a/android/soong_config_modules_test.go b/android/soong_config_modules_test.go
index 04aafde..f98e02b 100644
--- a/android/soong_config_modules_test.go
+++ b/android/soong_config_modules_test.go
@@ -321,10 +321,10 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- foo := result.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
+ foo := result.ModuleForTests(t, "foo", "").Module().(*soongConfigTestModule)
AssertDeepEquals(t, "foo cflags", tc.fooExpectedFlags, foo.props.Cflags)
- fooDefaults := result.ModuleForTests("foo_with_defaults", "").Module().(*soongConfigTestModule)
+ fooDefaults := result.ModuleForTests(t, "foo_with_defaults", "").Module().(*soongConfigTestModule)
AssertDeepEquals(t, "foo_with_defaults cflags", tc.fooDefaultsExpectedFlags, fooDefaults.props.Cflags)
})
}
@@ -499,8 +499,8 @@
).RunTest(t)
// Make sure that the singleton was created.
- result.SingletonForTests("test_singleton")
- m := result.ModuleForTests("wiley", "").module.(*soongConfigTestSingletonModule)
+ result.SingletonForTests(t, "test_singleton")
+ m := result.ModuleForTests(t, "wiley", "").module.(*soongConfigTestSingletonModule)
AssertStringEquals(t, "fragments", test.expectedFragments, fmt.Sprintf("%+v", m.props.Fragments))
})
}
diff --git a/android/team_test.go b/android/team_test.go
index ccfcaaa..dcc1c99 100644
--- a/android/team_test.go
+++ b/android/team_test.go
@@ -61,9 +61,9 @@
`)
// Assert the rule from GenerateAndroidBuildActions exists.
- m := ctx.ModuleForTests("main_test", "")
+ m := ctx.ModuleForTests(t, "main_test", "")
AssertStringEquals(t, "msg", m.Module().base().Team(), "someteam")
- m = ctx.ModuleForTests("tool", "")
+ m = ctx.ModuleForTests(t, "tool", "")
AssertStringEquals(t, "msg", m.Module().base().Team(), "team2")
}
diff --git a/android/test_suites_test.go b/android/test_suites_test.go
index db9a34d..bf4de19 100644
--- a/android/test_suites_test.go
+++ b/android/test_suites_test.go
@@ -52,7 +52,7 @@
}
`)
- config := ctx.SingletonForTests("testsuites")
+ config := ctx.SingletonForTests(t, "testsuites")
allOutputs := config.AllOutputs()
wantContents := map[string]string{
diff --git a/android/testing.go b/android/testing.go
index 6c4f4f8..8e38b3b 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -530,7 +530,8 @@
// 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 @@
})
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 @@
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 @@
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 @@
}
// 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 @@
}
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 (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 @@
//
// 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 @@
// 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) 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) 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) 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 @@
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,
}
}
diff --git a/android/variable_test.go b/android/variable_test.go
index 73dc052..1d928f2 100644
--- a/android/variable_test.go
+++ b/android/variable_test.go
@@ -299,7 +299,7 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- foo := result.ModuleForTests("foo", "").Module().(*productVariablesDefaultsTestModule)
+ foo := result.ModuleForTests(t, "foo", "").Module().(*productVariablesDefaultsTestModule)
want := []string{"defaults", "module", "product_variable_defaults", "product_variable_module"}
AssertDeepEquals(t, "foo", want, foo.properties.Foo)
@@ -360,7 +360,7 @@
FixtureWithRootAndroidBp(bp),
).RunTest(t)
- foo := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*productVariablesDefaultsTestModule)
+ foo := result.ModuleForTests(t, "foo", "android_arm64_armv8-a").Module().(*productVariablesDefaultsTestModule)
want := []string{"module", "arm64"}
AssertDeepEquals(t, "foo", want, foo.properties.Foo)
diff --git a/android/vintf_fragment_test.go b/android/vintf_fragment_test.go
index cb038f5..7f0078c 100644
--- a/android/vintf_fragment_test.go
+++ b/android/vintf_fragment_test.go
@@ -29,7 +29,7 @@
testResult := PrepareForTestWithAndroidBuildComponents.RunTestWithBp(t, bp)
- vintfFragmentBuild := testResult.TestContext.ModuleForTests("test_vintf_fragment", "android_common").Rule("assemble_vintf")
+ vintfFragmentBuild := testResult.TestContext.ModuleForTests(t, "test_vintf_fragment", "android_common").Rule("assemble_vintf")
if !strings.Contains(vintfFragmentBuild.RuleParams.Command, "assemble_vintf") {
t.Error("Vintf_manifest build command does not process with assemble_vintf : " + vintfFragmentBuild.RuleParams.Command)
}