Skip to content

Commit 5a10815

Browse files
authored
Merge pull request ocaml#11869 from Alizter/fix-foo_t_run_t
fix: dune test dirtest.t/run.t running cram test incorrectly
2 parents f379937 + ab6d23b commit 5a10815

File tree

7 files changed

+91
-71
lines changed

7 files changed

+91
-71
lines changed

bin/runtest.ml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@ let runtest_info =
3535

3636
let find_cram_test path ~parent_dir =
3737
let open Memo.O in
38-
Source_tree.nearest_dir parent_dir
39-
>>= Dune_rules.Cram_rules.cram_tests
40-
(* We ignore the errors we get when searching for cram tests as they will
41-
be reported during building anyway. We are only interested in the
42-
presence of cram tests. *)
43-
>>| List.filter_map ~f:Result.to_option
44-
(* We search our list of known cram tests for the test we are looking
45-
for. *)
46-
>>| List.find ~f:(fun (test : Source.Cram_test.t) ->
47-
let src =
48-
match test with
49-
| File src -> src
50-
| Dir { dir = src; _ } -> src
51-
in
52-
Path.Source.equal path src)
38+
Source_tree.find_dir parent_dir
39+
>>= function
40+
| None -> Memo.return None
41+
| Some dir ->
42+
Dune_rules.Cram_rules.cram_tests dir
43+
>>| List.find_map ~f:(function
44+
| Ok cram_test when Path.Source.equal path (Source.Cram_test.path cram_test) ->
45+
Some cram_test
46+
(* We raise any error we encounter when looking for our test specifically. *)
47+
| Error (Dune_rules.Cram_rules.Missing_run_t cram_test)
48+
when Path.Source.equal path (Source.Cram_test.path cram_test) ->
49+
Dune_rules.Cram_rules.missing_run_t cram_test
50+
(* Any errors or successes unrelated to our test are discarded. *)
51+
| Error (Dune_rules.Cram_rules.Missing_run_t _) | Ok _ -> None)
5352
;;
5453

