Skip to content

Commit 8273a71

Browse files
committed
refactor(runtest): split finding candidates and did you mean error
Signed-off-by: Ali Caglayan <[email protected]>
1 parent e80ac2d commit 8273a71

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

bin/runtest_common.ml

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,33 @@ let find_cram_test cram_tests path =
2020
| Error (Dune_rules.Cram_rules.Missing_run_t _) | Ok _ -> None)
2121
;;
2222

23-
let explain_unsuccessful_search path ~parent_dir =
23+
let all_tests_of_dir parent_dir =
2424
let open Memo.O in
25-
(* If the user misspelled the test name, we give them a hint. *)
26-
let+ hints =
27-
(* We search for all files and directories in the parent directory and
28-
suggest them as possible candidates. *)
29-
let+ candidates =
30-
let+ cram_candidates =
31-
let+ cram_tests = cram_tests_of_dir parent_dir in
32-
List.filter_map cram_tests ~f:(fun res ->
33-
Result.to_option res
34-
|> Option.map ~f:(fun test ->
35-
Source.Cram_test.path test |> Path.Source.to_string))
36-
and+ dir_candidates =
37-
let* parent_source_dir = Source_tree.find_dir parent_dir in
38-
match parent_source_dir with
39-
| None -> Memo.return []
40-
| Some parent_source_dir ->
41-
let dirs = Source_tree.Dir.sub_dirs parent_source_dir in
42-
String.Map.to_list dirs
43-
|> Memo.List.map ~f:(fun (_candidate, candidate_path) ->
44-
Source_tree.Dir.sub_dir_as_t candidate_path
45-
>>| Source_tree.Dir.path
46-
>>| Path.Source.to_string)
47-
in
48-
List.concat [ cram_candidates; dir_candidates ]
49-
in
50-
User_message.did_you_mean (Path.Source.to_string path) ~candidates
25+
let+ cram_candidates =
26+
cram_tests_of_dir parent_dir
27+
>>| List.filter_map ~f:(fun res ->
28+
Result.to_option res
29+
|> Option.map ~f:(fun test -> Source.Cram_test.path test |> Path.Source.to_string))
30+
and+ dir_candidates =
31+
let* parent_source_dir = Source_tree.find_dir parent_dir in
32+
match parent_source_dir with
33+
| None -> Memo.return []
34+
| Some parent_source_dir ->
35+
let dirs = Source_tree.Dir.sub_dirs parent_source_dir in
36+
String.Map.to_list dirs
37+
|> Memo.List.map ~f:(fun (_candidate, candidate_path) ->
38+
Source_tree.Dir.sub_dir_as_t candidate_path
39+
>>| Source_tree.Dir.path
40+
>>| Path.Source.to_string)
5141
in
42+
List.concat [ cram_candidates; dir_candidates ]
43+
;;
44+
45+
let explain_unsuccessful_search path ~parent_dir =
46+
let open Memo.O in
47+
let+ candidates = all_tests_of_dir parent_dir in
5248
User_error.raise
53-
~hints
49+
~hints:(User_message.did_you_mean (Path.Source.to_string path) ~candidates)
5450
[ Pp.textf "%S does not match any known test." (Path.Source.to_string path) ]
5551
;;
5652

0 commit comments

Comments
 (0)