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,25 @@
In this test we make sure a cram test is not run twice when it belongs to two separate
aliases that are being built together.
$ cat >dune-project <<EOF
> (lang dune 3.17)
> EOF
$ cat >dune <<EOF
> (cram
> (runtest_alias true)
> (alias this))
> EOF
$ cat >foo.t <<EOF
> $ echo foo
> EOF
$ dune build @this @runtest
File "foo.t", line 1, characters 0-0:
Error: Files _build/default/foo.t and _build/default/foo.t.corrected differ.
[1]
Here we make sure that the cram test is only run once
$ cat _build/log | grep dune_cram | sed 's/.*dune_cram_[0-9a-f]*_/dune_cram_HASH_/g'
dune_cram_HASH_.cram.sh/main.sh)

View file

@ -0,0 +1,63 @@
Test the behavior when there are cram stanzas but cram tests are not enabled.
We need a sub-directory, otherwise the inner dune will see this run.t
file and we enter a loop:
$ mkdir test
$ cd test
$ cat >dune-project<<EOF
> (lang dune 2.8)
> EOF
$ cat >dune<<EOF
> (cram)
> EOF
$ cat >run.t<<EOF
> $ echo "Hello, world!"
> EOF
With older version of Dune, this would do nothing because of the
missing (cram enable) in the dune-project file:
$ dune runtest
File "dune", line 1, characters 0-6:
1 | (cram)
^^^^^^
Warning: Cram tests are not enabled in this project.
Hint: You can enable cram tests by adding (cram enable) to your dune-project
file.
Check that once we enable cram tests, the test are indeed being
executed:
$ echo "(cram enable)" >> dune-project
$ dune runtest
File "run.t", line 1, characters 0-0:
Error: Files _build/default/run.t and _build/default/run.t.corrected differ.
[1]
With Dune 3.0 and later, we don't get an error since cram tests are enabled by
default:
$ cat >dune-project<<EOF
> (lang dune 3.0)
> EOF
$ dune runtest
File "run.t", line 1, characters 0-0:
Error: Files _build/default/run.t and _build/default/run.t.corrected differ.
[1]
And if we disable them on purpose, we get an error message:
$ echo "(cram disable)" >> dune-project
$ dune runtest
File "dune", line 1, characters 0-6:
1 | (cram)
^^^^^^
Error: Cram tests are not enabled in this project.
Hint: You can enable cram tests by adding (cram enable) to your dune-project
file.
[1]

View file

