Allow per test rules in neverallow_test.go

Makes testing individual rules easier by allowing them to be specified
per test rather than having to add them to the global defaults.

Bug: 138428610
Test: m nothing
Change-Id: Ic65a55dee2a02b6d33254753c047295dd5804408
This commit is contained in:
Paul Duffin
2019-08-07 15:31:07 +01:00
parent 91e3819335
commit 115445b5d6
2 changed files with 39 additions and 13 deletions

View File

@@ -177,7 +177,7 @@ func neverallowMutator(ctx BottomUpMutatorContext) {
osClass := ctx.Module().Target().Os.Class
for _, r := range neverallows {
for _, r := range neverallowRules(ctx.Config()) {
n := r.(*rule)
if !n.appliesToPath(dir) {
continue
@@ -551,3 +551,19 @@ func matchValue(value reflect.Value, check func(string) bool) bool {
panic("Can't handle type: " + value.Kind().String())
}
var neverallowRulesKey = NewOnceKey("neverallowRules")
func neverallowRules(config Config) []Rule {
return config.Once(neverallowRulesKey, func() interface{} {
// No test rules were set by setTestNeverallowRules, use the global rules
return neverallows
}).([]Rule)
}
// Overrides the default neverallow rules for the supplied config.
//
// For testing only.
func setTestNeverallowRules(config Config, testRules []Rule) {
config.Once(neverallowRulesKey, func() interface{} { return testRules })
}