|
| 1 | +package regal.rules.idiomatic["single-item-in_test"] |
| 2 | + |
| 3 | +import data.regal.ast |
| 4 | +import data.regal.config |
| 5 | + |
| 6 | +import data.regal.rules.idiomatic["single-item-in"] as rule |
| 7 | + |
| 8 | +test_fail_single_item_in_array if { |
| 9 | + r := rule.report with input as ast.policy("fail if 1 in [1]") |
| 10 | + |
| 11 | + r == {{ |
| 12 | + "category": "idiomatic", |
| 13 | + "description": "Avoid `in` for single item collection", |
| 14 | + "level": "error", |
| 15 | + "location": { |
| 16 | + "col": 11, |
| 17 | + "end": { |
| 18 | + "col": 13, |
| 19 | + "row": 3, |
| 20 | + }, |
| 21 | + "file": "policy.rego", |
| 22 | + "row": 3, |
| 23 | + "text": "fail if 1 in [1]", |
| 24 | + }, |
| 25 | + "related_resources": [{ |
| 26 | + "description": "documentation", |
| 27 | + "ref": config.docs.resolve_url("$baseUrl/$category/single-item-in", "idiomatic"), |
| 28 | + }], |
| 29 | + "title": "single-item-in", |
| 30 | + }} |
| 31 | +} |
| 32 | + |
| 33 | +test_fail_single_item_in_set if { |
| 34 | + r := rule.report with input as ast.policy("fail if 1 in {1}") |
| 35 | + |
| 36 | + r == {{ |
| 37 | + "category": "idiomatic", |
| 38 | + "description": "Avoid `in` for single item collection", |
| 39 | + "level": "error", |
| 40 | + "location": { |
| 41 | + "col": 11, |
| 42 | + "end": { |
| 43 | + "col": 13, |
| 44 | + "row": 3, |
| 45 | + }, |
| 46 | + "file": "policy.rego", |
| 47 | + "row": 3, |
| 48 | + "text": "fail if 1 in {1}", |
| 49 | + }, |
| 50 | + "related_resources": [{ |
| 51 | + "description": "documentation", |
| 52 | + "ref": config.docs.resolve_url("$baseUrl/$category/single-item-in", "idiomatic"), |
| 53 | + }], |
| 54 | + "title": "single-item-in", |
| 55 | + }} |
| 56 | +} |
| 57 | + |
| 58 | +test_fail_single_item_in_object if { |
| 59 | + r := rule.report with input as ast.policy(`fail if 1 in {"x": 1}`) |
| 60 | + |
| 61 | + r == {{ |
| 62 | + "category": "idiomatic", |
| 63 | + "description": "Avoid `in` for single item collection", |
| 64 | + "level": "error", |
| 65 | + "location": { |
| 66 | + "col": 11, |
| 67 | + "end": { |
| 68 | + "col": 13, |
| 69 | + "row": 3, |
| 70 | + }, |
| 71 | + "file": "policy.rego", |
| 72 | + "row": 3, |
| 73 | + "text": `fail if 1 in {"x": 1}`, |
| 74 | + }, |
| 75 | + "related_resources": [{ |
| 76 | + "description": "documentation", |
| 77 | + "ref": config.docs.resolve_url("$baseUrl/$category/single-item-in", "idiomatic"), |
| 78 | + }], |
| 79 | + "title": "single-item-in", |
| 80 | + }} |
| 81 | +} |
| 82 | + |
| 83 | +test_success_in_used_on_var if { |
| 84 | + r := rule.report with input as ast.policy(`success if 1 in var`) |
| 85 | + |
| 86 | + r == set() |
| 87 | +} |
| 88 | + |
| 89 | +test_success_some_in_used_on_var if { |
| 90 | + r := rule.report with input as ast.policy(`success if [y | some y in x]`) |
| 91 | + |
| 92 | + r == set() |
| 93 | +} |
0 commit comments