Skip to content

Commit b5e0e5a

Browse files
authored
Update nested dune test to emulate calling dune build more closely (#12916)
The way the test is implemented, the code testing dune is always calling the (outside, Dune unter test) via its absolute path. This change, extracted from #12902 calls Dune via absolute path but wraps it in an utility where argv[0] is set to "dune" instead of the absolute path, more closely resembling a call to `dune build` instead of (a hypothetical) `/usr/bin/dune build`.
2 parents ad05d21 + 9a454ea commit b5e0e5a

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

test/blackbox-tests/test-cases/pkg/different-dune-in-path.t

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ Make a project that depends on the test packages:
2929
> (depends foo bar))
3030
> EOF
3131

32-
$ cat > dune <<EOF
33-
> (data_only_dirs bin)
34-
> EOF
35-
3632
Make lockfiles for the packages.
3733
$ make_lockdir
3834
$ make_lockpkg foo <<EOF
@@ -65,18 +61,40 @@ Make lockfiles for the packages.
6561
Test that the project can be built normally.
6662
$ build_pkg foo
6763

64+
6865
Make a fake dune exe:
69-
$ mkdir bin
70-
$ cat > bin/dune <<EOF
66+
67+
$ mkdir .bin
68+
$ cat > .bin/dune << 'EOF'
7169
> #!/bin/sh
72-
> echo "Fake dune! (args: \$@)"
70+
> echo "Fake dune! (args: $@)"
7371
> EOF
74-
$ chmod a+x bin/dune
72+
$ chmod a+x .bin/dune
7573

7674
$ dune clean
75+
7776
Try building in an environment where `dune` refers to the fake dune.
78-
$ DUNE=$(which dune) # otherwise we would start by running the wrong dune
79-
$ PATH=$PWD/bin:$PATH $DUNE build $pkg_root/$($dune pkg print-digest foo)/target/
77+
78+
Remember the path to the dune under test, otherwise we would launch the wrong
79+
Dune and add our fake `dune` to the PATH.
80+
81+
$ DUNE=$(which dune)
82+
$ fakepath=$PWD/.bin:$PATH
83+
84+
Remember the digests, to not to have to call nested Dunes:
85+
86+
$ foo_digest="$(dune pkg print-digest foo)"
87+
$ bar_digest="$(dune pkg print-digest bar)"
88+
89+
Call Dune with an absolute PATH as argv[0]:
90+
91+
$ PATH=$fakepath $DUNE build "$pkg_root/$foo_digest/target/"
8092
Fake dune! (args: build -p foo @install)
81-
$ PATH=$PWD/bin:$PATH $DUNE build $pkg_root/$($dune pkg print-digest bar)/target/
93+
$ PATH=$fakepath $DUNE build "$pkg_root/$bar_digest/target/"
8294
Fake dune! (args: build -p bar @install)
95+
96+
Make sure that fake dune is not picked up when dune is called with argv[0] = "dune":
97+
98+
$ dune clean
99+
$ PATH=$fakepath dune_cmd exec-a "dune" $DUNE build "$pkg_root/$foo_digest/target/"
100+
Fake dune! (args: build -p foo @install)

test/blackbox-tests/utils/dune_cmd.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,29 @@ module Wait_for_file_to_appear = struct
334334
let () = register name of_args run
335335
end
336336

337+
(* implements `exec -a` in a portable way *)
338+
module Exec_a = struct
339+
type t =
340+
{ argv0 : string
341+
; prog : string
342+
; args : string list
343+
}
344+
345+
let name = "exec-a"
346+
347+
let of_args = function
348+
| argv0 :: prog :: args -> { argv0; prog; args }
349+
| _ -> raise (Arg.Bad "Required arguments are <argv0> <program> [argument]...")
350+
;;
351+
352+
let run { argv0; prog; args } =
353+
let args = Array.of_list @@ (argv0 :: args) in
354+
Unix.execvp prog args
355+
;;
356+
357+
let () = register name of_args run
358+
end
359+
337360
let () =
338361
let name, args =
339362
match Array.to_list Sys.argv with

0 commit comments

Comments
 (0)