Skip to content

Commit cf522f1

Browse files
Alizterrgrinberg
authored andcommitted
feature: add aliases for inline tests
For a given library with inlines tests, we add an alias with the name of the library for running those tests prefixed with `runtest-`. Further work on separating out the partition actions would allow us to have an alias for each partition. For now, this allows us to run inline tests with the runtest command. Signed-off-by: Ali Caglayan <alizter@gmail.com>
1 parent 10b1412 commit cf522f1

6 files changed

Lines changed: 100 additions & 12 deletions

File tree

doc/changes/11109.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Inline test libraries now produce aliases `runtest-name_of_lib` allowing
2+
users to run specific inline tests as `dune build @runtest-name_of_lib`.
3+
(#11109, partially adresses #10239, @Alizter)

doc/tests.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ instance, if we make the test fail by replacing ``120`` by ``0`` we get:
129129
130130
FAILED 1 / 1 tests
131131
132+
Every inline test library generates an alias with the library name prefixed by
133+
`inline-test-`. You can build the specific inline test library by running
134+
``dune build @inline-test-foo`` in this case.
135+
132136
Note that in this case Dune knew how to build and run the tests
133137
without any special configuration. This is because ``ppx_inline_test``
134138
defines an inline tests backend that's used by the library. Some

src/dune_rules/inline_tests.ml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,8 @@ include Sub_system.Register_end_point (struct
336336
~link_args
337337
~promote:None
338338
in
339-
let partitions_flags =
340-
partition_flags ~expander ~lib_name:(snd lib.name) ~backends
341-
in
339+
let lib_name = snd lib.name in
340+
let partitions_flags = partition_flags ~expander ~lib_name ~backends in
342341
let deps, sandbox =
343342
let sandbox =
344343
let project = Scope.project scope in
@@ -363,16 +362,27 @@ include Sub_system.Register_end_point (struct
363362
|> Action_builder.with_stdout_to partition_file
364363
|> Super_context.add_rule sctx ~dir ~loc
365364
in
366-
let* runtest_alias =
367-
match mode with
368-
| Native | Best | Byte -> Memo.return Alias0.runtest
369-
| Jsoo mode -> Jsoo_rules.js_of_ocaml_runtest_alias ~dir ~mode
365+
let alias =
366+
"runtest-" ^ Lib_name.Local.to_string lib_name
367+
|> Alias.Name.of_string
368+
|> Alias.make ~dir
369+
in
370+
let* () =
371+
let* runtest_alias =
372+
(match mode with
373+
| Native | Best | Byte -> Memo.return Alias0.runtest
374+
| Jsoo mode -> Jsoo_rules.js_of_ocaml_runtest_alias ~dir ~mode)
375+
>>| Alias.make ~dir
376+
in
377+
Dep.alias alias
378+
|> Action_builder.dep
379+
|> Rules.Produce.Alias.add_deps runtest_alias ~loc
370380
in
371381
Super_context.add_alias_action
372382
sctx
373383
~dir
374384
~loc:info.loc
375-
(Alias.make ~dir runtest_alias)
385+
alias
376386
(let open Action_builder.O in
377387
let source_files = List.concat_map source_modules ~f:Module.sources in
378388
let+ actions =
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
In this test we check a cycle when a library depends on a genrated source file which in
2+
turn depends on the inline-test-name alias of the inline tests of the library.
3+
4+
$ cat >dune-project <<EOF
5+
> (lang dune 3.18)
6+
> EOF
7+
8+
$ cat >test.ml <<EOF
9+
> (*TEST: assert (1 = 2) *)
10+
> EOF
11+
12+
$ cat >dune <<EOF
13+
> (library
14+
> (name backend_simple)
15+
> (modules ())
16+
> (inline_tests.backend
17+
> (generate_runner (run sed "s/(\\\\*TEST:\\\\(.*\\\\)\\\\*)/let () = if \\"%{inline_tests}\\" = \\"enabled\\" then \\\\1;;/" %{impl-files}))))
18+
>
19+
> (library
20+
> (name foo_simple)
21+
> (inline_tests (backend backend_simple)))
22+
>
23+
> (rule
24+
> (deps
25+
> (alias runtest-foo_simple))
26+
> (action
27+
> (with-outputs-to bar.ml
28+
> (echo "let message = \"Hello world\""))))
29+
> EOF
30+
31+
This kind of cycle has a difficult to understand error message.
32+
$ dune build 2>&1 | grep -vwE "sed"
33+
File "dune", lines 7-9, characters 0-69:
34+
7 | (library
35+
8 | (name foo_simple)
36+
9 | (inline_tests (backend backend_simple)))
37+
Error: Dependency cycle between:
38+
_build/default/.foo_simple.objs/foo_simple__Bar.impl.all-deps
39+
-> _build/default/.foo_simple.objs/byte/foo_simple__Bar.cmi
40+
-> _build/default/.foo_simple.inline-tests/.t.eobjs/byte/dune__exe__Main.cmi
41+
-> _build/default/.foo_simple.inline-tests/.t.eobjs/native/dune__exe__Main.cmx
42+
-> _build/default/.foo_simple.inline-tests/inline-test-runner.exe
43+
-> alias runtest-foo_simple in dune:9
44+
-> _build/default/bar.ml
45+
-> _build/default/.foo_simple.objs/foo_simple__Bar.impl.d
46+
-> _build/default/.foo_simple.objs/foo_simple__Bar.impl.all-deps
47+
-> required by _build/default/.foo_simple.objs/byte/foo_simple__Bar.cmo
48+
-> required by _build/default/foo_simple.cma
49+
-> required by alias all
50+
-> required by alias default

test/blackbox-tests/test-cases/inline_tests/parallel.t/run.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ First, build silently to avoid some noise
55
See that `test1/runtest`, which uses `fake_backend_1, only runs one inline test runner
66

77
$ dune build --display short @test1/runtest 2>&1 | grep alias
8-
inline-test-runner alias test1/runtest
8+
inline-test-runner alias test1/runtest-test_lib1
99

1010
See that `test2/runtest`, which uses `fake_backend_2`, runs one inline test runner per partition
1111

1212
$ dune build --display short @test2/runtest 2>&1 | grep alias
13-
inline-test-runner alias test2/runtest
14-
inline-test-runner alias test2/runtest
15-
inline-test-runner alias test2/runtest
13+
inline-test-runner alias test2/runtest-test_lib2
14+
inline-test-runner alias test2/runtest-test_lib2
15+
inline-test-runner alias test2/runtest-test_lib2
1616

1717
See that we indeed have 3 partitions
1818

test/blackbox-tests/test-cases/inline_tests/simple.t

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@
3030
Fatal error: exception File ".foo_simple.inline-tests/main.ml-gen", line 1, characters 40-46: Assertion failed
3131
[1]
3232

33+
Inline tests also generate an alias
34+
$ dune build @runtest-foo_simple
35+
File "dune", line 9, characters 1-40:
36+
9 | (inline_tests (backend backend_simple)))
37+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
Fatal error: exception File ".foo_simple.inline-tests/main.ml-gen", line 1, characters 40-46: Assertion failed
39+
[1]
40+
41+
Make sure building both aliases doesn't build both
42+
$ dune build @runtest @lib-foo_simple
43+
Error: Alias "lib-foo_simple" specified on the command line is empty.
44+
It is not defined in . or any of its descendants.
45+
File "dune", line 9, characters 1-40:
46+
9 | (inline_tests (backend backend_simple)))
47+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
Fatal error: exception File ".foo_simple.inline-tests/main.ml-gen", line 1, characters 40-46: Assertion failed
49+
[1]
50+
This test demonstrates that the action is being run once
51+
$ cat _build/log | sed 's/\$ //g' | grep inline-test-runner
52+
(cd _build/default && .foo_simple.inline-tests/inline-test-runner.exe)
53+
3354
The expected behavior for the following three tests is to output nothing: the tests are disabled or ignored.
3455
$ env -u OCAMLRUNPARAM dune runtest --profile release
3556

0 commit comments

Comments
 (0)