Loading android/neverallow.go +17 −1 Original line number Diff line number Diff line Loading @@ -186,7 +186,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 Loading Loading @@ -560,3 +560,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 }) } android/neverallow_test.go +22 −12 Original line number Diff line number Diff line Loading @@ -20,16 +20,19 @@ import ( "github.com/google/blueprint" ) func init() { // Add extra rules needed for testing. AddNeverAllowRules( NeverAllow().InDirectDeps("not_allowed_in_direct_deps"), ) } var neverallowTests = []struct { // The name of the test. name string // Optional test specific rules. If specified then they are used instead of the default rules. rules []Rule // Additional contents to add to the virtual filesystem used by the tests. fs map[string][]byte // The expected error patterns. If empty then no errors are expected, otherwise each error // reported must be matched by at least one of these patterns. A pattern matches if the error // message contains the pattern. A pattern does not have to match the whole error message. expectedErrors []string }{ // Test General Functionality Loading @@ -37,6 +40,9 @@ var neverallowTests = []struct { // in direct deps tests { name: "not_allowed_in_direct_deps", rules: []Rule{ NeverAllow().InDirectDeps("not_allowed_in_direct_deps"), }, fs: map[string][]byte{ "top/Blueprints": []byte(` cc_library { Loading @@ -53,7 +59,7 @@ var neverallowTests = []struct { }, }, // Test specific rules // Test android specific rules // include_dir rule tests { Loading Loading @@ -246,11 +252,15 @@ var neverallowTests = []struct { } func TestNeverallow(t *testing.T) { config := TestConfig(buildDir, nil) for _, test := range neverallowTests { // Create a test per config to allow for test specific config, e.g. test rules. config := TestConfig(buildDir, nil) t.Run(test.name, func(t *testing.T) { // If the test has its own rules then use them instead of the default ones. if test.rules != nil { setTestNeverallowRules(config, test.rules) } _, errs := testNeverallow(config, test.fs) CheckErrorsAgainstExpectations(t, errs, test.expectedErrors) }) Loading Loading
android/neverallow.go +17 −1 Original line number Diff line number Diff line Loading @@ -186,7 +186,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 Loading Loading @@ -560,3 +560,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 }) }
android/neverallow_test.go +22 −12 Original line number Diff line number Diff line Loading @@ -20,16 +20,19 @@ import ( "github.com/google/blueprint" ) func init() { // Add extra rules needed for testing. AddNeverAllowRules( NeverAllow().InDirectDeps("not_allowed_in_direct_deps"), ) } var neverallowTests = []struct { // The name of the test. name string // Optional test specific rules. If specified then they are used instead of the default rules. rules []Rule // Additional contents to add to the virtual filesystem used by the tests. fs map[string][]byte // The expected error patterns. If empty then no errors are expected, otherwise each error // reported must be matched by at least one of these patterns. A pattern matches if the error // message contains the pattern. A pattern does not have to match the whole error message. expectedErrors []string }{ // Test General Functionality Loading @@ -37,6 +40,9 @@ var neverallowTests = []struct { // in direct deps tests { name: "not_allowed_in_direct_deps", rules: []Rule{ NeverAllow().InDirectDeps("not_allowed_in_direct_deps"), }, fs: map[string][]byte{ "top/Blueprints": []byte(` cc_library { Loading @@ -53,7 +59,7 @@ var neverallowTests = []struct { }, }, // Test specific rules // Test android specific rules // include_dir rule tests { Loading Loading @@ -246,11 +252,15 @@ var neverallowTests = []struct { } func TestNeverallow(t *testing.T) { config := TestConfig(buildDir, nil) for _, test := range neverallowTests { // Create a test per config to allow for test specific config, e.g. test rules. config := TestConfig(buildDir, nil) t.Run(test.name, func(t *testing.T) { // If the test has its own rules then use them instead of the default ones. if test.rules != nil { setTestNeverallowRules(config, test.rules) } _, errs := testNeverallow(config, test.fs) CheckErrorsAgainstExpectations(t, errs, test.expectedErrors) }) Loading