2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
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. *)
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
)
26 module Pos_asserter
= Asserter.Make_asserter
(struct
29 Printf.sprintf
"(%s)" (FileInfo.show_pos pos
)
38 function bar
(): void
{}
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
56 let (file_infos
, errors
, failed_parsing
) = Parsing_service.go
58 Relative_path.Set.empty
59 ~get_next
:(MultiWorker.next None
(List.map
files ~f
:fst
))
63 if not
(Errors.is_empty errors
) then begin
64 Errors.iter_error_list
66 List.iter
(Errors.to_list e
) ~f
:begin fun (pos
, msg
) ->
67 eprintf
"%s: %s\n" (Pos.string (Pos.to_absolute pos
)) msg
71 failwith
"Expected no errors from parsing."
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.{
81 heap_size
= 1024 * 1024;
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;
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
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
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")
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
131 Asserter.String_asserter.assert_option_equals
133 (Naming_table.Types.get_canon_name
"\\foo")
134 "Check for class canon name";
135 Asserter.String_asserter.assert_option_equals
137 (Naming_table.Funs.get_canon_name
"\\bar")
138 "Check for function canon name";
139 Asserter.String_asserter.assert_option_equals
141 (Naming_table.Types.get_canon_name
"\\baz")
142 "Check for typedef canon name"
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
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
166 (Naming_table.get_forward_naming_fallback_path
backed_naming_table)
167 "get_forward_naming_fallback_path should return the expected value"
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);