This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,22 @@
(copy_files# lexers/*.ml{,i})
(copy_files# include/bar.h)
(rule
(targets dummy.ml)
(action (with-stdout-to %{targets} (echo ""))))
(library
(name foo)
(c_names bar)
(modules dummy)
(wrapped false))
(executable
(name test)
(modules :standard \ dummy)
(libraries foo))
(alias
(name bar-source)
(deps bar.h)
(action (echo "%{read:bar.h}")))

View file

@ -0,0 +1,6 @@
{
}
rule lex = parse
| _ { true }
| eof { false }

View file

@ -0,0 +1,6 @@
Test that (copy_files ...) works
$ dune build test.exe .merlin-conf/lib-foo .merlin-conf/exe-test
$ dune build @bar-source
#line 1 "include/bar.h"
int foo () {return 42;}

View file

@ -0,0 +1 @@
(rule (with-stdout-to dummy.txt (echo "hello")))

View file

@ -0,0 +1,5 @@
(copy_files# ../dummy.txt)
(alias
(name cat)
(action (cat %{dep:dummy.txt})))

View file

@ -0,0 +1,5 @@
Test that (copy_files ...) works
$ dune build @foo/cat
# 1 "dummy.txt"
hello

View file

@ -0,0 +1,30 @@
Test (alias ...) and (mode ...) fields:
$ cat >dune-project <<EOF
> (lang dune 2.6)
> EOF
$ cat >dune <<EOF
> (copy_files
> (alias foo)
> (mode promote-until-clean)
> (files subdir/*.txt))
> EOF
$ mkdir -p subdir
$ echo Foo >subdir/foo.txt
$ dune build @foo
File "dune", line 2, characters 1-12:
2 | (alias foo)
^^^^^^^^^^^
Error: 'alias' is only available since version 2.7 of the dune language.
Please update your dune-project file to have (lang dune 2.7).
[1]
$ cat >dune-project <<EOF
> (lang dune 2.7)
> EOF
$ dune build @foo
$ cat foo.txt
Foo

View file

@ -0,0 +1,13 @@
Test external paths:
$ cat >dune-project <<EOF
> (lang dune 2.7)
> EOF
$ P=$(mktemp)
$ echo Hola > $P
$ cat >dune <<EOF
> (copy_files $P)
> EOF
$ dune build $(basename $P)
$ cat _build/default/$(basename $P)
Hola

View file

@ -0,0 +1,21 @@
Test (enabled_if ...)
$ mkdir -p subdir
$ cat >dune-project <<EOF
> (lang dune 2.8)
> EOF
$ cat >subdir/dune <<EOF
> (rule (with-stdout-to foo.txt (progn)))
> EOF
$ cat >dune <<EOF
> (copy_files (enabled_if false) (files subdir/foo.txt))
> EOF
$ dune build
$ ls _build/default | grep foo.txt
[1]
$ cat >dune <<EOF
> (copy_files (enabled_if true) (files subdir/foo.txt))
> EOF
$ dune build
$ ls _build/default | grep foo.txt
foo.txt

View file

@ -0,0 +1,45 @@
Show that copy_files operates on the build folder
$ mkdir -p target foo
$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF
$ cat >target/dune <<EOF
> (copy_files
> (files ../foo/*.txt))
> EOF
$ cat >foo/dune <<EOF
> (rule
> (write-file in-build.txt ""))
> EOF
$ touch foo/in-source.txt
$ dune build target/in-source.txt
$ dune build target/in-build.txt
Show the difference when `only_sources` is used
$ cat >target/dune <<EOF
> (copy_files
> (only_sources)
> (files ../foo/*.txt))
> EOF
$ dune build target/in-source.txt
$ dune build target/in-build.txt
Error: Don't know how to build target/in-build.txt
[1]
A blang expression can be used:
$ cat >target/dune <<EOF
> (copy_files
> (only_sources (= x x))
> (files ../foo/*.txt))
> EOF
$ dune build target/in-source.txt
$ dune build target/in-build.txt
Error: Don't know how to build target/in-build.txt
[1]

View file

@ -0,0 +1,25 @@
Show that copy_files errors out if no files are found
$ mkdir -p target foo
$ cat >dune-project <<EOF
> (lang dune 3.17)
> EOF
$ cat >target/dune <<EOF
> (copy_files
> (files ../foo/*.txt))
> EOF
$ dune build
File "target/dune", line 2, characters 8-20:
2 | (files ../foo/*.txt))
^^^^^^^^^^^^
Error: Does not match any files
[1]
It doesn't error out in older dune lang versions
$ cat >dune-project <<EOF
> (lang dune 3.16)
> EOF
$ dune build

View file

@ -0,0 +1,22 @@
$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF
$ mkdir a b
$ cat >a/dune<<EOF
> (dynamic_include ../b/dune)
> EOF
$ cat >b/dune<<EOF
> (dynamic_include ../a/dune)
> EOF
$ dune build
Error: Dependency cycle between:
dynamic_include b/dune in directory a
-> dynamic_include a/dune in directory b
-> dynamic_include b/dune in directory a
-> required by alias default
[1]

View file

@ -0,0 +1,21 @@
Demonstrate that we can load dynamically generated rules
$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF
$ mkdir a b
$ cat >a/dune <<EOF
> (rule
> (with-stdout-to dune.inc
> (echo "(rule (with-stdout-to foo (echo dynamic)))")))
> EOF
$ dune build a/dune.inc
$ cat >b/dune <<EOF
> (dynamic_include ../a/dune.inc)
> EOF
$ dune build b/foo

View file

@ -0,0 +1,77 @@
Some stanzas aren't allowed to be generated:
$ cat >dune-project <<EOF
> (lang dune 3.14)
> (using dune_site 0.1)
> (package (name foo))
> EOF
$ mkdir a b
$ cat >b/dune <<EOF
> (dynamic_include ../a/dune.inc)
> (rule (with-stdout-to x (echo "")))
> EOF
$ cat >a/dune <<EOF
> (copy_files ../dune.inc)
> EOF
$ runtest() {
> cat >dune.inc
> dune build b/x
> }
$ runtest <<EOF
> (library (name foo))
> EOF
File "_build/default/a/dune.inc", line 1, characters 0-20:
1 | (library (name foo))
^^^^^^^^^^^^^^^^^^^^
Error: This stanza cannot be generated dynamically
[1]
$ runtest <<EOF
> (install
> (section bin)
> (files foo as bar))
> EOF
File "_build/default/a/dune.inc", line 2, characters 10-13:
2 | (section bin)
^^^
Error: binary section cannot be generated dynamically
[1]
$ runtest <<EOF
> (executable
> (public_name foo))
> EOF
File "_build/default/a/dune.inc", lines 1-2, characters 0-31:
1 | (executable
2 | (public_name foo))
Error: This stanza cannot be generated dynamically
[1]
$ runtest <<EOF
> (plugin
> (libraries)
> (name foo)
> (site (foo bar)))
> EOF
File "_build/default/a/dune.inc", line 4, characters 7-16:
4 | (site (foo bar)))
^^^^^^^^^
Error: This stanza cannot be generated dynamically
[1]
$ runtest <<EOF
> (deprecated_library_name
> (old_public_name foo)
> (new_public_name y))
> EOF
File "_build/default/a/dune.inc", lines 1-3, characters 0-69:
1 | (deprecated_library_name
2 | (old_public_name foo)
3 | (new_public_name y))
Error: This stanza cannot be generated dynamically
[1]

View file

@ -0,0 +1,25 @@
Nesting of dynamic_include stanzas
$ mkdir a b c
$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF
$ cat >a/dune <<EOF
> (dynamic_include ../b/dune.inc)
> EOF
$ cat >b/dune <<EOF
> (rule
> (with-stdout-to dune.inc
> (echo "(dynamic_include ../c/dune.inc)")))
> EOF
$ cat >c/dune <<EOF
> (rule
> (with-stdout-to dune.inc
> (echo "(rule (with-stdout-to foo (echo bar)))")))
> EOF
$ dune build a/foo

View file

@ -0,0 +1,20 @@
Normal include from a dynamic include
$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF
$ mkdir a b
$ cat >a/dune <<EOF
> (dynamic_include ../b/dune.inc)
> EOF
$ cat >b/dune.inc <<EOF
> (include ../b/dune2.inc)
> EOF
$ cat >b/dune2.inc <<EOF
> (rule (with-stdout-to foo (echo bar)))
> EOF
$ dune build a/foo

View file

@ -0,0 +1,13 @@
$ cat >dune-project <<EOF
> (lang dune 2.7)
> EOF
$ cat >dune.inc <<EOF
> (dirs foo)
> (rule (with-stdout-to foo (echo bar)))
> EOF
$ cat >dune <<EOF
> (include dune.inc)
> EOF
$ dune build ./foo
$ cat _build/default/foo
bar

View file

@ -0,0 +1,14 @@
Test the include stanza inside a subdirectory
$ cat >dune-project <<EOF
> (lang dune 3.13)
> EOF
$ mkdir a
$ cat >a/dune <<EOF
> (include dune.inc)
> EOF
$ cat >a/dune.inc <<EOF
> (rule (with-stdout-to foo (echo "")))
> EOF
$ dune build a/foo

View file

@ -0,0 +1,10 @@
$ dune build
File "dune", line 1, characters 0-15:
1 | (include a.inc)
^^^^^^^^^^^^^^^
Error: Recursive inclusion of dune files detected:
File a.inc is included from c.inc:1
included from b.inc:1
-> included from a.inc:1
-> included from dune:1
[1]

View file

@ -0,0 +1,14 @@
Test the include stanza pulling from a parent dir
$ cat >dune-project <<EOF
> (lang dune 3.13)
> EOF
$ mkdir a
$ cat >a/dune <<EOF
> (include ../dune.inc)
> EOF
$ cat >dune.inc <<EOF
> (rule (with-stdout-to foo (echo "")))
> EOF
$ dune build a/foo

View file

@ -0,0 +1,135 @@
(subdir ..) allows us to interpret stanzas in a sub directory
$ echo "(lang dune 2.5)" > dune-project
$ cat >dune <<EOF
> (rule (with-stdout-to foo.txt (echo "bar")))
> (subdir bar
> (rule (with-stdout-to foo.txt (echo "bar"))))
> EOF
$ dune build ./foo.txt ./bar/foo.txt
$ cat _build/default/foo.txt
bar
We can use paths such as foo/bar in (subdir ..)
$ cat >dune <<EOF
> (rule (with-stdout-to foo.txt (echo bar)))
> (subdir bar/baz
> (subdir final
> (rule (with-stdout-to txt (echo final))))
> (rule (with-stdout-to foo.txt (echo bar))))
> EOF
$ dune build bar/baz/foo.txt bar/baz/final/txt
$ cat _build/default/bar/baz/foo.txt
bar
$ cat _build/default/bar/baz/final/txt
final
This is an error because we cannot specify data_only_dirs more than once per
dir.
$ cat >dune <<EOF
> (subdir bar (data_only_dirs foo))
> EOF
$ mkdir bar
$ echo "(data_only_dirs foo)" > bar/dune
$ dune build @all
File "bar/dune", line 1, characters 16-19:
1 | (data_only_dirs foo)
^^^
Error: This stanza stanza was already specified at:
dune:1
[1]
Overriding dune files in the sub directory is possible:
$ mkdir override; cd override
$ echo "(lang dune 2.5)" > dune-project
$ cat >dune <<EOF
> (data_only_dirs shadow)
> (subdir shadow (rule (with-stdout-to bar (echo shadow))))
> EOF
$ mkdir shadow
$ echo "does not work" > shadow/dune
$ dune build ./shadow/bar
$ cat _build/default/shadow/bar
shadow
$ cd ..
In conjunction with dune generated files:
$ mkdir dune-syntax; cd dune-syntax
$ echo "(lang dune 2.5)" > dune-project
$ cat >dune <<EOF
> (subdir sub (rule (with-stdout-to fromparent (echo parent))))
> EOF
$ mkdir sub
$ cat >sub/dune <<EOF
> (* -*- tuareg -*- *)
> let () = Jbuild_plugin.V1.send {|(rule (with-stdout-to bar (echo %{read:fromparent})))|};
> EOF
$ dune build ./sub/bar
$ cat _build/default/sub/bar
parent
subdir stanzas can also appear in included files
$ mkdir -p include/subdir; cd include
$ cat >dune-project <<EOF
> (lang dune 2.5)
> EOF
$ cat >dune <<EOF
> (include dune.inc)
> EOF
$ cat >dune.inc <<EOF
> (subdir subdir
> (rule
> (action (with-stdout-to hello.txt (echo "Hello from subdir\n")))))
> EOF
$ dune build --root . subdir/hello.txt
File "dune.inc", lines 1-3, characters 0-90:
1 | (subdir subdir
2 | (rule
3 | (action (with-stdout-to hello.txt (echo "Hello from subdir\n")))))
Error: Using a `subdir' stanza within an `include'd file is only available
since version 2.7 of the dune language. Please update your dune-project file
to have (lang dune 2.7).
[1]
$ cat >dune-project <<EOF
> (lang dune 2.7)
> EOF
$ dune build --root . subdir/hello.txt
$ cat _build/default/subdir/hello.txt
Hello from subdir
Include stanzas within subdir stanzas
$ mkdir -p subdir-include/a; cd subdir-include
$ cat >dune-project <<EOF
> (lang dune 2.5)
> EOF
$ cat >dune <<EOF
> (subdir a (include dune.inc))
> EOF
$ cat >a/dune.inc <<EOF
> (rule (with-stdout-to hello.txt (echo Hello!)))
> EOF
$ dune build --root . a/hello.txt
$ cat _build/default/a/hello.txt
Hello!
$ echo "(lang dune 2.5)" > dune-project
$ cat >dune <<EOF
> (rule (with-stdout-to foo.txt (echo "bar")))
> (subdir /absolute/path/to/bar
> (rule (with-stdout-to foo.txt (echo "bar"))))
> EOF
$ dune build ./foo.txt ./bar/foo.txt
File "dune", line 2, characters 8-29:
2 | (subdir /absolute/path/to/bar
^^^^^^^^^^^^^^^^^^^^^
Error: invalid sub-directory path "/absolute/path/to/bar"
Hint: sub-directory path must be relative
[1]

View file

@ -0,0 +1,9 @@
Directories with spaces
$ cat <<EOF >dune-project
> (lang dune 3.0)
> EOF
$ cat <<EOF >dune
> (subdir "foo bar" (rule (with-stdout-to foo (echo foo))))
> EOF
$ dune build

View file

@ -0,0 +1,25 @@
Aliases defined in data_only_dirs aren't traversed
$ cat >dune-project <<EOF
> (lang dune 3.8)
> EOF
$ cat >dune<<EOF
> (subdir foo
> (rule
> (alias run)
> (deps (universe))
> (action (echo run alias))))
> EOF
$ dune build @run
run alias
$ cat >> dune <<EOF
> (data_only_dirs foo)
> EOF
$ dune build @run
Error: Alias "run" specified on the command line is empty.
It is not defined in . or any of its descendants.
[1]

View file

@ -0,0 +1,33 @@
Demonstrate that we (wrongly) allow the expected file to be generated by a rule:
$ cat >dune-project <<EOF
> (lang dune 3.18)
> EOF
$ cat >dune <<EOF
> (rule (write-file foo.expected "foo bar"))
> (test
> (name foo))
> EOF
$ cat >foo.ml <<EOF
> print_endline "baz";;
> EOF
$ dune runtest
File "foo.expected", line 1, characters 0-0:
Error: Files _build/default/foo.expected and _build/default/foo.exe.output
differ.
[1]
The promotion wasn't registered
$ dune promotion list
Therefore, we keep re-running the test
$ dune runtest
File "foo.expected", line 1, characters 0-0:
Error: Files _build/default/foo.expected and _build/default/foo.exe.output
differ.
[1]

View file

@ -0,0 +1,6 @@
(rule
(with-stdout-to generated.expected (echo "foo\nbar\n")))
(test
(name generated)
(modules generated))

View file

@ -0,0 +1 @@
let () = print_endline "foo\nbaz"

View file

@ -0,0 +1,5 @@
$ dune runtest
File "generated.expected", line 1, characters 0-0:
Error: Files _build/default/generated.expected and
_build/default/generated.exe.output differ.
[1]

View file

@ -0,0 +1,3 @@
(test
(name modes)
(modes byte native))

View file

@ -0,0 +1 @@
testing multiple modes

View file

@ -0,0 +1 @@
print_endline "testing multiple modes"

View file

@ -0,0 +1,2 @@
(tests
(names expect_test regular_test regular_test2))

View file

@ -0,0 +1 @@
let () = print_endline "expect test"

View file

@ -0,0 +1 @@
let () = print_endline "regular test"

View file

@ -0,0 +1 @@
let () = print_endline "regular test2"

View file

@ -0,0 +1,3 @@
$ dune runtest
regular test
regular test2

View file

@ -0,0 +1,2 @@
(test
(name singular))

View file

@ -0,0 +1,2 @@
$ dune runtest
singular test

View file

@ -0,0 +1 @@
print_endline "singular test"

View file

@ -0,0 +1,3 @@
(test
(name test)
(action (run %{test})))

View file

@ -0,0 +1,9 @@
If there is an (action) field defined in a test stanza and dune-project sets
(lang dune v) with v < 1.2, a warning is emitted:
$ dune runtest
File "dune", line 3, characters 1-23:
3 | (action (run %{test})))
^^^^^^^^^^^^^^^^^^^^^^
Warning: 'action' is only available since version 1.2 of the dune language.
Please update your dune-project file to have (lang dune 1.2).

View file

@ -0,0 +1,4 @@
let show_argument n argument =
Printf.printf "argv[%d] = %S\n" n argument
let () = Array.iteri show_argument Sys.argv

View file

@ -0,0 +1,3 @@
(tests
(names my_test)
(action (run %{test} arg1 arg2 arg3)))

View file

@ -0,0 +1,4 @@
argv[0] = "./my_test.exe"
argv[1] = "arg1"
argv[2] = "arg2"
argv[3] = "arg3"

View file

@ -0,0 +1,4 @@
let show_argument n argument =
Printf.printf "argv[%d] = %S\n" n argument
let () = Array.iteri show_argument Sys.argv

View file

@ -0,0 +1,3 @@
(tests
(names my_test)
(action (run %{test} arg1 arg2 arg3)))

View file

@ -0,0 +1,4 @@
let show_argument n argument =
Printf.printf "argv[%d] = %S\n" n argument
let () = Array.iteri show_argument Sys.argv

View file

@ -0,0 +1,15 @@
If there is an (action) field, it is used to invoke to the executable (in both
regular and expect modes:
$ dune build @explicit-regular/runtest
argv[0] = "./my_test.exe"
argv[1] = "arg1"
argv[2] = "arg2"
argv[3] = "arg3"
$ dune build @explicit-expect/runtest
If there is no field, the program is run with no arguments:
$ dune build @default/runtest
argv[0] = "./my_test.exe"

View file

@ -0,0 +1,38 @@
Tests stanzas produce aliases with the executable names
$ cat > dune-project << EOF
> (lang dune 3.17)
> EOF
$ cat > dune << EOF
> (tests
> (names a b))
> EOF
$ cat > a.ml << EOF
> let () = print_endline "a"
> EOF
$ cat > b.ml << EOF
> let () = print_endline "b"
> EOF
$ dune build @runtest-a @runtest-b
a
b
Checking interaction with enabled_if
$ cat > dune << EOF
> (tests
> (names a b)
> (enabled_if false))
> EOF
$ dune build @a @b
Error: Alias "a" specified on the command line is empty.
It is not defined in . or any of its descendants.
Hint: did you mean all?
Error: Alias "b" specified on the command line is empty.
It is not defined in . or any of its descendants.
[1]

View file

@ -0,0 +1,26 @@
For a test based on an .expected file, the deps field is ignored.
This is visible when trying to build the `@all` alias. See #5950.
$ cat > dune-project << EOF
> (lang dune 3.3)
> EOF
$ cat > dune << EOF
> (test
> (name t)
> (deps data.txt))
> EOF
$ cat > t.ml << EOF
> let () =
> In_channel.with_open_bin "data.txt"
> (fun ic -> print_string (In_channel.input_all ic))
> EOF
$ cat > data.txt << EOF
> data
> EOF
$ cp data.txt t.expected
$ dune build t.exe.output

View file

@ -0,0 +1,9 @@
(library
(name fooppx)
(modules fooppx)
(libraries ppxlib)
(kind ppx_rewriter))
(toplevel
(name tt)
(preprocess (pps fooppx)))

View file

@ -0,0 +1,10 @@
open Ppxlib
let rules =
let extension =
Extension.declare "test" Expression Ast_pattern.__ (fun ~loc ~path:_ _ ->
Ast_builder.Default.eint ~loc 42)
in
[ Context_free.Rule.extension extension ]
let () = Ppxlib.Driver.register_transformation "rules" ~rules

View file

@ -0,0 +1,3 @@
let () =
Printf.printf "PPX extension: %d\n%!" [%test];
exit 0

View file

@ -0,0 +1,6 @@
Running toplevel with preprocessor
$ dune exec ./tt.exe -- -init init.ml | sed -E 's/OCaml version .*$/Ocaml version REDACTED/g'
Ocaml version REDACTED
Enter #help;; for help.
PPX extension: 42

View file

@ -0,0 +1,7 @@
(library
(name foo)
(modules (:standard \ init)))
(toplevel
(name tt)
(libraries foo))

View file

@ -0,0 +1,3 @@
let () =
Printf.printf "Foo.x = %d\n%!" Foo.x;
exit 0

View file

@ -0,0 +1,8 @@
Simple example to run toplevel
$ dune exec ./tt.exe -- -init init.ml | sed -E 's/OCaml version .*$/OCaml version REDACTED/g'
OCaml version REDACTED
Enter #help;; for help.
Foo.x = 42