Ensure flag restoration logic is always executed in SetFeatureFlagsRule.

`base.evaluate()` can be interrupted by other rules in the chain, such
as DevSdkIgnoreRule, if an assumption fails. This prevents
SetFeatureFlagsRule from executing its flag value restoration logic,
leading to an inconsistent state. This change ensures the restoration
logic runs regardless of exceptions.

Flag: EXEMPT mainline
Bug: 440618747
Test: TH
Change-Id: Iba4483b45f11aeed8ae565f8080b2bebd299046c
This commit is contained in:
Xiao Ma 2025-08-27 16:39:56 +09:00
commit bda151cd67

View file

@ -16,6 +16,7 @@
package com.android.testutils.com.android.testutils
import com.android.testutils.tryTest
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
@ -90,10 +91,12 @@ class SetFeatureFlagsRule(
// Execute the test method, which includes methods annotated with
// @Before, @Test and @After.
base.evaluate()
valuesToBeRestored.forEach {
setFlagsMethod(it.key, it.value)
tryTest {
base.evaluate()
} cleanup {
valuesToBeRestored.forEach {
setFlagsMethod(it.key, it.value)
}
}
}
}