AndroidPackageParsingTestBase: Re-throw failures in setUpPackages in individual tests

Throwing in an @BeforeClass method breaks our test runner, due to
not reporting any results for the tests therein, leading to a difference
between actual and expected test counts.

The tool failure also obscured the fact that these tests
were already failing.

Bug: 153058196
Test: atest AndroidPackageInfoFlagBehaviorTest AndroidPackageParsingEquivalenceTest
Change-Id: Id0c1ebe2c99ff58451c1a1ccc20c6f9e355a725f
This commit is contained in:
Adrian Roos
2020-04-02 15:12:37 +02:00
parent f0059d21c7
commit b1d3bf70cd

View File

@@ -91,24 +91,35 @@ open class AndroidPackageParsingTestBase {
lateinit var newPackages: List<AndroidPackage>
var failureInBeforeClass: Throwable? = null
@Suppress("ConstantConditionIf")
@JvmStatic
@BeforeClass
fun setUpPackages() {
this.oldPackages = apks.map {
packageParser.parsePackage(it, PackageParser.PARSE_IS_SYSTEM_DIR, false)
}
failureInBeforeClass = null
try {
this.oldPackages = apks.map {
packageParser.parsePackage(it, PackageParser.PARSE_IS_SYSTEM_DIR, false)
}
this.newPackages = apks.map {
packageParser2.parsePackage(it, PackageParser.PARSE_IS_SYSTEM_DIR, false)
}
this.newPackages = apks.map {
packageParser2.parsePackage(it, PackageParser.PARSE_IS_SYSTEM_DIR, false)
}
if (DUMP_HPROF_TO_EXTERNAL) {
System.gc()
Environment.getExternalStorageDirectory()
.resolve("${AndroidPackageParsingTestBase::class.java.simpleName}.hprof")
.absolutePath
.run(Debug::dumpHprofData)
if (DUMP_HPROF_TO_EXTERNAL) {
System.gc()
Environment.getExternalStorageDirectory()
.resolve(
"${AndroidPackageParsingTestBase::class.java.simpleName}.hprof")
.absolutePath
.run(Debug::dumpHprofData)
}
} catch (t: Throwable) {
// If we crash here we cause a tool failure (because we don't run any of the tests
// in the subclasses, leading to a difference between expected and actual test
// result counts).
failureInBeforeClass = t
}
}
@@ -137,6 +148,13 @@ open class AndroidPackageParsingTestBase {
}
}
@org.junit.Before
fun verifySetUpPackages() {
failureInBeforeClass?.let {
throw AssertionError("setUpPackages failed:", it)
}
}
// The following methods dump an exact set of fields from the object to compare, because
// 1. comprehensive equals/toStrings do not exist on all of the Info objects, and
// 2. the test must only verify fields that [PackageParser.Package] can actually fill, as