@ -0,0 +1,27 @@
Create a cram test and try to run it with DUNE_BUILD_DIR set to an absolute
path
$ cat >dune-project <<EOF
> (lang dune 3.5)
> EOF
$ cat >foo.t <<EOF
> $ echo " $ echo bar" >bar.t
> $ dune runtest
> EOF
$ DUNE_BUILD_DIR=$PWD/tmp dune runtest --auto-promote
File "foo.t", line 1, characters 0-0:
Error: Files
$TESTCASE_ROOT/tmp/default/foo.t
and
$TESTCASE_ROOT/tmp/default/foo.t.corrected
differ.
Promoting
$TESTCASE_ROOT/tmp/default/foo.t.corrected
to foo.t.
[1]
$ sed -E '/\(pid: [0-9]+\)/{s//(pid: ###)/; s/instance.*/.../g; q;}' foo.t
$ echo " $ echo bar" >bar.t
$ dune runtest
Error: A running dune (pid: ###) ...

View file

@ -0,0 +1,35 @@
This test demonstrates that we pointlessly re-run cram tests
after they're promted
$ cat >dune-project<<EOF
> (lang dune 3.12)
> EOF
$ cat >foo.t <<EOF
> $ echo run >> $PWD/side-effect
> $ echo bazy
> EOF
$ dune runtest foo.t
File "foo.t", line 1, characters 0-0:
Error: Files _build/default/foo.t and _build/default/foo.t.corrected differ.
[1]
$ cat side-effect
run
$ dune promote
Promoting _build/default/foo.t.corrected to foo.t.
$ dune runtest foo.t
side-effect should only contain a single "run":
$ cat side-effect
run
However, if passing --force, we should still be able to re-run cram tests:
$ dune runtest foo.t --force
There should be two "run"s here, however there is only one:
$ cat side-effect
run

View file

@ -0,0 +1,3 @@
(cram
(applies_to subprocess)
(deps %{bin:ps}))

View file

@ -0,0 +1,32 @@
Expansion of enabled_if in the cram stanza
$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF
We define a test that is disabled by default but can be turned using the FOO
env var.
$ cat >dune <<EOF
> (cram
> (applies_to :whole_subtree)
> (enabled_if %{env:FOO=false}))
> EOF
$ mkdir sub
We set the FOO env var in the sub directory of the test, but this shouldn't
have an effect because environment variable should be expanded where the cram
stanza was defined.
$ cat >sub/dune <<EOF
> (env
> (_
> (env-vars (FOO true))))
> EOF
$ cat >sub/foo.t <<EOF
> $ echo should be disabled
> EOF
$ dune runtest

View file

@ -0,0 +1,40 @@
Cram and private binaries from the env stanza
$ mkdir fresh && cd fresh
$ cat >dune-project <<EOF
> (lang dune 2.8)
> (cram enable)
> EOF
$ mkdir helper
$ cat >helper/dune <<EOF
> (executable (name helper))
> EOF
$ cat >helper/helper.ml <<EOF
> print_endline "Helper launched successfully";;
> EOF
$ env_stanza="(env (_ (binaries (helper/helper.exe as helper))))"
$ test_stanza="(cram (deps %{bin:helper}))"
First we test case where the cram stanza is one level below
$ mkdir tests
$ cat >tests/run.t <<EOF
> $ helper
> Helper launched successfully
> EOF
$ printf "%s\n" $test_stanza > tests/dune
$ printf "%s\n" $env_stanza > dune
$ dune runtest
Next, we test the case where the cram stanza is in the same directory as the
env stanza:
$ printf "%s\n%s\n" $env_stanza $test_stanza > dune
$ mv tests/run.t ./
$ rm -r tests/
$ dune runtest

View file

@ -0,0 +1,4 @@
Error messages are neat and don't leak out generated file names.
$ <doesnotexist cat
cannot open doesnotexist: No such file
[2]

View file

@ -0,0 +1,23 @@
Check that actions don't have access to the outer git repository.
$ mkdir git
$ cd git
$ git init -q
$ echo '(lang dune 3.0)' > dune-project
$ cat >test.t <<"EOF"
> $ git rev-parse --show-toplevel
> EOF
$ dune runtest --auto-promote
File "test.t", line 1, characters 0-0:
Error: Files _build/default/test.t and _build/default/test.t.corrected
differ.
Promoting _build/default/test.t.corrected to test.t.
[1]
The inner call to git shouldn't be able to access the outer git repo:
$ cat test.t
$ git rev-parse --show-toplevel
fatal: invalid gitfile format: $TESTCASE_ROOT/git/_build/.sandbox/.git
[128]

View file

@ -0,0 +1,24 @@
We demonstrate the leaking of the display of internal processes when running
cram tests.
First we make a cram test:
$ cat > dune-project << EOF
> (lang dune 3.7)
> EOF
$ cat > mytest.t << EOF
> $ echo A
> B
> EOF
We need to avoid the special treatment of the test when INSIDE_DUNE is set:
$ unset INSIDE_DUNE
We do not observe the leaking of the display of internal processes when running
dune build. Note that we scrub the actual reported error due to the diff tool
being bogus.
$ bash -c 'set -o pipefail; dune build --always-show-command-line --root=. --diff-command="exit 1; echo" --display=short @runtest 2>&1 | grep -v "(cd"'
File "mytest.t", line 1, characters 0-0:
Command exited with code 1.
[1]

View file

@ -0,0 +1,31 @@
Check that actions don't have access to the outer hg repository.
$ mkdir hg
$ cd hg
We can't call "hg init" because "hg init" reads the fake .hg created
by Dune and fails. Thankfully, creating an empty .hg directory is
enough for a few hg commands such as "hg root" to succeed:
$ mkdir .hg
$ hg root
$TESTCASE_ROOT/hg
$ echo '(lang dune 3.0)' > dune-project
$ cat >test.t <<"EOF"
> $ hg root 2>&1 | sed 's/!$//'
> EOF
$ dune runtest --auto-promote
File "test.t", line 1, characters 0-0:
Error: Files _build/default/test.t and _build/default/test.t.corrected
differ.
Promoting _build/default/test.t.corrected to test.t.
[1]
The inner call to hg shouldn't be able to access the outer hg repo:
$ cat test.t
$ hg root 2>&1 | sed 's/!$//'
abort: repository requires features unknown to this Mercurial: Escaping the Dune sandbox
(see https://mercurial-scm.org/wiki/MissingRequirement for more information)

View file

@ -0,0 +1,48 @@
Cram tests inside (include_subdirs unqualified)
$ cat >dune-project <<EOF
> (lang dune 3.11)
> EOF
We have a file cram test inside a subdirectory and a directory cram test. When
the (include_subdirs unqualified) is not present, both tests fail as expected.
$ mkdir sub/
$ cat >sub/foo.t <<EOF
> $ echo foo
> EOF
$ mkdir bar.t
$ cat > bar.t/run.t <<EOF
> $ echo bar
> EOF
$ dune runtest
File "bar.t/run.t", line 1, characters 0-0:
Error: Files _build/default/bar.t/run.t and
_build/default/bar.t/run.t.corrected differ.
File "sub/foo.t", line 1, characters 0-0:
Error: Files _build/default/sub/foo.t and _build/default/sub/foo.t.corrected
differ.
[1]
Previously, adding (include_subdirs unqualified) highlights two issues:
1. The file cram test in the subdirectory is no longer being run.
2. Multiple rules are being generated for the directory test.
These have now been fixed, and both cram tests work correctly with
(include_subdirs unqualified) in tandem.
$ cat >dune <<EOF
> (include_subdirs unqualified)
> EOF
$ dune runtest
File "bar.t/run.t", line 1, characters 0-0:
Error: Files _build/default/bar.t/run.t and
_build/default/bar.t/run.t.corrected differ.
File "sub/foo.t", line 1, characters 0-0:
Error: Files _build/default/sub/foo.t and _build/default/sub/foo.t.corrected
differ.
[1]

View file

@ -0,0 +1,72 @@
Cram supports different kinds of tests.
For any kind of test to work, the following has to be put in dune-project:
$ cat > dune-project << EOF
> (lang dune 2.7)
> (cram enable)
> EOF
Tests can be in a single file.
$ mkdir file
$ cat > file/file.t << EOF
> $ echo File test
> EOF
$ dune runtest file
File "file/file.t", line 1, characters 0-0:
Error: Files _build/default/file/file.t and
_build/default/file/file.t.corrected differ.
[1]
$ dune promote file/file.t
Promoting _build/default/file/file.t.corrected to file/file.t.
$ cat file/file.t
$ echo File test
File test
$ dune runtest file
They can be in a test directory. In this case, a run.t file must be present. All
other files are visible within the test.
$ mkdir -p dir/dir.t
$ echo "Contents of file a" > dir/dir.t/a
$ cat > dir/dir.t/run.t << EOF
> $ echo Dir test
> $ cat a
> EOF
$ dune runtest dir
File "dir/dir.t/run.t", line 1, characters 0-0:
Error: Files _build/default/dir/dir.t/run.t and
_build/default/dir/dir.t/run.t.corrected differ.
[1]
$ dune promote dir/dir.t/run.t
Promoting _build/default/dir/dir.t/run.t.corrected to dir/dir.t/run.t.
$ cat dir/dir.t/run.t
$ echo Dir test
Dir test
$ cat a
Contents of file a
$ dune runtest dir
If there is no run.t file, an error message is displayed.
$ mkdir -p dir-no-run/dir.t
$ echo "Contents of file a" > dir-no-run/dir.t/a
$ dune runtest dir-no-run
File "dir-no-run/dir.t", line 1, characters 0-0:
Error: Cram test directory dir-no-run/dir.t does not contain a run.t file.
[1]
However, if the directory is empty, this check is skipped. (git can leave such
empty directories)
$ mkdir -p dir-empty/dir.t
$ dune runtest dir-empty

View file

@ -0,0 +1,47 @@
We can attach cram tests to packages
This sub dir is needed because we are running cram inside and cram, and we
don't want our own test file to be visible.
$ mkdir subdir
$ cd subdir
$ cat >dune-project <<EOF
> (lang dune 2.8)
> (cram enable)
> (package (name foo))
> (package (name bar))
> EOF
$ cat >dune <<EOF
> (cram
> (applies_to foo)
> (package foo))
> (cram
> (applies_to bar)
> (package bar))
> EOF
$ cat >foo.t <<EOF
> $ echo foo
> EOF
$ cat >bar.t <<EOF
> $ echo bar
> EOF
$ dune build @runtest --only-packages foo
File "foo.t", line 1, characters 0-0:
Error: Files _build/default/foo.t and _build/default/foo.t.corrected differ.
[1]
$ dune promote foo.t
Promoting _build/default/foo.t.corrected to foo.t.
$ dune build @runtest --only-packages bar
File "bar.t", line 1, characters 0-0:
Error: Files _build/default/bar.t and _build/default/bar.t.corrected differ.
[1]
$ dune promote bar.t
Promoting _build/default/bar.t.corrected to bar.t.

View file

@ -0,0 +1,30 @@
Cram and public binaries with an absolute build directory
$ cat >dune-project <<EOF
> (lang dune 2.8)
> (cram enable)
> (name public-name-exe-test)
> EOF
$ touch public-name-exe-test.opam
$ mkdir helper
$ cat >helper/dune <<EOF
> (executable (public_name helper))
> EOF
$ cat >helper/helper.ml <<EOF
> print_endline "Helper launched successfully";;
> EOF
$ mkdir tests
$ cat >tests/run.t <<EOF
> $ helper
> Helper launched successfully
> EOF
$ echo "(cram (deps %{bin:helper}))" > tests/dune
Running `dune runtest` with a relative build directory works
$ DUNE_BUILD_DIR=./_other_build dune runtest
$ export NEW_BUILD_DIR="$PWD/_other_build"
$ DUNE_BUILD_DIR="$NEW_BUILD_DIR" dune runtest --auto-promote

View file

@ -0,0 +1,45 @@
Demonstrate that cram tests that add new commands are correctly executed:
$ cat >dune-project <<EOF
> (lang dune 3.17)
> EOF
$ cat >test.t <<EOF
> $ echo "$ echo foo"
> EOF
$ runTest() {
> dune runtest test.t
> dune promote
> cat test.t
> }
$ runTest
File "test.t", line 1, characters 0-0:
Error: Files _build/default/test.t and _build/default/test.t.corrected
differ.
Promoting _build/default/test.t.corrected to test.t.
$ echo "$ echo foo"
$ echo foo
$ runTest
File "test.t", line 1, characters 0-0:
Error: Files _build/default/test.t and _build/default/test.t.corrected
differ.
Promoting _build/default/test.t.corrected to test.t.
$ echo "$ echo foo"
$ echo foo
$ echo foo
foo
$ runTest
File "test.t", line 1, characters 0-0:
Error: Files _build/default/test.t and _build/default/test.t.corrected
differ.
Promoting _build/default/test.t.corrected to test.t.
$ echo "$ echo foo"
$ echo foo
$ echo foo
foo
$ echo foo
foo

View file

@ -0,0 +1,44 @@
Control the default runtest alias for cram tests
$ cat >dune-project <<EOF
> (lang dune 3.12)
> EOF
$ cat >dune <<EOF
> (cram
> (runtest_alias false)
> (alias this))
> EOF
$ cat >foo.t <<EOF
> $ echo foo
> EOF
This shouldn't run the test
$ dune runtest
This should run the test
$ dune build @this
File "foo.t", line 1, characters 0-0:
Error: Files _build/default/foo.t and _build/default/foo.t.corrected differ.
[1]
Now we try setting runtest alias default twice. This should be impossible:
$ cat >dune <<EOF
> (cram (runtest_alias false))
> (cram (runtest_alias true))
> EOF
$ dune build @a
File "dune", line 2, characters 21-25:
2 | (cram (runtest_alias true))
^^^^
Error: enabling or disabling the runtest alias for a cram test may only be
set once.
It's already set for the test "foo"
The first definition is at:
dune:1
[1]

View file

@ -0,0 +1,29 @@
Test of the cram test framework itself
Multiline commands
$ cat <<EOF
> Multiline
> Text
> EOF
Multiline
Text
The environment is preserved across phrases
$ TOTO=hello
$ echo $TOTO
hello
$ mkdir -p toto
$ cd toto
$ pwd
$TESTCASE_ROOT/toto
$ cd ..
Printing stuff with backslashes
$ cat <<EOF
> abc \ def \n hij
> EOF
abc \ def \n hij

View file

@ -0,0 +1,30 @@
Testing the termination of subprocesses in cram tests. We first create a dune
project with a single cram test.
$ cat > dune-project <<EOF
> (lang dune 3.19)
> EOF
We create a file for tracking the PID of a subprocess.
$ pidFile="$PWD/pid.txt"
We create a cram test that spawns a subprocess and records its PID in the file
we gave before.
$ cat > mycram.t <<EOF
> $ sleep 5 &
> $ echo \$! > $pidFile
> EOF
We can now run this test, which will record its PID in the file.
$ dune runtest mycram.t
The test finished successfully, now we make sure that the PID was correctly
terminated.
$ [ -s $pidFile ] && echo pid file created
pid file created
$ ps -p $(cat $pidFile) > /dev/null || echo "Process terminated"
Process terminated

View file

@ -0,0 +1,42 @@
Demonstrate the files and directories listed in a cram test:
$ cat >test.t <<EOF
> $ find . | sort
> EOF
$ dune runtest test.t
File "test.t", line 1, characters 0-0:
Error: Files _build/default/test.t and _build/default/test.t.corrected
differ.
[1]
$ dune promote
Promoting _build/default/test.t.corrected to test.t.
$ cat test.t
$ find . | sort
.
$ ls _build/default | sort
test.t
Now repeat the test for a test defined using a directory:
$ mkdir foo.t
$ cat >foo.t/run.t <<EOF
> $ find . | sort
> EOF
$ dune runtest foo.t
File "foo.t/run.t", line 1, characters 0-0:
Error: Files _build/default/foo.t/run.t and
_build/default/foo.t/run.t.corrected differ.
[1]
$ dune promote
Promoting _build/default/foo.t/run.t.corrected to foo.t/run.t.
$ cat foo.t/run.t
$ find . | sort
.
$ ( cd _build/default/foo.t && find . | sort )
.
./run.t

View file

@ -0,0 +1,125 @@
Here we check the validation of the timeout field of the cram stanza.
First we check the version guard.
$ cat > dune-project <<EOF
> (lang dune 3.19)
> EOF
$ cat > dune <<EOF
> (cram
> (timeout 1))
> EOF
$ dune build
File "dune", line 2, characters 1-12:
2 | (timeout 1))
^^^^^^^^^^^
Error: 'timeout' is only available since version 3.20 of the dune language.
Please update your dune-project file to have (lang dune 3.20).
[1]
Next we check some invalid values:
$ cat > dune-project <<EOF
> (lang dune 3.20)
> EOF
$ cat > test.t <<EOF
> $ echo hi
> EOF
Negative values fail immediately.
$ cat > dune <<EOF
> (cram
> (timeout -1.0))
> EOF
$ dune test test.t
File "dune", line 2, characters 10-14:
2 | (timeout -1.0))
^^^^
Error: Timeout value must be a non-negative float.
[1]
Checking some currently accepted float values:
$ test() {
> echo "(cram (timeout $1))" > dune
> dune build
> }
$ test -1
File "dune", line 1, characters 15-17:
1 | (cram (timeout -1))
^^
Error: Timeout value must be a non-negative float.
[1]
$ test Inf
$ test +Inf
$ test -Inf
File "dune", line 1, characters 15-19:
1 | (cram (timeout -Inf))
^^^^
Error: Timeout value must be a non-negative float.
[1]
$ test nan
File "dune", line 1, characters 15-18:
1 | (cram (timeout nan))
^^^
Error: Timeout value must be a non-negative float.
[1]
$ test .5
$ test 0.
$ test 1.
$ test 1.0e1
$ test 1e1
$ test 1e-1
$ test 1e+1
$ test 1e308
$ test 1e-324
Invalid values should be vetted correctly:
$ test
File "dune", line 1, characters 6-16:
1 | (cram (timeout ))
^^^^^^^^^^
Error: Not enough arguments for "timeout"
[1]
$ test foo
File "dune", line 1, characters 15-18:
1 | (cram (timeout foo))
^^^
Error: Float expected
[1]
$ test --1
File "dune", line 1, characters 15-18:
1 | (cram (timeout --1))
^^^
Error: Float expected
[1]
$ test 1..0
File "dune", line 1, characters 15-19:
1 | (cram (timeout 1..0))
^^^^
Error: Float expected
[1]
$ test 1e
File "dune", line 1, characters 15-17:
1 | (cram (timeout 1e))
^^
Error: Float expected
[1]
$ test 1.0.0
File "dune", line 1, characters 15-20:
1 | (cram (timeout 1.0.0))
^^^^^
Error: Float expected
[1]

