Skip to content

Commit 3e595a6

Browse files
authored
feature(cram): include failed command in cram timeouts (#12307)
Signed-off-by: Rudi Grinberg <[email protected]>
1 parent f377b92 commit 3e595a6

4 files changed

Lines changed: 73 additions & 7 deletions

File tree

doc/changes/12307.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Feature: Include shell command that was executed when a cram test has
2+
occurred in the error message (#12307, @rgrinberg)

src/dune_rules/cram/cram_exec.ml

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,39 @@ let run_cram_test env ~src ~script ~cram_stanzas ~temp_dir ~cwd ~timeout =
409409
| Ok () -> read_and_attach_exit_codes sh_script |> sanitize ~parent_script:script
410410
| Error `Timed_out ->
411411
let timeout_loc, timeout = Option.value_exn timeout in
412+
let timeout_set_message =
413+
[ Pp.textf "A time limit of %.2fs has been set in " timeout
414+
; Pp.tag User_message.Style.Loc @@ Loc.pp_file_colon_line timeout_loc
415+
]
416+
in
417+
let timeout_msg =
418+
match
419+
let completed_count =
420+
read_exit_codes_and_prefix_maps sh_script.metadata_file |> List.length
421+
in
422+
let command_blocks_only =
423+
List.filter_map sh_script.cram_to_output ~f:(function
424+
| Cram_lexer.Comment _ -> None
425+
| Cram_lexer.Command block_result -> Some block_result)
426+
in
427+
let total_commands = List.length command_blocks_only in
428+
if completed_count < total_commands
429+
then (
430+
(* Find the command that got stuck - it's the one at index completed_count *)
431+
match List.nth command_blocks_only completed_count with
432+
| Some { command; _ } -> Some (String.concat ~sep:" " command)
433+
| None -> None)
434+
else None
435+
with
436+
| None -> [ Pp.text "Cram test timed out" ]
437+
| Some cmd ->
438+
[ Pp.textf "Cram test timed out while running command:"
439+
; Pp.verbatimf " $ %s" cmd
440+
]
441+
in
412442
User_error.raise
413443
~loc:(Loc.in_file (Path.drop_optional_build_context_maybe_sandboxed src))
414-
[ Pp.concat
415-
[ Pp.textf "Cram test timed out. A time limit of %.2fs has been set in " timeout
416-
; Pp.tag User_message.Style.Loc @@ Loc.pp_file_colon_line timeout_loc
417-
; Pp.verbatim "."
418-
]
419-
]
444+
(timeout_msg @ timeout_set_message)
420445
;;
421446

422447
let run_produce_correction ~src ~env ~script ~timeout lexbuf =
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Testing that timeout errors don't include the command that caused the timeout.
2+
3+
This test demonstrates the current behavior where timeout error messages
4+
don't include information about which specific command caused the timeout.
5+
6+
$ cat > dune-project <<EOF
7+
> (lang dune 3.20)
8+
> EOF
9+
10+
$ cat > dune <<EOF
11+
> (cram
12+
> (timeout 0.1))
13+
> EOF
14+
15+
Create a cram test with multiple commands, where the second one will timeout:
16+
17+
$ cat > test.t <<EOF
18+
> $ echo "This command runs fine"
19+
> $ echo "This is the problematic command" && sleep 2
20+
> EOF
21+
22+
Run the test and verify that the timeout error doesn't mention
23+
which specific command caused the timeout:
24+
25+
$ dune test test.t
26+
File "test.t", line 1, characters 0-0:
27+
Error: Cram test timed out while running command:
28+
$ echo "This is the problematic command" && sleep 2
29+
A time limit of 0.10s has been set in
30+
dune:2
31+
[1]
32+
33+
The error message above shows that we get a generic timeout message but no
34+
indication that it was the "echo && sleep 2" command that caused the timeout.
35+
This makes debugging timeout issues difficult when there are multiple commands
36+
in a test.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ behaviour is for dune to kill the cram test immediately.
4141

4242
$ timeout 1 dune test test.t
4343
File "test.t", line 1, characters 0-0:
44-
Error: Cram test timed out. A time limit of 0.00s has been set in dune:2.
44+
Error: Cram test timed out while running command:
45+
$ echo hi
46+
A time limit of 0.00s has been set in
47+
dune:2
4548
[1]
4649

0 commit comments

Comments
 (0)