|
| 1 | +(* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + *) |
| 7 | + |
| 8 | +open Core |
| 9 | +open OUnit2 |
| 10 | +open Commands.NewAnalyze |
| 11 | +module Path = PyrePath |
| 12 | + |
| 13 | +let test_json_parsing context = |
| 14 | + let assert_parsed ~expected json = |
| 15 | + match AnalyzeConfiguration.of_yojson json with |
| 16 | + | Result.Error message -> |
| 17 | + let message = Format.sprintf "Unexpected JSON parsing failure: %s" message in |
| 18 | + assert_failure message |
| 19 | + | Result.Ok actual -> |
| 20 | + assert_equal |
| 21 | + ~ctxt:context |
| 22 | + ~cmp:[%compare.equal: AnalyzeConfiguration.t] |
| 23 | + ~printer:(fun result -> Sexp.to_string ([%sexp_of: AnalyzeConfiguration.t] result)) |
| 24 | + expected |
| 25 | + actual |
| 26 | + in |
| 27 | + |
| 28 | + let dummy_analyze_configuration = |
| 29 | + { |
| 30 | + AnalyzeConfiguration.base = BaseConfigurationTest.dummy_base_configuration; |
| 31 | + dump_call_graph = false; |
| 32 | + dump_model_query_results = false; |
| 33 | + find_missing_flows = None; |
| 34 | + inline_decorators = false; |
| 35 | + maximum_tito_depth = None; |
| 36 | + maximum_trace_length = None; |
| 37 | + repository_root = None; |
| 38 | + rule_filter = None; |
| 39 | + save_results_to = None; |
| 40 | + strict = false; |
| 41 | + taint_model_paths = []; |
| 42 | + use_cache = false; |
| 43 | + no_verify = false; |
| 44 | + } |
| 45 | + in |
| 46 | + |
| 47 | + assert_parsed |
| 48 | + (`Assoc (("dump_call_graph", `Bool true) :: BaseConfigurationTest.dummy_base_json)) |
| 49 | + ~expected:{ dummy_analyze_configuration with dump_call_graph = true }; |
| 50 | + assert_parsed |
| 51 | + (`Assoc (("dump_model_query_results", `Bool true) :: BaseConfigurationTest.dummy_base_json)) |
| 52 | + ~expected:{ dummy_analyze_configuration with dump_model_query_results = true }; |
| 53 | + assert_parsed |
| 54 | + (`Assoc (("find_missing_flows", `String "obscure") :: BaseConfigurationTest.dummy_base_json)) |
| 55 | + ~expected:{ dummy_analyze_configuration with find_missing_flows = Some "obscure" }; |
| 56 | + assert_parsed |
| 57 | + (`Assoc (("inline_decorators", `Bool true) :: BaseConfigurationTest.dummy_base_json)) |
| 58 | + ~expected:{ dummy_analyze_configuration with inline_decorators = true }; |
| 59 | + assert_parsed |
| 60 | + (`Assoc (("maximum_tito_depth", `Int 5) :: BaseConfigurationTest.dummy_base_json)) |
| 61 | + ~expected:{ dummy_analyze_configuration with maximum_tito_depth = Some 5 }; |
| 62 | + assert_parsed |
| 63 | + (`Assoc (("maximum_trace_length", `Int 5) :: BaseConfigurationTest.dummy_base_json)) |
| 64 | + ~expected:{ dummy_analyze_configuration with maximum_trace_length = Some 5 }; |
| 65 | + assert_parsed |
| 66 | + (`Assoc (("no_verify", `Bool true) :: BaseConfigurationTest.dummy_base_json)) |
| 67 | + ~expected:{ dummy_analyze_configuration with no_verify = true }; |
| 68 | + assert_parsed |
| 69 | + (`Assoc (("repository_root", `String "/root") :: BaseConfigurationTest.dummy_base_json)) |
| 70 | + ~expected: |
| 71 | + { dummy_analyze_configuration with repository_root = Some (Path.create_absolute "/root") }; |
| 72 | + assert_parsed |
| 73 | + (`Assoc (("rule_filter", `List [`Int 1; `Int 2]) :: BaseConfigurationTest.dummy_base_json)) |
| 74 | + ~expected:{ dummy_analyze_configuration with rule_filter = Some [1; 2] }; |
| 75 | + assert_parsed |
| 76 | + (`Assoc (("save_results_to", `String "/result") :: BaseConfigurationTest.dummy_base_json)) |
| 77 | + ~expected: |
| 78 | + { dummy_analyze_configuration with save_results_to = Some (Path.create_absolute "/result") }; |
| 79 | + assert_parsed |
| 80 | + (`Assoc (("strict", `Bool true) :: BaseConfigurationTest.dummy_base_json)) |
| 81 | + ~expected:{ dummy_analyze_configuration with strict = true }; |
| 82 | + assert_parsed |
| 83 | + (`Assoc |
| 84 | + (("taint_model_paths", `List [`String "/taint"; `String "/model"]) |
| 85 | + :: BaseConfigurationTest.dummy_base_json)) |
| 86 | + ~expected: |
| 87 | + { |
| 88 | + dummy_analyze_configuration with |
| 89 | + taint_model_paths = [Path.create_absolute "/taint"; Path.create_absolute "/model"]; |
| 90 | + }; |
| 91 | + assert_parsed |
| 92 | + (`Assoc (("use_cache", `Bool true) :: BaseConfigurationTest.dummy_base_json)) |
| 93 | + ~expected:{ dummy_analyze_configuration with use_cache = true }; |
| 94 | + () |
| 95 | + |
| 96 | + |
| 97 | +let () = "configuration" >::: ["json_parsing" >:: test_json_parsing] |> Test.run |
0 commit comments