Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Jun 6, 2023
1 parent 92e234c commit b5b5e2a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 28 deletions.
89 changes: 61 additions & 28 deletions test/blackbox-tests/test-cases/test-build-if/feature.t
Original file line number Diff line number Diff line change
@@ -1,43 +1,76 @@
enabled_if has a limitation: it attempts building even if enabled_if evaluates to false.

$ cat > dune-project << EOF
> (lang dune 3.9)
> EOF

The fields evaluates to true then var=true is passed.

$ cat > dune << EOF
> (test
> (name t)
> (build_if %{env:BUILD=false})
> (enabled_if %{env:RUN=false}))
> (enabled_if %{env:ENABLED=false}))
> EOF

$ cat > t.ml << EOF
> broken
> EOF
$ touch t.ml

We test the various combinations:

$ test_one () {
> dune clean
> output=$( dune build "$1" --display short 2>&1 )
> echo When building $1 with ENABLED=${ENABLED:-unset}:
> if echo $output|grep -q ocamlopt ; then
> echo ' build was done: YES'
> else
> echo ' build was done: NO'
> fi
> if echo $output|grep -q "alias runtest" ; then
> echo ' test did run: YES'
> else
> echo ' test did run: NO'
> fi
> }

The build is attempted only when the enabled_if clause evaluates to true.
$ test_all () {
> test_one @all
> test_one @runtest
> ENABLED=true test_one @all
> ENABLED=true test_one @runtest
> }

$ dune build
$ BUILD=true dune build
File "t.ml", line 1, characters 0-6:
1 | broken
^^^^^^
Error: Unbound value broken
[1]
$ test_all
When building @all with ENABLED=unset:
build was done: YES
test did run: NO
When building @runtest with ENABLED=unset:
build was done: NO
test did run: NO
When building @all with ENABLED=true:
build was done: YES
test did run: NO
When building @runtest with ENABLED=true:
build was done: YES
test did run: YES

In the presence of both enabled_if and build_if, the test only runs if both
conditions hold.
Now with build_if:

$ cat > t.ml << EOF
> let () = exit 1
$ cat > dune << EOF
> (test
> (name t)
> (build_if %{env:ENABLED=false}))
> EOF

$ dune runtest
$ BUILD=true dune runtest
$ RUN=true dune runtest
$ BUILD=true RUN=true dune runtest
File "dune", line 2, characters 7-8:
2 | (name t)
^
Command exited with code 1.
[1]
Notice that in the first case, nothing is done at all:

$ test_all
When building @all with ENABLED=unset:
build was done: NO
test did run: NO
When building @runtest with ENABLED=unset:
build was done: NO
test did run: NO
When building @all with ENABLED=true:
build was done: YES
test did run: NO
When building @runtest with ENABLED=true:
build was done: YES
test did run: YES
42 changes: 42 additions & 0 deletions test/blackbox-tests/test-cases/test-build-if/package.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
build_if is compatible with package.

This is important to test because in that case, (test) can not be split into two stanzas:

$ cat > dune-project << EOF
> (lang dune 3.9)
>
> (package (name a) (allow_empty))
> EOF

$ cat > dune << EOF
> (test
> (name t)
> (package a)
> (build_if %{env:ENABLED=false}))
> EOF

$ touch t.ml

$ dune runtest

If we try to split it we get an error:

$ cat > dune << EOF
> (executable
> (name t)
> (package a)
> (enabled_if %{env:ENABLED=false}))
>
> (rule
> (alias runtest)
> (action (run ./t.exe))
> (package a)
> (enabled_if %{env:ENABLED=false}))
> EOF

$ dune runtest
File "dune", line 3, characters 1-12:
3 | (package a)
^^^^^^^^^^^
Error: This field is useless without a (public_name ...) field.
[1]

0 comments on commit b5b5e2a

Please sign in to comment.