Skip to content

Commit c5724de

Browse files
committed
cram - add (shell ..) option to the (cram) user action
Signed-off-by: Haochen Kotoi-Xie <hx@kxc.inc>
1 parent 60de1af commit c5724de

File tree

4 files changed

+199
-9
lines changed

4 files changed

+199
-9
lines changed

src/dune_lang/action.ml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ type t =
156156
| Diff of (String_with_vars.t, String_with_vars.t) Diff.t
157157
| No_infer of t
158158
| Pipe of Outputs.t * t list
159-
| Cram of String_with_vars.t
159+
| Cram of
160+
{ script : String_with_vars.t
161+
; shell_spec : Shell_spec.t
162+
}
160163
| Patch of String_with_vars.t
161164
| Substitute of String_with_vars.t * String_with_vars.t
162165
| Withenv of String_with_vars.t Env_update.t list * t
@@ -329,8 +332,14 @@ let cstrs_dune_file t =
329332
Pipe (Outputs, ts) )
330333
; ( "cram"
331334
, Syntax.since Stanza.syntax (2, 7)
332-
>>> let+ script = sw in
333-
Cram script )
335+
>>> let+ script = sw
336+
and+ shell_spec =
337+
fields
338+
(field "shell"
339+
(Syntax.since Stanza.syntax (3, 10) >>> Shell_spec.decode)
340+
~default:Shell_spec.default)
341+
in
342+
Cram { script; shell_spec } )
334343
]
335344

336345
let decode_dune_file = Decoder.fix @@ fun t -> Decoder.sum (cstrs_dune_file t)
@@ -410,7 +419,12 @@ let rec encode =
410419
List
411420
(atom (sprintf "pipe-%s" (Outputs.to_string outputs))
412421
:: List.map l ~f:encode)
413-
| Cram script -> List [ atom "cram"; sw script ]
422+
| Cram { script; shell_spec } ->
423+
let shell_spec =
424+
if shell_spec = Shell_spec.default then []
425+
else [ List [ atom "script"; Shell_spec.encode shell_spec ] ]
426+
in
427+
List (atom "cram" :: sw script :: shell_spec)
414428
| Patch i -> List [ atom "patch"; sw i ]
415429
| Substitute (i, o) -> List [ atom "substitute"; sw i; sw o ]
416430
| Withenv (ops, t) ->
@@ -493,7 +507,13 @@ let rec map_string_with_vars t ~f =
493507
| Diff diff -> Diff (Diff.map diff ~path:f ~target:f)
494508
| No_infer t -> No_infer (map_string_with_vars t ~f)
495509
| Pipe (o, ts) -> Pipe (o, List.map ts ~f:(map_string_with_vars ~f))
496-
| Cram sw -> Cram (f sw)
510+
| Cram { script; shell_spec } ->
511+
let shell_spec =
512+
match shell_spec with
513+
| System_shell | Bash_shell -> shell_spec
514+
| Exec_file_shell p -> Exec_file_shell (f p)
515+
in
516+
Cram { script = f script; shell_spec }
497517
| Patch i -> Patch (f i)
498518
| Substitute (i, o) -> Substitute (f i, f o)
499519
| Withenv (ops, t) ->

src/dune_lang/action.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ type t =
120120
| Diff of (String_with_vars.t, String_with_vars.t) Diff.t
121121
| No_infer of t
122122
| Pipe of Outputs.t * t list
123-
| Cram of String_with_vars.t
123+
| Cram of
124+
{ script : String_with_vars.t
125+
; shell_spec : Shell_spec.t
126+
}
124127
| Patch of String_with_vars.t
125128
| Substitute of String_with_vars.t * String_with_vars.t
126129
| Withenv of String_with_vars.t Env_update.t list * t

