From 90607e9056f6ff4cec2447fdd7a8b252d67ffde7 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Tue, 11 Feb 2025 14:58:07 -0800 Subject: 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 --- java/sdk_library_test.go | 82 ++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 41 deletions(-) (limited to 'java/sdk_library_test.go') diff --git a/java/sdk_library_test.go b/java/sdk_library_test.go index 0d872f008..2cb827dc2 100644 --- a/java/sdk_library_test.go +++ b/java/sdk_library_test.go @@ -115,19 +115,19 @@ func TestJavaSdkLibrary(t *testing.T) { `) // check the existence of the internal modules - foo := result.ModuleForTests("foo", "android_common") - result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common") - result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common") - result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common") - result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common") - result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common") - result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common") - result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo")+".api.contribution", "") - result.ModuleForTests(apiScopePublic.apiLibraryModuleName("foo"), "android_common") - result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common") - result.ModuleForTests("foo.api.public.28", "") - result.ModuleForTests("foo.api.system.28", "") - result.ModuleForTests("foo.api.test.28", "") + foo := result.ModuleForTests(t, "foo", "android_common") + result.ModuleForTests(t, apiScopePublic.stubsLibraryModuleName("foo"), "android_common") + result.ModuleForTests(t, apiScopeSystem.stubsLibraryModuleName("foo"), "android_common") + result.ModuleForTests(t, apiScopeTest.stubsLibraryModuleName("foo"), "android_common") + result.ModuleForTests(t, apiScopePublic.stubsSourceModuleName("foo"), "android_common") + result.ModuleForTests(t, apiScopeSystem.stubsSourceModuleName("foo"), "android_common") + result.ModuleForTests(t, apiScopeTest.stubsSourceModuleName("foo"), "android_common") + result.ModuleForTests(t, apiScopePublic.stubsSourceModuleName("foo")+".api.contribution", "") + result.ModuleForTests(t, apiScopePublic.apiLibraryModuleName("foo"), "android_common") + result.ModuleForTests(t, "foo"+sdkXmlFileSuffix, "android_common") + result.ModuleForTests(t, "foo.api.public.28", "") + result.ModuleForTests(t, "foo.api.system.28", "") + result.ModuleForTests(t, "foo.api.test.28", "") exportedComponentsInfo, _ := android.OtherModuleProvider(result, foo.Module(), android.ExportedComponentsInfoProvider) expectedFooExportedComponents := []string{ @@ -147,7 +147,7 @@ func TestJavaSdkLibrary(t *testing.T) { } android.AssertArrayString(t, "foo exported components", expectedFooExportedComponents, exportedComponentsInfo.Components) - bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac") + bazJavac := result.ModuleForTests(t, "baz", "android_common").Rule("javac") // tests if baz is actually linked to the stubs lib android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar") // ... and not to the impl lib @@ -155,20 +155,20 @@ func TestJavaSdkLibrary(t *testing.T) { // test if baz is not linked to the system variant of foo android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar") - bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac") + bazTestJavac := result.ModuleForTests(t, "baz-test", "android_common").Rule("javac") // tests if baz-test is actually linked to the test stubs lib android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar") - baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac") + baz29Javac := result.ModuleForTests(t, "baz-29", "android_common").Rule("javac") // tests if baz-29 is actually linked to the system 29 stubs lib android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/sdk_system_29_foo/android_common/combined/sdk_system_29_foo.jar") - bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac") + bazModule30Javac := result.ModuleForTests(t, "baz-module-30", "android_common").Rule("javac") // tests if "baz-module-30" is actually linked to the module 30 stubs lib android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/sdk_module-lib_30_foo/android_common/combined/sdk_module-lib_30_foo.jar") // test if baz has exported SDK lib names foo and bar to qux - qux := result.ModuleForTests("qux", "android_common") + qux := result.ModuleForTests(t, "qux", "android_common") if quxLib, ok := qux.Module().(*Library); ok { requiredSdkLibs, optionalSdkLibs := quxLib.ClassLoaderContexts().UsesLibs() android.AssertDeepEquals(t, "qux exports (required)", []string{"fred", "quuz", "foo", "bar"}, requiredSdkLibs) @@ -176,13 +176,13 @@ func TestJavaSdkLibrary(t *testing.T) { } // test if quuz have created the api_contribution module - result.ModuleForTests(apiScopePublic.stubsSourceModuleName("quuz")+".api.contribution", "") + result.ModuleForTests(t, apiScopePublic.stubsSourceModuleName("quuz")+".api.contribution", "") - fooImplDexJar := result.ModuleForTests("foo.impl", "android_common").Rule("d8") + fooImplDexJar := result.ModuleForTests(t, "foo.impl", "android_common").Rule("d8") // tests if kotlinc generated files are NOT excluded from output of foo.impl. android.AssertStringDoesNotContain(t, "foo.impl dex", fooImplDexJar.BuildParams.Args["mergeZipsFlags"], "-stripFile META-INF/*.kotlin_module") - barImplDexJar := result.ModuleForTests("bar.impl", "android_common").Rule("d8") + barImplDexJar := result.ModuleForTests(t, "bar.impl", "android_common").Rule("d8") // tests if kotlinc generated files are excluded from output of bar.impl. android.AssertStringDoesContain(t, "bar.impl dex", barImplDexJar.BuildParams.Args["mergeZipsFlags"], "-stripFile META-INF/*.kotlin_module") } @@ -220,7 +220,7 @@ func TestJavaSdkLibrary_UpdatableLibrary(t *testing.T) { `) // test that updatability attributes are passed on correctly - fooUpdatable := result.ModuleForTests("fooUpdatable.xml", "android_common").Output("fooUpdatable.xml") + fooUpdatable := result.ModuleForTests(t, "fooUpdatable.xml", "android_common").Output("fooUpdatable.xml") fooUpdatableContents := android.ContentFromFileRuleForTests(t, result.TestContext, fooUpdatable) android.AssertStringDoesContain(t, "fooUpdatable.xml contents", fooUpdatableContents, `on-bootclasspath-since="U"`) android.AssertStringDoesContain(t, "fooUpdatable.xml contents", fooUpdatableContents, `on-bootclasspath-before="V"`) @@ -229,7 +229,7 @@ func TestJavaSdkLibrary_UpdatableLibrary(t *testing.T) { // double check that updatability attributes are not written if they don't exist in the bp file // the permissions file for the foo library defined above - fooPermissions := result.ModuleForTests("foo.xml", "android_common").Output("foo.xml") + fooPermissions := result.ModuleForTests(t, "foo.xml", "android_common").Output("foo.xml") fooPermissionsContents := android.ContentFromFileRuleForTests(t, result.TestContext, fooPermissions) android.AssertStringDoesNotContain(t, "foo.xml contents", fooPermissionsContents, `on-bootclasspath-since`) android.AssertStringDoesNotContain(t, "foo.xml contents", fooPermissionsContents, `on-bootclasspath-before`) @@ -370,7 +370,7 @@ func TestJavaSdkLibrary_UpdatableLibrary_usesNewTag(t *testing.T) { } `) // test that updatability attributes are passed on correctly - fooUpdatable := result.ModuleForTests("foo.xml", "android_common").Output("foo.xml") + fooUpdatable := result.ModuleForTests(t, "foo.xml", "android_common").Output("foo.xml") fooUpdatableContents := android.ContentFromFileRuleForTests(t, result.TestContext, fooUpdatable) android.AssertStringDoesContain(t, "foo.xml contents", fooUpdatableContents, ` module is . - barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac") + barLibrary := result.ModuleForTests(t, "bar", "android_common").Rule("javac") stubLibraries := []string{ stubsPath("foo-public", apiScopePublic), stubsPath("foo-system", apiScopeSystem), @@ -850,10 +850,10 @@ func TestJavaSdkLibraryImport(t *testing.T) { `) for _, scope := range []string{"", ".system", ".test"} { - fooModule := result.ModuleForTests("foo"+scope, "android_common") + fooModule := result.ModuleForTests(t, "foo"+scope, "android_common") javac := fooModule.Rule("javac") - sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Output("combined/sdklib.stubs" + scope + ".jar").Output + sdklibStubsJar := result.ModuleForTests(t, "sdklib.stubs"+scope, "android_common").Output("combined/sdklib.stubs" + scope + ".jar").Output android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String()) } @@ -999,7 +999,7 @@ func testJavaSdkLibraryImport_Preferred(t *testing.T, prefer string, preparer an // Make sure that dependencies on sdklib that resolve to one of the child libraries use the // prebuilt library. - public := result.ModuleForTests("public", "android_common") + public := result.ModuleForTests(t, "public", "android_common") rule := public.Output("javac/public.jar") inputs := rule.Implicits.Strings() expected := "out/soong/.intermediates/prebuilt_sdklib.stubs/android_common/combined/sdklib.stubs.jar" @@ -1119,7 +1119,7 @@ func TestSdkLibraryImport_MetadataModuleSupersedesPreferred(t *testing.T) { ).RunTestWithBp(t, bp) // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions - public := result.ModuleForTests("public", "android_common") + public := result.ModuleForTests(t, "public", "android_common") rule := public.Output("javac/public.jar") inputs := rule.Implicits.Strings() expectedInputs := []string{ @@ -1207,7 +1207,7 @@ func TestJavaSdkLibraryDist(t *testing.T) { for _, tt := range testCases { t.Run(tt.module, func(t *testing.T) { t.Parallel() - m := result.ModuleForTests(apiScopePublic.exportableStubsLibraryModuleName(tt.module), "android_common").Module().(*Library) + m := result.ModuleForTests(t, apiScopePublic.exportableStubsLibraryModuleName(tt.module), "android_common").Module().(*Library) dists := m.Dists() if len(dists) != 1 { t.Fatalf("expected exactly 1 dist entry, got %d", len(dists)) @@ -1330,7 +1330,7 @@ func TestJavaSdkLibrary_StubOnlyLibs_PassedToDroidstubs(t *testing.T) { `) // The foo.stubs.source should depend on bar-lib - fooStubsSources := result.ModuleForTests("foo.stubs.source", "android_common").Module().(*Droidstubs) + fooStubsSources := result.ModuleForTests(t, "foo.stubs.source", "android_common").Module().(*Droidstubs) eval := fooStubsSources.ConfigurableEvaluator(android.PanickingConfigAndErrorContext(result.TestContext)) android.AssertStringListContains(t, "foo stubs should depend on bar-lib", fooStubsSources.Javadoc.properties.Libs.GetOrDefault(eval, nil), "bar-lib") } @@ -1358,7 +1358,7 @@ func TestJavaSdkLibrary_Scope_Libs_PassedToDroidstubs(t *testing.T) { `) // The foo.stubs.source should depend on bar-lib - fooStubsSources := result.ModuleForTests("foo.stubs.source", "android_common").Module().(*Droidstubs) + fooStubsSources := result.ModuleForTests(t, "foo.stubs.source", "android_common").Module().(*Droidstubs) eval := fooStubsSources.ConfigurableEvaluator(android.PanickingConfigAndErrorContext(result.TestContext)) android.AssertStringListContains(t, "foo stubs should depend on bar-lib", fooStubsSources.Javadoc.properties.Libs.GetOrDefault(eval, nil), "bar-lib") } @@ -1409,7 +1409,7 @@ func TestJavaSdkLibrary_ApiLibrary(t *testing.T) { } for _, c := range testCases { - m := result.ModuleForTests(c.scope.apiLibraryModuleName("foo"), "android_common").Module().(*ApiLibrary) + m := result.ModuleForTests(t, c.scope.apiLibraryModuleName("foo"), "android_common").Module().(*ApiLibrary) android.AssertArrayString(t, "Module expected to contain api contributions", c.apiContributions, m.properties.Api_contributions) } } @@ -1472,7 +1472,7 @@ func TestSdkLibraryDependency(t *testing.T) { } `) - barPermissions := result.ModuleForTests("bar.xml", "android_common").Output("bar.xml") + barPermissions := result.ModuleForTests(t, "bar.xml", "android_common").Output("bar.xml") barContents := android.ContentFromFileRuleForTests(t, result.TestContext, barPermissions) android.AssertStringDoesContain(t, "bar.xml java_sdk_xml command", barContents, `dependency="foo"`) } @@ -1515,8 +1515,8 @@ func TestSdkLibraryExportableStubsLibrary(t *testing.T) { exportableSourceStubsLibraryModuleName := apiScopePublic.exportableSourceStubsLibraryModuleName("foo") // Check modules generation - result.ModuleForTests(exportableStubsLibraryModuleName, "android_common") - result.ModuleForTests(exportableSourceStubsLibraryModuleName, "android_common") + result.ModuleForTests(t, exportableStubsLibraryModuleName, "android_common") + result.ModuleForTests(t, exportableSourceStubsLibraryModuleName, "android_common") // Check static lib dependency android.AssertBoolEquals(t, "exportable top level stubs library module depends on the"+ @@ -1575,7 +1575,7 @@ func TestStubResolutionOfJavaSdkLibraryInLibs(t *testing.T) { result := fixture.RunTestWithBp(t, bp) // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions - public := result.ModuleForTests("mymodule", "android_common") + public := result.ModuleForTests(t, "mymodule", "android_common") rule := public.Output("javac/mymodule.jar") inputs := rule.Implicits.Strings() android.AssertStringListContains(t, "Could not find the expected stub on classpath", inputs, "out/soong/.intermediates/sdklib.stubs/android_common/turbine-combined/sdklib.stubs.jar") @@ -1661,7 +1661,7 @@ func TestMultipleSdkLibraryPrebuilts(t *testing.T) { result := fixture.RunTestWithBp(t, fmt.Sprintf(bp, tc.selectedDependencyName)) // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions - public := result.ModuleForTests("mymodule", "android_common") + public := result.ModuleForTests(t, "mymodule", "android_common") rule := public.Output("javac/mymodule.jar") inputs := rule.Implicits.Strings() android.AssertStringListContains(t, "Could not find the expected stub on classpath", inputs, tc.expectedStubPath) -- cgit v1.2.3-59-g8ed1b