View file

@ -0,0 +1,61 @@
Testing how timeout affects the digest:
$ cat > dune-project <<EOF
> (lang dune 3.20)
> EOF
$ cat > mytest.t
This test counts the occurances of the cram script in the log.
$ check() {
> cat _build/log | grep -c main.sh
> }
We can observe the test is run the first time:
$ dune test mytest.t
$ check
1
And is not run the second time:
$ dune test mytest.t
$ check
0
[1]
If we add a timeout, we would not expect for the digest of the cram test to
change.
$ cat > dune <<EOF
> (cram
> (timeout 1))
> EOF
However this is currently not the case and we rerun the cram test:
$ dune test mytest.t
$ check
1
$ dune test mytest.t
$ check
0
[1]
This is again the case on another time change:
$ cat > dune <<EOF
> (cram
> (timeout 2))
> EOF
$ dune test mytest.t
$ check
1
$ dune test mytest.t
$ check
0
[1]

View file

@ -0,0 +1,46 @@
Testing the timeout functionality of cram tests.
First we create a cram test that will take less than our time budget. This will
allow the test to fail. (Since "hi" needs to be promoted).
$ cat > dune-project <<EOF
> (lang dune 3.20)
> EOF
$ cat > dune <<EOF
> (cram
> (timeout 1))
> EOF
$ cat > test.t <<EOF
> $ echo hi
> EOF
$ dune test test.t
File "test.t", line 1, characters 0-0:
Error: Files _build/default/test.t and _build/default/test.t.corrected
differ.
[1]
Next we create a cram test that will take longer than our timeout budget which
will cause dune to kill the test.
$ cat > dune <<EOF
> (cram
> (timeout 0.0))
> EOF
$ cat > test.t <<EOF
> $ echo hi
> $ sleep 2
> EOF
The cram test will take 2 seconds to run unless it is killed. We make sure this
fails earlier by passing a timeout command in front of dune. Our expected
behaviour is for dune to kill the cram test immediately.
$ timeout 1 dune test test.t
File "test.t", line 1, characters 0-0:
Error: Cram test timed out. A time limit of 0.00s has been set in dune:2.
[1]