5554
let explain_unsuccessful_search path ~parent_dir =
@@ -95,7 +94,7 @@ let disambiguate_test_name path =
9594
>>= (function
9695
| Some test ->
9796
(* If we find the cram test, then we request that is run. *)
98-
Memo.return (`Test (parent_dir, Source.Cram_test.name test))
97+
Memo.return (`Cram (parent_dir, test))
9998
| None ->
10099
(* If we don't find it, then we assume the user intended a directory for
101100
@runtest to be used. *)
@@ -140,7 +139,8 @@ let runtest_term =
140139
Alias.request
141140
@@
142141
match alias_kind with
143-
| `Test (dir, alias_name) ->
142+
| `Cram (dir, cram) ->
143+
let alias_name = Source.Cram_test.name cram in
144144
Alias.in_dir
145145
~name:(Dune_engine.Alias.Name.of_string alias_name)
146146
~recursive:false

src/dune_rules/cram/cram_rules.ml

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,19 @@ end
3131
type error = Missing_run_t of Cram_test.t
3232

3333
let missing_run_t (error : Cram_test.t) =
34-
Action_builder.fail
35-
{ fail =
36-
(fun () ->
37-
let dir =
38-
match error with
39-
| File _ ->
40-
(* This error is impossible for file tests *)
41-
assert false
42-
| Dir { dir; file = _ } -> dir
43-
in
44-
User_error.raise
45-
~loc:(Loc.in_dir (Path.source dir))
46-
[ Pp.textf
47-
"Cram test directory %s does not contain a run.t file."
48-
(Path.Source.to_string dir)
49-
])
50-
}
34+
let dir =
35+
match error with
36+
| File _ ->
37+
(* This error is impossible for file tests *)
38+
assert false
39+
| Dir { dir; file = _ } -> dir
40+
in
41+
User_error.raise
42+
~loc:(Loc.in_dir (Path.source dir))
43+
[ Pp.textf
44+
"Cram test directory %s does not contain a run.t file."
45+
(Path.Source.to_string dir)
46+
]
5147
;;
5248

5349
let test_rule
@@ -81,7 +77,8 @@ let test_rule
8177
match test with
8278
| Error (Missing_run_t test) ->
8379
(* We error out on invalid tests even if they are disabled. *)
84-
Alias_rules.add sctx ~alias ~loc (missing_run_t test)
80+
Action_builder.fail { fail = (fun () -> missing_run_t test) }
81+
|> Alias_rules.add sctx ~alias ~loc
8582
| Ok test ->
8683
(* Morally, this is equivalent to evaluating them all concurrently and
8784
taking the conjunction, but we do it this way to avoid evaluating things

src/dune_rules/cram/cram_rules.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
open Import
44

55
(** The type of errors that can occur when searching for cram tests *)
6-
type error
6+
type error = Missing_run_t of Cram_test.t
7+
8+
(** [missing_run_t cram_test] raises an error message explaining that the
9+
directory test [cram_test] is missing it's [run.t]. *)
10+
val missing_run_t : Cram_test.t -> 'a
711

812
(** Memoized list of cram tests in a directory. *)
913
val cram_tests : Source_tree.Dir.t -> (Cram_test.t, error) result list Memo.t

src/source/cram_test.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ type t =
77
; dir : Path.Source.t
88
}
99

10-
let is_cram_suffix = String.is_suffix ~suffix:".t"
10+
let fname_in_dir_test = "run.t"
11+
let suffix = ".t"
12+
let is_cram_suffix = String.is_suffix ~suffix
1113

12-
let dyn_of_t =
14+
let to_dyn =
1315
let open Dyn in
1416
function
1517
| File f -> variant "File" [ Path.Source.to_dyn f ]
@@ -19,19 +21,17 @@ let dyn_of_t =
1921
[ record [ "file", Path.Source.to_dyn file; "dir", Path.Source.to_dyn dir ] ]
2022
;;
2123

24+
let path = function
25+
| File file -> file
26+
| Dir d -> d.dir
27+
;;
28+
2229
let name t =
23-
String.drop_suffix
24-
~suffix:".t"
25-
(match t with
26-
| File file -> Path.Source.basename file
27-
| Dir { file = _; dir } -> Path.Source.basename dir)
28-
|> Option.value_exn
30+
path t |> Path.Source.basename |> String.drop_suffix ~suffix |> Option.value_exn
2931
;;
3032

3133
let script t =
3234
match t with
3335
| File f -> f
3436
| Dir d -> d.file
3537
;;
36-
37-
let fname_in_dir_test = "run.t"

src/source/cram_test.mli

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
open Import
22

3+
(** [t] represents the source file associated to a cram test. *)
34
type t =
45
| File of Path.Source.t
56
| Dir of
67
{ file : Path.Source.t
78
; dir : Path.Source.t
89
}
910

10-
val is_cram_suffix : string -> bool
11-
val dyn_of_t : t -> Dyn.t
12-
val name : t -> string
11+
val to_dyn : t -> Dyn.t
12+
13+
(** Checks if a filename has the ".t" suffix for a cram test. *)
14+
val is_cram_suffix : Filename.t -> bool
15+
16+
(** The "run.t" filename for directory cram tests. *)
1317
val fname_in_dir_test : Filename.t
18+
19+
(** The [name] of a cram test. If this is a file test, then it will be the file
20+
name without the cram suffix. If this is a directory test, then it will be
21+
the directory name without the cram suffix. *)
22+
val name : t -> string
23+
24+
(** The [path] associated to a cram test. If this is a file test, then it will
25+
be the file. If this is a directory test, then it will be the directory. *)
26+
val path : t -> Path.Source.t
27+
28+
(** The [script] of a cram test. If this is a file test, then it will be the
29+
file. If this is a directory test, then it will be the "run.t" file inside
30+
that directory. *)
1431
val script : t -> Path.Source.t

test/blackbox-tests/test-cases/cram/kinds.t

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ If there is no run.t file, an error message is displayed.
6464
File "dir-no-run/dir.t", line 1, characters 0-0:
6565
Error: Cram test directory dir-no-run/dir.t does not contain a run.t file.
6666
[1]
67+
$ dune runtest dir-no-run/dir.t
68+
File "dir-no-run/dir.t", line 1, characters 0-0:
69+
Error: Cram test directory dir-no-run/dir.t does not contain a run.t file.
70+
[1]
6771

6872
However, if the directory is empty, this check is skipped. (git can leave such
6973
empty directories)

test/blackbox-tests/test-cases/runtest-cmd.t

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,52 @@ Here we test the features of the `dune runtest` command.
66

77
$ cat > mytest.t <<EOF
88
> $ echo "Hello, world!"
9-
> "Goodbye, world!"
9+
> "Goodbye, world!"
1010
> EOF
1111
$ mkdir -p tests/myothertest.t
1212
$ echo 'Hello, world!' > tests/myothertest.t/hello.world
1313
$ cat > tests/myothertest.t/run.t <<EOF
1414
> $ cat hello.world
15-
> "Goodbye, world!"
15+
> "Goodbye, world!"
1616
> EOF
1717
$ cat > tests/filetest.t <<EOF
1818
> $ echo "Hello, world!"
19-
> "Goodbye, world!"
19+
> "Goodbye, world!"
2020
> EOF
2121

22-
23-
This should work:
22+
dune runtest should be able to run a specfic test. In this case,
23+
tests/myothertest.t should fail because the expected output is different from
24+
the observed output.
2425

2526
$ dune test tests/myothertest.t
2627
File "tests/myothertest.t/run.t", line 1, characters 0-0:
2728
Error: Files _build/default/tests/myothertest.t/run.t and
2829
_build/default/tests/myothertest.t/run.t.corrected differ.
2930
[1]
30-
31-
There is no diff produced because the test passes
32-
3331
$ dune promotion diff tests/myothertest.t/run.t
3432

35-
This should not work
36-
37-
$ dune test myotherttest.t
38-
Error: "myotherttest.t" does not match any known test.
39-
[1]
33+
We use the promotion diff command to check there is a promotion pending. If
34+
there is no promotion it will warn.
4035

41-
This is a bug. Running the test this way does not correctly include the
42-
dependencies.
36+
If the user writes the run.t file of a directory test, we should correct it to
37+
be the corresponding directory cram test.
4338

4439
$ dune test tests/myothertest.t/run.t
4540
File "tests/myothertest.t/run.t", line 1, characters 0-0:
4641
Error: Files _build/default/tests/myothertest.t/run.t and
4742
_build/default/tests/myothertest.t/run.t.corrected differ.
4843
[1]
49-
5044
$ dune promotion diff tests/myothertest.t/run.t
5145

52-
$ cat _build/.promotion-staging/tests/myothertest.t/run.t
53-
$ cat hello.world
54-
cat: hello.world: No such file or directory
55-
[1]
56-
"Goodbye, world!"
46+
We cannot give the name of a cram test in a subdirectory and expect Dune to
47+
find it.
48+
49+
$ dune test myothertest.t
50+
Error: "myothertest.t" does not match any known test.
51+
[1]
52+
53+
$ dune promotion diff tests/myothertest.t/run.t
54+
Warning: Nothing to promote for tests/myothertest.t/run.t.
5755

5856
Passing no arguments to $ dune runtest should be equivalent to $ dune build
5957
@runtest.
@@ -161,7 +159,7 @@ the directory is mispelled.
161159
Error: This path is outside the workspace: /a/b/c/
162160
[1]
163161

164-
Here we test behavour for running tests in specific contexts.
162+
Here we test behaviour for running tests in specific contexts.
165163

166164
$ cat > dune-workspace <<EOF
167165
> (lang dune 3.20)

0 commit comments

Comments
 (0)