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:
@@ -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 })
|
||||
}
|
||||
|
Reference in New Issue
Block a user