Add readonly to decl and function types
[hiphop-php.git] / hphp / hack / test / unit / common_setup.ml
blob59a9ac09d4acf0d807ccb580a3421be973aec7d8
1 (*
2 * Copyright (c) Facebook, Inc. and its affiliates.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the "hack" directory of this source tree.
7 *)
9 let foo_contents =
10 {|<?hh //strict
11 class Foo {
12 public function foo (Bar $b) : int {
13 return $b->toString();
17 function f1(Foo $x) : void { }
18 function f2(foo $x) : void { }
21 let bar_contents =
22 {|<?hh //strict
23 class Bar {
24 public function toString() : string {
25 return "bar";
30 type setup = {
31 ctx: Provider_context.t;
32 foo_path: Relative_path.t;
33 foo_contents: string;
34 bar_path: Relative_path.t;
35 bar_contents: string;
36 nonexistent_path: Relative_path.t;
37 naming_table: Naming_table.t;
40 let fake_dir = Filename.concat Sys_utils.temp_dir_name "fake"
42 let in_fake_dir path = Filename.concat fake_dir path
44 (** This lays down some files on disk. Sets up a forward naming table.
45 Sets up the provider's reverse naming table. Returns an empty context, plus
46 information about the files on disk. The [sqlite] flag determines
47 whether we return a ctx where the naming table is backed by sqlite, or
48 all in memory. *)
49 let setup ~(sqlite : bool) (tcopt : GlobalOptions.t) : setup =
50 (* Set up a simple fake repo *)
51 Disk.mkdir_p @@ in_fake_dir "root/";
52 Relative_path.set_path_prefix
53 Relative_path.Root
54 (Path.make @@ in_fake_dir "root/");
56 (* We'll need to parse these files in order to create a naming table, which
57 will be used for look up of symbols in type checking. *)
58 Disk.write_file ~file:(in_fake_dir "root/Foo.php") ~contents:foo_contents;
59 Disk.write_file ~file:(in_fake_dir "root/Bar.php") ~contents:bar_contents;
60 let foo_path = Relative_path.from_root ~suffix:"Foo.php" in
61 let bar_path = Relative_path.from_root ~suffix:"Bar.php" in
62 let nonexistent_path = Relative_path.from_root ~suffix:"Nonexistent.php" in
63 (* Parsing produces the file infos that the naming table module can use
64 to construct the forward naming table (files-to-symbols) *)
65 let popt = ParserOptions.default in
66 let deps_mode = Typing_deps_mode.SQLiteMode in
67 let ctx =
68 Provider_context.empty_for_tool
69 ~popt
70 ~tcopt
71 ~backend:(Provider_backend.get ())
72 ~deps_mode
74 let (file_infos, _errors, _failed_parsing) =
75 Parsing_service.go
76 ctx
77 None
78 Relative_path.Set.empty
79 ~get_next:(MultiWorker.next None [foo_path; bar_path])
80 popt
81 ~trace:true
83 let naming_table = Naming_table.create file_infos in
84 (* Construct the reverse naming table (symbols-to-files) *)
85 let fast = Naming_table.to_fast naming_table in
86 Relative_path.Map.iter
87 fast
88 ~f:(fun (name : Relative_path.t) (info : FileInfo.names) ->
89 Naming_global.ndecl_file_fast
90 ctx
91 name
92 ~funs:info.FileInfo.n_funs
93 ~classes:info.FileInfo.n_classes
94 ~record_defs:info.FileInfo.n_record_defs
95 ~typedefs:info.FileInfo.n_types
96 ~consts:info.FileInfo.n_consts);
98 let (ctx, naming_table) =
99 if sqlite then (
100 let db_name = Filename.temp_file "server_naming" ".sqlite" in
101 let (_save_result : Naming_sqlite.save_result) =
102 Naming_table.save naming_table db_name
104 (* Now, I want a fresh ctx with no reverse-naming entries in it,
105 and I want it to be backed by a sqlite naming database. *)
106 Provider_backend.set_local_memory_backend_with_defaults ();
107 let sqlite_ctx =
108 Provider_context.empty_for_tool
109 ~popt:(Provider_context.get_popt ctx)
110 ~tcopt:(Provider_context.get_tcopt ctx)
111 ~backend:(Provider_backend.get ())
112 ~deps_mode:(Provider_context.get_deps_mode ctx)
114 (sqlite_ctx, Naming_table.load_from_sqlite sqlite_ctx db_name)
115 ) else
116 (ctx, naming_table)
120 ctx;
121 naming_table;
122 foo_path;
123 foo_contents;
124 bar_path;
125 bar_contents;
126 nonexistent_path;