From ae9ee41f789e1893508fadb80b4d0cd2358d66c5 Mon Sep 17 00:00:00 2001 From: Martino Luca Date: Fri, 13 Jul 2018 02:56:33 -0700 Subject: [PATCH] [TD] Change JSON format for test-determinator Reviewed By: mbouaziz Differential Revision: D8806257 fbshipit-source-id: e1b5c65 --- infer/src/Makefile | 2 +- infer/src/atd/java_profiler_samples.atd | 12 +++++ infer/src/java/JavaProfilerSamples.ml | 54 +++++++------------ infer/src/unit/JavaProfilerSamplesTest.ml | 32 +++++------ .../MyFavouriteClassTwo.java.mod1 | 2 + .../MyFavouriteClassTwo.java.mod2 | 2 + .../MyFavouriteClassTwo.java.original | 2 + .../profiler_samples.json | 19 ++++--- .../test_determinator.json.mod1.exp | 2 +- 9 files changed, 65 insertions(+), 62 deletions(-) create mode 100644 infer/src/atd/java_profiler_samples.atd diff --git a/infer/src/Makefile b/infer/src/Makefile index 9938bcd40..6d066bcf1 100644 --- a/infer/src/Makefile +++ b/infer/src/Makefile @@ -19,7 +19,7 @@ INFER_MAIN = infer #### Checkers declarations #### -INFER_ATDGEN_STUB_BASES = atd/jsonbug atd/runstate atd/stacktree atd/java_method_decl atd/perf_profiler +INFER_ATDGEN_STUB_BASES = atd/jsonbug atd/runstate atd/stacktree atd/java_method_decl atd/perf_profiler atd/java_profiler_samples INFER_ATDGEN_TYPES = j t INFER_ATDGEN_STUB_ATDS = $(INFER_ATDGEN_STUB_BASES:.atd) INFER_ATDGEN_SUFFIXES = $(foreach atd_t,$(INFER_ATDGEN_TYPES),_$(atd_t).ml _$(atd_t).mli) diff --git a/infer/src/atd/java_profiler_samples.atd b/infer/src/atd/java_profiler_samples.atd new file mode 100644 index 000000000..163cb729b --- /dev/null +++ b/infer/src/atd/java_profiler_samples.atd @@ -0,0 +1,12 @@ +type sampled_method = { + classname : string; + methodname : string; + signature : string; +} + +type java_profiler_sample = { + test : string; + methods : sampled_method list; +} + +type java_profiler_samples = java_profiler_sample list \ No newline at end of file diff --git a/infer/src/java/JavaProfilerSamples.ml b/infer/src/java/JavaProfilerSamples.ml index 5b4ef1c49..611710e38 100644 --- a/infer/src/java/JavaProfilerSamples.ml +++ b/infer/src/java/JavaProfilerSamples.ml @@ -284,41 +284,27 @@ type labeled_profiler_sample = string * ProfilerSample.t [@@deriving compare] let equal_labeled_profiler_sample = [%compare.equal : labeled_profiler_sample] -let from_json j ~use_signature = - let parse_profiler_result label result = - let methods = - match result with - | `Assoc [_; _; _; _; ("methods", `List j); _; _] -> - j - | _ -> - L.(die UserError "Unexpected JSON input for the collection of methods") - in - let rec parse_json j acc = - match j with - | `Assoc - [ ("class", `String classname) - ; _ - ; ("method", `String methodname) - ; ("signature", `String signature) - ; _ ] - :: tl -> - let signature = if use_signature then signature else JNI.void_method_with_no_arguments in - let procname = create_procname ~classname ~methodname ~signature in - parse_json tl (procname :: acc) - | [] -> - acc - | _ -> - L.(die UserError "Unexpected JSON input for the description of a single method") - in - (label, ProfilerSample.of_list (parse_json methods [])) +let from_java_profiler_samples j ~use_signature = + let process_methods methods = + ProfilerSample.of_list + (List.map + ~f:(fun {Java_profiler_samples_t.classname; methodname; signature} -> + let signature = + if use_signature then signature else JNI.void_method_with_no_arguments + in + create_procname ~classname ~methodname ~signature ) + methods) in - match j with - | `Assoc pr -> - List.map ~f:(fun (label, result) -> parse_profiler_result label result) pr - | _ -> - L.(die UserError "Unexpected JSON input for the list of profiler results") + List.map j ~f:(fun {Java_profiler_samples_t.test; methods} -> (test, process_methods methods)) + +let from_json_string str ~use_signature = + from_java_profiler_samples + (Java_profiler_samples_j.java_profiler_samples_of_string str) + ~use_signature -let from_json_string str ~use_signature = from_json (Yojson.Basic.from_string str) ~use_signature -let from_json_file file ~use_signature = from_json (Yojson.Basic.from_file file) ~use_signature +let from_json_file file ~use_signature = + from_java_profiler_samples + (Ag_util.Json.from_file Java_profiler_samples_j.read_java_profiler_samples file) + ~use_signature diff --git a/infer/src/unit/JavaProfilerSamplesTest.ml b/infer/src/unit/JavaProfilerSamplesTest.ml index f81467eb3..03f214783 100644 --- a/infer/src/unit/JavaProfilerSamplesTest.ml +++ b/infer/src/unit/JavaProfilerSamplesTest.ml @@ -164,22 +164,18 @@ let test_from_json_string_with_valid_input = ~cmp:(List.equal ~equal:JavaProfilerSamples.equal_labeled_profiler_sample) expected found in - let input1 = - "{\"label1\": {\"field1\": {}, \"field2\": {}, \"field3\": {}, \"field4\": {}, \"methods\": \ - [], \"field6\": {},\"field7\": {}}}" - in + let input1 = "[{\"test\": \"label1\",\"methods\": []}]" in let expected1 = [("label1", JavaProfilerSamples.ProfilerSample.of_list [])] in let input2 = Printf.sprintf - "{\"label1\": {\"field1\": {}, \"field2\": {}, \"field3\": {}, \"field4\": {}, \"methods\": \ - [{\"class\": \"ggg.hhh.Iii\", \"boo\": \"\", \"method\": \"\", \"signature\": \ - \"(Ljava/lang/String;[IJ)V\", \"wat\": \"\"},{\"class\": \"lll.mmm.Nnn\", \"boo\": \"\", \ - \"method\": \"\", \"signature\": \"(Ljava/lang/String;[IJ)V\", \"wat\": \"\"}], \ - \"field6\": {},\"field7\": {}},\"label2\": {\"field1\": {}, \"field2\": {}, \"field3\": \ - {}, \"field4\": {}, \"methods\": [{\"class\": \"aaa.bbb.Ccc\", \"boo\": \"\", \"method\": \ - \"methodOne\", \"signature\": \"%s\", \"wat\": \"\"},{\"class\": \"ddd.eee.Fff\", \"boo\": \ - \"\", \"method\": \"methodTwo\", \"signature\": \"(Ljava/lang/String;[IJ)[[C\", \"wat\": \ - \"\"}], \"field6\": {},\"field7\": {}}}" + "[{\"foo\":{},\"test\": \"label1\",\"methods\": [{\"class\": \"ggg.hhh.Iii\", \"boo\": \ + \"\", \"method\": \"\", \"signature\": \"(Ljava/lang/String;[IJ)V\",\"wat\": \ + \"\"},{\"class\": \"lll.mmm.Nnn\",\"boo\": \"\",\"method\": \"\",\"signature\": \ + \"(Ljava/lang/String;[IJ)V\",\"wat\": \"\"}]},{\"boo\":\"aaa\",\"test\": \ + \"label2\",\"methods\": [{\"class\": \"aaa.bbb.Ccc\",\"boo\": \"\",\"method\": \ + \"methodOne\",\"signature\": \"%s\",\"wat\": \"\"},{\"class\": \"ddd.eee.Fff\",\"boo\": \ + \"\",\"method\": \"methodTwo\",\"signature\": \"(Ljava/lang/String;[IJ)[[C\",\"wat\": \ + \"\"}]}]" JavaProfilerSamples.JNI.void_method_with_no_arguments in let expected2 = @@ -264,14 +260,18 @@ let test_from_json_string_with_invalid_input = in [ ( "test_from_json_string_1" , "{\"whatever\": {}, \"methods\": []}" - , Logging.InferUserError "Unexpected JSON input for the collection of methods" ) + , Yojson.Json_error + "Line 1, bytes 0-31:\nExpected '[' but found '{\"whatever\": {}, \"methods\": []}'" ) ; ( "test_from_json_string_2" , Printf.sprintf "{\"whatever\": {}, \"methods\": [{\"class\": \"aaa.bbb.Ccc\", \"boo\": \"\", \"method\": \ \"methodOne\", \"signature\": \"%s\"}], \"foo\": {}}" JavaProfilerSamples.JNI.void_method_with_no_arguments - , Logging.InferUserError "Unexpected JSON input for the collection of methods" ) - ; ("test_from_json_string_3", "(", Yojson.Json_error "Line 1, bytes 0-1:\nInvalid token '('") ] + , Yojson.Json_error + "Line 1, bytes 0-33:\nExpected '[' but found '{\"whatever\": {}, \"methods\": [{\"cl'" ) + ; ( "test_from_json_string_3" + , "(" + , Yojson.Json_error "Line 1, bytes 0-1:\nExpected '[' but found '('" ) ] |> List.map ~f:(fun (name, test_input, expected_exception) -> name >:: create_test test_input expected_exception ) diff --git a/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod1 b/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod1 index 0f8c55269..9e18ceded 100644 --- a/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod1 +++ b/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod1 @@ -4,6 +4,8 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + + class MyFavouriteClassTwo { void methodOne() {} diff --git a/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod2 b/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod2 index 396fc7346..78d4bded1 100644 --- a/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod2 +++ b/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.mod2 @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ + + class MyFavouriteClassTwo { void methodOne() {} diff --git a/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.original b/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.original index 5efe0bd60..bfd52fdeb 100644 --- a/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.original +++ b/infer/tests/build_systems/java_test_determinator/MyFavouriteClassTwo.java.original @@ -4,6 +4,8 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + + class MyFavouriteClassTwo { void methodOne() {} diff --git a/infer/tests/build_systems/java_test_determinator/profiler_samples.json b/infer/tests/build_systems/java_test_determinator/profiler_samples.json index 2e53c7f03..5e881fbfd 100644 --- a/infer/tests/build_systems/java_test_determinator/profiler_samples.json +++ b/infer/tests/build_systems/java_test_determinator/profiler_samples.json @@ -1,9 +1,8 @@ -{ - "label1": { +[ + { "field1": {}, + "test": "label1", "field2": {}, - "field3": {}, - "field4": {}, "methods": [ { "class": "MyFavouriteClassTwo", @@ -20,13 +19,13 @@ "src_file": "" } ], - "field6": {}, - "field7": {} + "field3": {} }, - "label2": { + { "field1": {}, "field2": {}, "field3": {}, + "test": "label2", "field4": {}, "methods": [ { @@ -44,7 +43,7 @@ "src_file": "" } ], - "field6": {}, - "field7": {} + "field5": {}, + "field6": {} } - } \ No newline at end of file + ] \ No newline at end of file diff --git a/infer/tests/build_systems/java_test_determinator/test_determinator.json.mod1.exp b/infer/tests/build_systems/java_test_determinator/test_determinator.json.mod1.exp index 0637a088a..262097c58 100644 --- a/infer/tests/build_systems/java_test_determinator/test_determinator.json.mod1.exp +++ b/infer/tests/build_systems/java_test_determinator/test_determinator.json.mod1.exp @@ -1 +1 @@ -[] \ No newline at end of file +["label1"] \ No newline at end of file