@@ -95,11 +95,12 @@ func TestEvaluatedRuleTracker(t *testing.T) {
9595
9696func TestEvaluatedRuleLabelsScopes (t * testing.T ) {
9797 tests := []struct {
98- note string
99- module string
100- query string
101- input string
102- exp []map [string ]any
98+ note string
99+ module string
100+ modules map [string ]string
101+ query string
102+ input string
103+ exp []map [string ]any
103104 }{
104105 {
105106 note : "rule scope labels" ,
@@ -190,13 +191,108 @@ allow if input.role == "viewer"
190191 {"id" : "allow-admin" },
191192 },
192193 },
194+ {
195+ note : "package scope labels inherited by rules" ,
196+ module : `# METADATA
197+ # scope: package
198+ # labels:
199+ # service: auth
200+ package test
201+
202+ # METADATA
203+ # labels:
204+ # severity: high
205+ allow if input.role == "admin"
206+ ` ,
207+ query : "data.test.allow" ,
208+ exp : []map [string ]any {
209+ {"service" : "auth" },
210+ {"severity" : "high" },
211+ },
212+ },
213+ {
214+ note : "package and document and rule scope all combine" ,
215+ module : `# METADATA
216+ # scope: package
217+ # labels:
218+ # service: auth
219+ package test
220+
221+ # METADATA
222+ # scope: document
223+ # labels:
224+ # component: authz
225+
226+ # METADATA
227+ # labels:
228+ # severity: high
229+ allow if input.role == "admin"
230+ ` ,
231+ query : "data.test.allow" ,
232+ exp : []map [string ]any {
233+ {"service" : "auth" },
234+ {"component" : "authz" },
235+ {"severity" : "high" },
236+ },
237+ },
238+ {
239+ note : "both rules fire, both severities collected" ,
240+ module : `package test
241+
242+ # METADATA
243+ # labels:
244+ # severity: high
245+ reasons contains "admin" if "admin" in input.roles
246+
247+ # METADATA
248+ # labels:
249+ # severity: low
250+ reasons contains "viewer" if "viewer" in input.roles
251+ ` ,
252+ query : "data.test.reasons" ,
253+ input : `{"roles": ["admin", "viewer"]}` ,
254+ exp : []map [string ]any {
255+ {"severity" : "high" },
256+ {"severity" : "low" },
257+ },
258+ },
259+ {
260+ note : "subpackages scope labels inherited by rules in child packages" ,
261+ modules : map [string ]string {
262+ "parent" : `# METADATA
263+ # scope: subpackages
264+ # labels:
265+ # org: acme
266+ package test
267+ ` ,
268+ "child" : `package test.authz
269+
270+ # METADATA
271+ # labels:
272+ # severity: high
273+ allow if input.role == "admin"
274+ ` ,
275+ },
276+ query : "data.test.authz.allow" ,
277+ exp : []map [string ]any {
278+ {"org" : "acme" },
279+ {"severity" : "high" },
280+ },
281+ },
193282 }
194283
195284 for _ , tc := range tests {
196285 t .Run (tc .note , func (t * testing.T ) {
197- mod := ast .MustParseModuleWithOpts (tc .module , ast.ParserOptions {ProcessAnnotation : true })
286+ modules := make (map [string ]* ast.Module )
287+ if tc .modules != nil {
288+ for name , src := range tc .modules {
289+ modules [name ] = ast .MustParseModuleWithOpts (src , ast.ParserOptions {ProcessAnnotation : true })
290+ }
291+ } else {
292+ modules ["test" ] = ast .MustParseModuleWithOpts (tc .module , ast.ParserOptions {ProcessAnnotation : true })
293+ }
198294 c := ast .NewCompiler ()
199- c .Compile (map [ string ] * ast. Module { "test" : mod } )
295+ c .Compile (modules )
200296 if c .Failed () {
201297 t .Fatal (c .Errors )
202298 }
0 commit comments