Explose naming table SQLite path
[hiphop-php.git] / hphp / hack / test / unit / naming / naming_table_tests.ml
blob5494f15f22e12e8eda90d4ee48f5158f2c492383
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 = Naming_table.Types.pos
20 let to_string (pos, type_of_type) =
21 Printf.sprintf "(%s, %d)"
22 (FileInfo.show_pos pos)
23 (Naming_table.type_of_type_to_enum type_of_type)
24 let is_equal = (=)
25 end)
26 module Pos_asserter = Asserter.Make_asserter (struct
27 type t = FileInfo.pos
28 let to_string pos =
29 Printf.sprintf "(%s)" (FileInfo.show_pos pos)
30 let is_equal = (=)
31 end)
33 let files = [
34 ("foo.php", {|<?hh
35 class Foo {}
36 |});
37 ("bar.php", {|<?hh
38 function bar(): void {}
39 |});
40 ("baz.php", {|<?hh
41 type Baz = Foo;
42 |});
43 ("qux.php", {|<?hh
44 const int Qux = 5;
45 |});
48 let write_and_parse_test_files () =
49 let files = List.map files ~f:(fun (fn, contents) -> (Relative_path.from_root fn, contents)) in
50 List.iter files ~f:begin fun (fn, contents) ->
51 let fn = Path.make (Relative_path.to_absolute fn) in
52 let dir = Path.dirname fn in
53 Disk.mkdir_p (Path.to_string dir);
54 Disk.write_file ~file:(Path.to_string fn) ~contents
55 end;
56 let (file_infos, errors, failed_parsing) = Parsing_service.go
57 None
58 Relative_path.Set.empty
59 ~get_next:(MultiWorker.next None (List.map files ~f:fst))
60 ParserOptions.default
61 ~trace:true
63 if not (Errors.is_empty errors) then begin
64 Errors.iter_error_list
65 begin fun e ->
66 List.iter (Errors.to_list e) ~f:begin fun (pos, msg) ->
67 eprintf "%s: %s\n" (Pos.string (Pos.to_absolute pos)) msg
68 end
69 end
70 errors;
71 failwith "Expected no errors from parsing."
72 end;
73 if failed_parsing <> Relative_path.Set.empty then failwith "Expected all files to pass parsing.";
74 Naming_table.create file_infos
76 let run_naming_table_test f =
77 Tempfile.with_real_tempdir begin fun path ->
78 Relative_path.set_path_prefix Relative_path.Root (Path.concat path "root/");
79 let config = SharedMem.{
80 global_size = 1024;
81 heap_size = 1024 * 1024;
82 dep_table_pow = 16;
83 hash_table_pow = 10;
84 shm_dirs = [];
85 shm_min_avail = 0;
86 log_level = 0;
87 sample_rate = 0.0;
88 } in
89 let _ : SharedMem.handle = SharedMem.init config ~num_workers:0 in
90 let unbacked_naming_table = write_and_parse_test_files () in
91 let db_name = Path.to_string (Path.concat path "naming_table.sqlite") in
92 Asserter.Int_asserter.assert_equals
94 (Naming_table.save unbacked_naming_table db_name)
95 "Expected to add eight rows (four files and four symbols)";
96 let backed_naming_table = Naming_table.load_from_sqlite ~update_reverse_entries:false db_name in
97 f ~unbacked_naming_table ~backed_naming_table ~db_name;
98 true
99 end
102 let test_get_pos () =
103 run_naming_table_test begin fun ~unbacked_naming_table:_ ~backed_naming_table:_ ~db_name:_->
104 Types_pos_asserter.assert_option_equals
105 (Some (
106 FileInfo.File (FileInfo.Class, Relative_path.from_root "foo.php"),
107 Naming_table.TClass))
108 (Naming_table.Types.get_pos "\\Foo")
109 "Check for class type";
110 Pos_asserter.assert_option_equals
111 (Some (FileInfo.File (FileInfo.Fun, Relative_path.from_root "bar.php")))
112 (Naming_table.Funs.get_pos "\\bar")
113 "Check for function";
114 Types_pos_asserter.assert_option_equals
115 (Some (
116 FileInfo.File (FileInfo.Typedef, Relative_path.from_root "baz.php"),
117 Naming_table.TTypedef))
118 (Naming_table.Types.get_pos "\\Baz")
119 "Check for typedef type";
120 Pos_asserter.assert_option_equals
121 (Some (FileInfo.File (FileInfo.Const, Relative_path.from_root "qux.php")))
122 (Naming_table.Consts.get_pos "\\Qux")
123 "Check for const"
126 let test_get_canon_name () =
127 run_naming_table_test begin fun ~unbacked_naming_table:_ ~backed_naming_table:_ ~db_name:_ ->
128 (* Since we're parsing but not naming, the canon heap must fall back to the
129 files on disk, which is the situation we'd be in when loading from a
130 saved state. *)
131 Asserter.String_asserter.assert_option_equals
132 (Some "\\Foo")
133 (Naming_table.Types.get_canon_name "\\foo")
134 "Check for class canon name";
135 Asserter.String_asserter.assert_option_equals
136 (Some "\\bar")
137 (Naming_table.Funs.get_canon_name "\\bar")
138 "Check for function canon name";
139 Asserter.String_asserter.assert_option_equals
140 (Some "\\Baz")
141 (Naming_table.Types.get_canon_name "\\baz")
142 "Check for typedef canon name"
145 let test_remove () =
146 run_naming_table_test begin fun ~unbacked_naming_table ~backed_naming_table ~db_name:_ ->
147 let foo_path = Relative_path.from_root "foo.php" in
148 assert (Naming_table.get_file_info unbacked_naming_table foo_path |> Option.is_some);
149 let unbacked_naming_table = Naming_table.remove unbacked_naming_table foo_path in
150 assert (Naming_table.get_file_info unbacked_naming_table foo_path |> Option.is_none);
152 assert (Naming_table.get_file_info backed_naming_table foo_path |> Option.is_some);
153 let backed_naming_table = Naming_table.remove backed_naming_table foo_path in
154 assert (Naming_table.get_file_info backed_naming_table foo_path |> Option.is_none)
157 let test_get_sqlite_paths () =
158 run_naming_table_test begin fun ~unbacked_naming_table:_ ~backed_naming_table ~db_name ->
159 Asserter.String_asserter.assert_option_equals
160 (Some db_name)
161 (Naming_table.get_reverse_naming_fallback_path ())
162 "get_reverse_naming_fallback_path should return the expected value";
164 Asserter.String_asserter.assert_option_equals
165 (Some db_name)
166 (Naming_table.get_forward_naming_fallback_path backed_naming_table)
167 "get_forward_naming_fallback_path should return the expected value"
170 let () =
171 Unit_test.run_all [
172 ("test_get_pos", test_get_pos);
173 ("test_get_canon_name", test_get_canon_name);
174 ("test_remove", test_remove);
175 ("test_get_sqlite_paths", test_get_sqlite_paths);