Skip to content

Commit

Permalink
move search_path to configuration
Browse files Browse the repository at this point in the history
Reviewed By: dkgi

Differential Revision: D10242944

fbshipit-source-id: c80f194d68c7b917769a6fe5d4c53cebaa5c904f
  • Loading branch information
sinancepel authored and facebook-github-bot committed Oct 8, 2018
1 parent a59548e commit cb8eb85
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
17 changes: 17 additions & 0 deletions configuration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ module Analysis = struct

let pyre_root { local_root; _ } =
Path.append local_root ~element:".pyre"


let search_path { local_root; search_path; typeshed; _ } =
(* Have an ordering of search_path > typeshed > local_root with the parser. search_path precedes
* local_root due to the possibility of having a subdirectory of the root in the search path. *)
let roots =
match typeshed with
| None ->
[local_root]
| Some typeshed ->
[
Path.create_relative ~root:typeshed ~relative:"stdlib";
Path.create_relative ~root:typeshed ~relative:"third_party";
local_root;
]
in
search_path @ roots
end


Expand Down
2 changes: 2 additions & 0 deletions configuration.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ module Analysis: sig
val localize: t -> local_debug: bool -> local_strict: bool -> declare: bool -> t

val pyre_root: t -> Path.t

val search_path: t -> Path.t list
end

module Server: sig
Expand Down
21 changes: 2 additions & 19 deletions file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,6 @@ let write { path; content } =
Log.error "No contents to write to `%s`" path


let search_path { Configuration.Analysis.local_root; search_path; typeshed; _ } =
(* Have an ordering of search_path > typeshed > local_root with the parser. search_path precedes
* local_root due to the possibility of having a subdirectory of the root in the search path. *)
let roots =
match typeshed with
| None ->
[local_root]
| Some typeshed ->
[
Path.create_relative ~root:typeshed ~relative:"stdlib";
Path.create_relative ~root:typeshed ~relative:"third_party";
local_root;
]
in
search_path @ roots


module Handle = struct
type t = string
[@@deriving compare, eq, show, sexp, hash]
Expand All @@ -109,7 +92,7 @@ module Handle = struct
else
None
in
List.find_map (search_path configuration) ~f:construct_relative_to_root
List.find_map (Configuration.Analysis.search_path configuration) ~f:construct_relative_to_root


include Hashable.Make(struct
Expand Down Expand Up @@ -151,7 +134,7 @@ exception NonexistentHandle of string


let handle ~configuration { path; _ } =
let possible_roots = search_path configuration in
let possible_roots = Configuration.Analysis.search_path configuration in
match List.find_map possible_roots ~f:(fun root -> Path.get_relative_to_root ~root ~path) with
| Some handle ->
Handle.create handle
Expand Down
39 changes: 39 additions & 0 deletions test/configurationTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
LICENSE file in the root directory of this source tree. *)


open Core

open OUnit2
open Test

Expand Down Expand Up @@ -78,8 +80,45 @@ let test_equal _ =
(Configuration.Analysis.create ~debug:false ()))



let test_search_path _ =
let assert_search_path ?typeshed ?(search_path = []) ~local_root expected =
let typeshed =
typeshed
>>| Path.create_absolute ~follow_symbolic_links:false
in
let search_path =
List.map search_path ~f:(Path.create_absolute ~follow_symbolic_links:false)
in
let local_root = Path.create_absolute ~follow_symbolic_links:false local_root in
let search_path =
Configuration.Analysis.search_path
(Configuration.Analysis.create ?typeshed ~search_path ~local_root ())

|> List.map ~f:Path.show
in
assert_equal ~printer:(List.to_string ~f:ident) expected search_path
in
assert_search_path ~local_root:"/a" ["/a"];
assert_search_path
~typeshed:"/typeshed"
~local_root:"/a"
["/typeshed/stdlib"; "/typeshed/third_party"; "/a"];
assert_search_path
~search_path:["/other"; "/another"]
~local_root:"/a"
["/other"; "/another"; "/a"];
assert_search_path
~typeshed:"/typeshed"
~search_path:["/other"; "/another"]
~local_root:"/a"
["/other"; "/another"; "/typeshed/stdlib"; "/typeshed/third_party"; "/a"];
()


let () =
"configuration">:::[
"equal">::test_equal;
"search_path">::test_search_path;
]
|> Test.run

0 comments on commit cb8eb85

Please sign in to comment.