Convert TestApexPermittedPackagesRules to use test fixtures

Adds PrepareForTestWithNeverallowRules to make it easy to test
neverallow rules. Avoid exporting any unnecessary neverallow related
methods from the android package.

Bug: 181070625
Test: m nothing
Change-Id: Idfc6955cb23f1a4d1790be7879388154b03f3980
This commit is contained in:
Paul Duffin
2021-03-30 23:07:52 +01:00
parent 4f6d15465b
commit 45338f05e3
4 changed files with 40 additions and 42 deletions

View File

@@ -42,7 +42,7 @@ import (
// counts as a match
// - it has none of the "Without" properties matched (same rules as above)
func RegisterNeverallowMutator(ctx RegisterMutatorsContext) {
func registerNeverallowMutator(ctx RegisterMutatorsContext) {
ctx.BottomUp("neverallow", neverallowMutator).Parallel()
}
@@ -661,6 +661,22 @@ func neverallowRules(config Config) []Rule {
// Overrides the default neverallow rules for the supplied config.
//
// For testing only.
func SetTestNeverallowRules(config Config, testRules []Rule) {
func setTestNeverallowRules(config Config, testRules []Rule) {
config.Once(neverallowRulesKey, func() interface{} { return testRules })
}
// Prepares for a test by setting neverallow rules and enabling the mutator.
//
// If the supplied rules are nil then the default rules are used.
func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer {
return GroupFixturePreparers(
FixtureModifyConfig(func(config Config) {
if testRules != nil {
setTestNeverallowRules(config, testRules)
}
}),
FixtureRegisterWithContext(func(ctx RegistrationContext) {
ctx.PostDepsMutators(registerNeverallowMutator)
}),
)
}