blob: e9484a635ad88d8b7ef98d9d78446264d09f1dfb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
open Core
module To_test = struct
let extract_locals = Compiler_rewrite.Compiler.extract_local_variables_wrapper
end
let strings =
Alcotest.testable
(Format.pp_print_list Format.pp_print_string)
(List.equal String.equal)
let invoke ?(stdin = "") ?(env = [||]) cmd =
let ic, oc, ec = UnixLabels.open_process_full cmd ~env in
Out_channel.output_string oc stdin;
Out_channel.close oc;
(In_channel.input_all ic, In_channel.input_all ec)
let rego_to_ast policy_code =
let stdout, _ =
invoke "/home/jakob/.local/bin/opa parse -f json /dev/stdin"
~stdin:policy_code ~env:[||]
in
Yojson.Basic.from_string stdout |> Compiler_rewrite.Compiler.parse_tree
let extract_query policy =
match rego_to_ast policy with
| { policy = [ { body; _ } ]; _ } -> body
| _ -> failwith "failed to parse"
let test_local_extraction_simple_1 () =
let policy_code = {|
package test
q {
x := 2
}
|} in
Alcotest.(check strings)
"expected identifier list" [ "x" ]
(To_test.extract_locals (extract_query policy_code))
let test_local_extraction_simple_2 () =
let policy_code = {|
package test
q {
x := 2
y := 3
z := 4
}
|} in
Alcotest.(check strings)
"expected identifier list" [ "x"; "y"; "z" ]
(To_test.extract_locals (extract_query policy_code))
let test_local_dependent_on_previously_defined_local () =
let policy_code = {|
package test
q {
x := 2
y := x
}
|} in
Alcotest.(check strings)
"expected identifier list" [ "x"; "y" ]
(To_test.extract_locals (extract_query policy_code))
let test_local_not_ground_because_of_ordering () =
let policy_code = {|
package test
q {
y := x
x := 2
}
|} in
Alcotest.(check strings)
"expected identifier list" [ "x" ]
(To_test.extract_locals (extract_query policy_code))
let () =
let open Alcotest in
run "Compiler"
[
( "local extraction",
[
test_case "local extraction simple (1)" `Quick
test_local_extraction_simple_1;
test_case "local extraction simple (2)" `Quick
test_local_extraction_simple_2;
test_case
"local extraction with local dependent on previously-defined local \
(1)"
`Quick test_local_dependent_on_previously_defined_local;
test_case
"local extraction with local dependent on previously-defined local \
(2)"
`Quick test_local_not_ground_because_of_ordering;
] );
]
|
© 2015 - 2026 Jakob L. Kreuze