|
| 1 | +open OUnit |
| 2 | + |
| 3 | +(* Load-path lookups accept a module's file under its name as written and |
| 4 | + uncapitalized. They return the name as written when it exists, because on a |
| 5 | + case-insensitive file system both spellings name the same file and the |
| 6 | + result shows up in error messages and import paths. *) |
| 7 | + |
| 8 | +let rec remove_tree path = |
| 9 | + match (Unix.lstat path).st_kind with |
| 10 | + | Unix.S_DIR -> |
| 11 | + Array.iter |
| 12 | + (fun name -> remove_tree (Filename.concat path name)) |
| 13 | + (Sys.readdir path); |
| 14 | + Unix.rmdir path |
| 15 | + | _ -> Sys.remove path |
| 16 | + | exception Unix.Unix_error (Unix.ENOENT, _, _) -> () |
| 17 | + |
| 18 | +let with_temp_dir action = |
| 19 | + let path = Filename.temp_file "rescript-find-in-path-" "" in |
| 20 | + Sys.remove path; |
| 21 | + Unix.mkdir path 0o755; |
| 22 | + let path = Unix.realpath path in |
| 23 | + Fun.protect ~finally:(fun () -> remove_tree path) (fun () -> action path) |
| 24 | + |
| 25 | +let write_cmi path = |
| 26 | + ignore |
| 27 | + (Cmi_format.create_cmi path |
| 28 | + { |
| 29 | + Cmi_format.cmi_name = "Abc"; |
| 30 | + cmi_sign = []; |
| 31 | + cmi_crcs = []; |
| 32 | + cmi_flags = []; |
| 33 | + } |
| 34 | + : Digest.t) |
| 35 | + |
| 36 | +(* [files] are created in a temporary directory, which is the load path while |
| 37 | + [check] runs with that directory. *) |
| 38 | +let in_load_path files check = |
| 39 | + with_temp_dir (fun root -> |
| 40 | + List.iter (fun name -> write_cmi (Filename.concat root name)) files; |
| 41 | + Compiler_request_state.with_fresh ~cwd:root (fun () -> |
| 42 | + Config.set_load_path [root]; |
| 43 | + check root)) |
| 44 | + |
| 45 | +let printer = Fun.id |
| 46 | + |
| 47 | +let lookups = |
| 48 | + [ |
| 49 | + ( "misc", |
| 50 | + fun name -> |
| 51 | + Misc.find_in_path_uncap (Config.get_load_path ()) (name ^ ".cmi") ); |
| 52 | + ( "config_util", |
| 53 | + fun name -> Option.get (Config_util.find_opt (name ^ ".cmi")) ); |
| 54 | + ( "bs_cmi_load", |
| 55 | + fun name -> |
| 56 | + (Option.get (Bs_cmi_load.load_cmi ~unit_name:name)) |
| 57 | + .Env.Persistent_signature.filename ); |
| 58 | + ] |
| 59 | + |
| 60 | +let as_written_first_tests _ = |
| 61 | + List.iter |
| 62 | + (fun (lookup_name, lookup) -> |
| 63 | + in_load_path ["Abc.cmi"; "abc.cmi"] (fun root -> |
| 64 | + assert_equal ~printer ~msg:lookup_name |
| 65 | + (Filename.concat root "Abc.cmi") |
| 66 | + (lookup "Abc")); |
| 67 | + in_load_path ["Abc.cmi"] (fun root -> |
| 68 | + assert_equal ~printer ~msg:lookup_name |
| 69 | + (Filename.concat root "Abc.cmi") |
| 70 | + (lookup "Abc"))) |
| 71 | + lookups |
| 72 | + |
| 73 | +(* Only a case-sensitive file system tells the two spellings apart. *) |
| 74 | +let uncapitalized_fallback_tests _ = |
| 75 | + List.iter |
| 76 | + (fun (lookup_name, lookup) -> |
| 77 | + in_load_path ["abc.cmi"] (fun root -> |
| 78 | + let found = lookup "Abc" in |
| 79 | + if not (Sys.file_exists (Filename.concat root "Abc.cmi")) then |
| 80 | + assert_equal ~printer ~msg:lookup_name |
| 81 | + (Filename.concat root "abc.cmi") |
| 82 | + found)) |
| 83 | + lookups |
| 84 | + |
| 85 | +let suites = |
| 86 | + __FILE__ |
| 87 | + >::: [ |
| 88 | + "as_written_first" >:: as_written_first_tests; |
| 89 | + "uncapitalized_fallback" >:: uncapitalized_fallback_tests; |
| 90 | + ] |
0 commit comments