Skip to content

Commit 8544e48

Browse files
authored
Merge pull request #12663 from Alizter/push-olmpmqxllpks
fix(cram): improve and deduplicate test suggestions
2 parents a6d7819 + 0745951 commit 8544e48

File tree

2 files changed

+65
-40
lines changed

2 files changed

+65
-40
lines changed

bin/runtest_common.ml

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,54 @@
11
open Import
22

3-
let find_cram_test path ~parent_dir =
3+
let cram_tests_of_dir parent_dir =
44
let open Memo.O in
55
Source_tree.find_dir parent_dir
66
>>= function
7-
| None -> Memo.return None
8-
| Some dir ->
9-
Dune_rules.Cram_rules.cram_tests dir
10-
>>| List.find_map ~f:(function
11-
| Ok cram_test when Path.Source.equal path (Source.Cram_test.path cram_test) ->
12-
Some cram_test
13-
(* We raise any error we encounter when looking for our test specifically. *)
14-
| Error (Dune_rules.Cram_rules.Missing_run_t cram_test)
15-
when Path.Source.equal path (Source.Cram_test.path cram_test) ->
16-
Dune_rules.Cram_rules.missing_run_t cram_test
17-
(* Any errors or successes unrelated to our test are discarded. *)
18-
| Error (Dune_rules.Cram_rules.Missing_run_t _) | Ok _ -> None)
7+
| None -> Memo.return []
8+
| Some dir -> Dune_rules.Cram_rules.cram_tests dir
199
;;
2010

21-
let explain_unsuccessful_search path ~parent_dir =
11+
let find_cram_test cram_tests path =
12+
List.find_map cram_tests ~f:(function
13+
| Ok cram_test when Path.Source.equal path (Source.Cram_test.path cram_test) ->
14+
Some cram_test
15+
(* We raise any error we encounter when looking for our test specifically. *)
16+
| Error (Dune_rules.Cram_rules.Missing_run_t cram_test)
17+
when Path.Source.equal path (Source.Cram_test.path cram_test) ->
18+
Dune_rules.Cram_rules.missing_run_t cram_test
19+
(* Any errors or successes unrelated to our test are discarded. *)
20+
| Error (Dune_rules.Cram_rules.Missing_run_t _) | Ok _ -> None)
21+
;;
22+
23+
let all_tests_of_dir parent_dir =
2224
let open Memo.O in
23-
(* If the user misspelled the test name, we give them a hint. *)
24-
let+ hints =
25-
(* We search for all files and directories in the parent directory and
26-
suggest them as possible candidates. *)
27-
let+ candidates =
28-
let+ file_candidates =
29-
let+ files = Source_tree.files_of parent_dir in
30-
Path.Source.Set.to_list_map files ~f:Path.Source.to_string
31-
and+ dir_candidates =
32-
let* parent_source_dir = Source_tree.find_dir parent_dir in
33-
match parent_source_dir with
34-
| None -> Memo.return []
35-
| Some parent_source_dir ->
36-
let dirs = Source_tree.Dir.sub_dirs parent_source_dir in
37-
String.Map.to_list dirs
38-
|> Memo.List.map ~f:(fun (_candidate, candidate_path) ->
39-
Source_tree.Dir.sub_dir_as_t candidate_path
40-
>>| Source_tree.Dir.path
41-
>>| Path.Source.to_string)
42-
in
43-
List.concat [ file_candidates; dir_candidates ]
44-
in
45-
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)
4641
in
42+
List.concat [ cram_candidates; dir_candidates ]
43+
|> String.Set.of_list
44+
|> String.Set.to_list
45+
;;
46+
47+
let explain_unsuccessful_search path ~parent_dir =
48+
let open Memo.O in
49+
let+ candidates = all_tests_of_dir parent_dir in
4750
User_error.raise
48-
~hints
51+
~hints:(User_message.did_you_mean (Path.Source.to_string path) ~candidates)
4952
[ Pp.textf "%S does not match any known test." (Path.Source.to_string path) ]
5053
;;
5154

@@ -57,8 +60,8 @@ let disambiguate_test_name path =
5760
| None -> Memo.return @@ `Runtest (Path.source Path.Source.root)
5861
| Some parent_dir ->
5962
let open Memo.O in
60-
find_cram_test path ~parent_dir
61-
>>= (function
63+
let* cram_tests = cram_tests_of_dir parent_dir in
64+
(match find_cram_test cram_tests path with
6265
| Some test ->
6366
(* If we find the cram test, then we request that is run. *)
6467
Memo.return (`Cram (parent_dir, test))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Test the "did you mean" hints for dune runtest command.
2+
3+
$ cat > dune-project <<EOF
4+
> (lang dune 3.21)
5+
> EOF
6+
7+
$ mkdir dir.t other_dir
8+
$ cat > dir.t/run.t <<EOF
9+
> $ echo "Directory-based cram test"
10+
> Directory-based cram test
11+
> EOF
12+
$ cat > dir_t
13+
14+
$ dune test dip.t
15+
Error: "dip.t" does not match any known test.
16+
Hint: did you mean dir.t?
17+
[1]
18+
19+
$ dune test other_dip
20+
Error: "other_dip" does not match any known test.
21+
Hint: did you mean other_dir?
22+
[1]

0 commit comments

Comments
 (0)