Label `from_root` suffix argument
[hiphop-php.git] / hphp / hack / test / unit / deptable / dependency_table_tests.ml
blob7946903460067d2c374b59da822608db7571b0ac
1 (*
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
9 *)
11 (* These are basically the same tests as in
12 * test/integration_ml/saved_state/test_naming_table_sqlite_fallback.ml, but
13 * as stripped down to just the basics as possible to make finding the root
14 * cause of test failures easier. *)
16 open Core_kernel
18 module Types_pos_asserter = Asserter.Make_asserter (struct
19 type t = FileInfo.pos * Naming_types.kind_of_type
21 let to_string (pos, type_of_type) =
22 Printf.sprintf
23 "(%s, %d)"
24 (FileInfo.show_pos pos)
25 (Naming_types.kind_of_type_to_enum type_of_type)
27 let is_equal = ( = )
28 end)
30 module Pos_asserter = Asserter.Make_asserter (struct
31 type t = FileInfo.pos
33 let to_string pos = Printf.sprintf "(%s)" (FileInfo.show_pos pos)
35 let is_equal = ( = )
36 end)
38 let files =
40 ("foo.php", {|<?hh
41 class Foo {}
42 |});
43 ("bar.php", {|<?hh
44 function bar(): void {}
45 |});
46 ("baz.php", {|<?hh
47 type Baz = Foo;
48 |});
49 ("qux.php", {|<?hh
50 const int Qux = 5;
51 |});
54 let write_and_parse_test_files () =
55 let files =
56 List.map files ~f:(fun (fn, contents) ->
57 (Relative_path.from_root ~suffix:fn, contents))
59 List.iter files ~f:(fun (fn, contents) ->
60 let fn = Path.make (Relative_path.to_absolute fn) in
61 let dir = Path.dirname fn in
62 Disk.mkdir_p (Path.to_string dir);
63 Disk.write_file ~file:(Path.to_string fn) ~contents);
64 let (file_infos, errors, failed_parsing) =
65 Parsing_service.go
66 None
67 Relative_path.Set.empty
68 ~get_next:(MultiWorker.next None (List.map files ~f:fst))
69 ParserOptions.default
70 ~trace:true
72 if not (Errors.is_empty errors) then (
73 Errors.iter_error_list
74 (fun e ->
75 List.iter (Errors.to_list e) ~f:(fun (pos, msg) ->
76 eprintf "%s: %s\n" (Pos.string (Pos.to_absolute pos)) msg))
77 errors;
78 failwith "Expected no errors from parsing."
80 if failed_parsing <> Relative_path.Set.empty then
81 failwith "Expected all files to pass parsing.";
82 Naming_table.create file_infos
84 let run_test f =
85 Tempfile.with_real_tempdir (fun path ->
86 Relative_path.set_path_prefix
87 Relative_path.Root
88 (Path.concat path "root/");
89 let config =
90 SharedMem.
92 global_size = 1024;
93 heap_size = 1024 * 1024;
94 dep_table_pow = 16;
95 hash_table_pow = 10;
96 shm_dirs = [];
97 shm_min_avail = 0;
98 log_level = 0;
99 sample_rate = 0.0;
102 let (_ : SharedMem.handle) = SharedMem.init config ~num_workers:0 in
103 let ctx =
104 Provider_context.empty_for_test
105 ~popt:ParserOptions.default
106 ~tcopt:TypecheckerOptions.default
109 let unbacked_naming_table = write_and_parse_test_files () in
110 let db_name = Path.to_string (Path.concat path "naming_table.sqlite") in
111 let save_results = Naming_table.save unbacked_naming_table db_name in
112 Asserter.Int_asserter.assert_equals
114 Naming_sqlite.(save_results.files_added + save_results.symbols_added)
115 "Expected to add eight rows (four files and four symbols)";
116 let (_backed_naming_table : Naming_table.t) =
117 Naming_table.load_from_sqlite ctx db_name
119 f ();
120 true)
122 let test_dep_graph_blob () =
123 run_test (fun () ->
124 Tempfile.with_tempdir @@ fun dir ->
125 let workers = None in
126 let delegate_state = Typing_service_delegate.default in
127 let opts = TypecheckerOptions.default in
128 let dynamic_view_files = Relative_path.Set.empty in
129 let memory_cap = None in
130 let check_info =
131 Typing_service_types.
133 init_id = "123";
134 recheck_id = None;
135 profile_log = false;
136 profile_type_check_duration_threshold = 0.0;
137 profile_type_check_twice = false;
140 let ctx =
141 Provider_context.empty_for_test ~popt:ParserOptions.default ~tcopt:opts
144 (* Check reentrancy *)
145 for i = 0 to 2 do
146 let (errors, _delegate_state, _telemetry) =
147 Typing_check_service.go
149 workers
150 delegate_state
151 (Telemetry.create ())
152 dynamic_view_files
153 [Relative_path.from_root ~suffix:"baz.php"]
154 memory_cap
155 check_info
158 Asserter.Bool_asserter.assert_equals
159 true
160 (Errors.is_empty errors)
161 "Unexpected type errors";
163 Asserter.Int_asserter.assert_equals
165 (SharedMem.get_in_memory_dep_table_entry_count ())
166 "Expected the correct # of edges in memory after saving dep table blob";
168 let filename = Path.(to_string (concat dir "deptable.bin")) in
169 let edges =
170 SharedMem.save_dep_table_blob
171 filename
172 "build_revision"
173 ~reset_state_after_saving:true
176 Asserter.Int_asserter.assert_equals
178 edges
179 "Expected # of edges to be correct";
181 Asserter.Int_asserter.assert_equals
183 (SharedMem.get_in_memory_dep_table_entry_count ())
184 "Expected 0 edges in memory after saving dep table blob"
185 done)
187 let () = Unit_test.run_all [("test_dep_graph_blob", test_dep_graph_blob)]