src/dune_rules/action_unexpanded.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,18 @@ let rec expand (t : Dune_lang.Action.t) ~context : Action.t Action_expander.t =
478478
| Pipe (outputs, l) ->
479479
let+ l = A.all (List.map l ~f:expand) in
480480
O.Pipe (outputs, l)
481-
| Cram script ->
482-
let+ script = E.dep script in
483-
Cram_exec.(action { script; shell_spec = Shell_spec.default })
481+
| Cram { script; shell_spec } ->
482+
let+ script = E.dep script
483+
and+ shell_spec =
484+
let open Cram_exec.Shell_spec in
485+
match shell_spec with
486+
| System_shell -> A.return System_shell
487+
| Bash_shell -> A.return Bash_shell
488+
| Exec_file_shell p ->
489+
let+ p = E.dep p in
490+
Exec_file_shell p
491+
in
492+
Cram_exec.(action { script; shell_spec })
484493
| Withenv _ | Substitute _ | Patch _ ->
485494
(* these can only be provided by the package language which isn't expanded here *)
486495
assert false
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
Check that cram tests run without shell option specified
2+
3+
$ echo '(lang dune 3.10)' > dune-project
4+
5+
$ cat > dune <<EOF
6+
> (rule
7+
> (target foo.cram.corrected)
8+
> (mode (promote (until-clean)))
9+
> (action (cram foo.cram))
10+
> )
11+
> EOF
12+
13+
$ cat > foo.cram <<EOF
14+
> $ echo "foo from foo.cram"
15+
> EOF
16+
17+
$ dune build
18+
$ cat foo.cram.corrected
19+
$ echo "foo from foo.cram"
20+
foo from foo.cram
21+
22+
Prepare custom and mock shells for following tests
23+
24+
$ dune clean
25+
$ cat > dune <<EOF
26+
> (executables (names sh bash custom_shell))
27+
> (rule (mode (promote (until-clean))) (action (copy %{dep:./sh.exe} sh)))
28+
> (rule (mode (promote (until-clean))) (action (copy %{dep:./bash.exe} bash)))
29+
> EOF
30+
31+
$ cat > sh.ml <<EOF
32+
> print_endline "sh.ml"
33+
> EOF
34+
35+
$ cat > bash.ml <<EOF
36+
> print_endline "bash.ml"
37+
> EOF
38+
39+
$ cat > custom_shell.ml <<EOF
40+
> print_endline "custom_shell.ml"
41+
> EOF
42+
43+
$ dune build
44+
$ ls
45+
_build
46+
bash
47+
bash.ml
48+
custom_shell.ml
49+
dune
50+
dune-project
51+
foo.cram
52+
sh
53+
sh.ml
54+
55+
$ ./sh
56+
sh.ml
57+
$ ./bash
58+
bash.ml
59+
60+
Check that shell option unset uses `env sh`
61+
62+
$ PATH=$PWD:$PATH
63+
$ cat > dune <<EOF
64+
> (rule
65+
> (target foo.cram.corrected)
66+
> (mode (promote (until-clean)))
67+
> (action (cram foo.cram))
68+
> )
69+
> EOF
70+
71+
$ cat > foo.cram <<EOF
72+
> $ echo "foo from foo.cram"
73+
> EOF
74+
75+
$ dune build
76+
sh.ml
77+
$ cat foo.cram.corrected
78+
$ echo "foo from foo.cram"
79+
***** UNREACHABLE *****
80+
81+
Check that shell = :bash uses `env bash`
82+
83+
$ PATH=$PWD:$PATH
84+
$ cat > dune <<EOF
85+
> (rule
86+
> (target foo.cram.corrected)
87+
> (mode (promote (until-clean)))
88+
> (deps sh)
89+
> (action (cram foo.cram (shell :bash)))
90+
> )
91+
> EOF
92+
93+
$ cat > foo.cram <<EOF
94+
> $ echo "foo from foo.cram"
95+
> EOF
96+
97+
$ dune build
98+
bash.ml
99+
$ cat foo.cram.corrected
100+
$ echo "foo from foo.cram"
101+
***** UNREACHABLE *****
102+
103+
Check that shell = :system uses `env sh`
104+
105+
$ PATH=$PWD:$PATH
106+
$ cat > dune <<EOF
107+
> (rule
108+
> (target foo.cram.corrected)
109+
> (mode (promote (until-clean)))
110+
> (deps sh)
111+
> (action (cram foo.cram (shell :system)))
112+
> )
113+
> EOF
114+
115+
$ cat > foo.cram <<EOF
116+
> $ echo "foo from foo.cram"
117+
> EOF
118+
119+
$ dune build
120+
sh.ml
121+
$ cat foo.cram.corrected
122+
$ echo "foo from foo.cram"
123+
***** UNREACHABLE *****
124+
125+
Clean-up for the following test cases
126+
127+
$ rm -rf sh bash
128+
$ dune clean
129+
$ ls
130+
bash.ml
131+
custom_shell.ml
132+
dune
133+
dune-project
134+
foo.cram
135+
sh.ml
136+
137+
Check that shell = %{dep:./custom_shell.exe} uses executable compiled
138+
from custom_shell.ml
139+
140+
$ cat > dune <<EOF
141+
> (executables (names custom_shell))
142+
> (rule
143+
> (target foo.cram.corrected)
144+
> (mode (promote (until-clean)))
145+
> (deps ./custom_shell.exe)
146+
> (action (cram foo.cram (shell ./custom_shell.exe)))
147+
> )
148+
> EOF
149+
150+
$ cat > foo.cram <<EOF
151+
> $ echo "foo from foo.cram"
152+
> EOF
153+
154+
$ dune build
155+
custom_shell.ml
156+
$ cat foo.cram.corrected
157+
$ echo "foo from foo.cram"
158+
***** UNREACHABLE *****

0 commit comments

Comments
 (0)