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,15 @@
(cram
(applies_to :whole_subtree)
(deps
(package mdx))
(alias all-mdx-tests)
(enabled_if
; temporary workaround for #10874
(<> %{system} macosx)))
(cram
(applies_to shared-libraries)
(deps
%{bin:gcc}
%{bin:ar}
(package mdx)))

View file

@ -0,0 +1,2 @@
(mdx
(enabled_if true))

View file

@ -0,0 +1,2 @@
(lang dune 2.8)
(using mdx 0.1)

View file

@ -0,0 +1,9 @@
(enabled_if) needs a recent (lang dune):
$ dune runtest
File "dune", line 2, characters 1-18:
2 | (enabled_if true))
^^^^^^^^^^^^^^^^^
Error: 'enabled_if' is only available since version 2.9 of the dune language.
Please update your dune-project file to have (lang dune 2.9).
[1]

View file

@ -0,0 +1,7 @@
(mdx
(files iftrue.md)
(enabled_if true))
(mdx
(files iffalse.md)
(enabled_if false))

View file

@ -0,0 +1,2 @@
(lang dune 2.9)
(using mdx 0.1)

View file

@ -0,0 +1,3 @@
```ocaml
# print_endline "run in iffalse";;
```

View file

@ -0,0 +1,3 @@
```ocaml
# print_endline "run in iftrue";;
```

View file

@ -0,0 +1,7 @@
The mdx stanza supports (enabled_if):
$ dune runtest
File "iftrue.md", line 1, characters 0-0:
Error: Files _build/default/iftrue.md and
_build/default/.mdx/iftrue.md.corrected differ.
[1]

View file

@ -0,0 +1,59 @@
Dune should know about the fact that mdx reads the MDX_RUN_NON_DETERMINISTIC
variable. When using the stanza 0.2, it is the mdx driver that reads that
variable. This is only the case since mdx 2.1.0.
$ cat > dune-project << EOF
> (lang dune 3.0)
> (using mdx 0.2)
> EOF
$ cat > dune << EOF
> (mdx (files README.md))
> EOF
$ cat > README.md << 'EOF'
> ```ocaml
> # "a";;
> ```
>
> ```ocaml non-deterministic
> # "b";;
> ```
> EOF
$ dune runtest --auto-promote
File "README.md", line 1, characters 0-0:
Error: Files _build/default/README.md and
_build/default/.mdx/README.md.corrected differ.
Promoting _build/default/.mdx/README.md.corrected to README.md.
[1]
$ cat README.md
```ocaml
# "a";;
- : string = "a"
```
```ocaml non-deterministic
# "b";;
```
$ dune runtest
$ MDX_RUN_NON_DETERMINISTIC=1 dune runtest --auto-promote
File "README.md", line 1, characters 0-0:
Error: Files _build/default/README.md and
_build/default/.mdx/README.md.corrected differ.
Promoting _build/default/.mdx/README.md.corrected to README.md.
[1]
$ cat README.md
```ocaml
# "a";;
- : string = "a"
```
```ocaml non-deterministic
# "b";;
- : string = "b"
```

View file

@ -0,0 +1,39 @@
When mdx is used to generate a program, the program will read the preludes at
run time. This test makes sure that this is recorded as a dependency.
$ cat > dune << EOF
> (mdx (preludes prelude.ml))
> EOF
$ cat > dune-project << EOF
> (lang dune 3.6)
> (using mdx 0.3)
> EOF
$ cat > prelude.ml << EOF
> let foo () = 1
> EOF
$ cat > README.md << 'EOF'
> ```ocaml
> # foo ();;
> - : int = 1
> ```
> EOF
$ dune runtest
$ echo 'let foo () = 2' > prelude.ml
$ dune runtest --auto-promote
File "README.md", line 1, characters 0-0:
Error: Files _build/default/README.md and
_build/default/.mdx/README.md.corrected differ.
Promoting _build/default/.mdx/README.md.corrected to README.md.
[1]
$ cat README.md
```ocaml
# foo ();;
- : int = 2
```

View file

@ -0,0 +1,14 @@
Building the executable generated by the MDX stanza works when implicit
transitive dependencies are disabled.
$ cat >dune-project <<EOF
> (lang dune 3.0)
> (using mdx 0.2)
> (implicit_transitive_deps false)
> EOF
$ cat >dune <<EOF
> (mdx)
> EOF
$ dune build

View file

@ -0,0 +1,39 @@
The MDX stanza requires dune lang 2.4 or higher
$ cat >dune-project <<EOF
> (lang dune 2.3)
> (using mdx 0.1)
> EOF
$ dune build @install
File "dune-project", line 2, characters 11-14:
2 | (using mdx 0.1)
^^^
Warning: Version 0.1 of mdx extension to verify code blocks in .md files is
not supported until version 2.4 of the dune language.
There are no supported versions of this extension in version 2.3 of the dune
language.
File "dune", line 1, characters 0-5:
1 | (mdx)
^^^^^
Error: 'mdx' is only available since version 2.4 of the dune language. Please
update your dune-project file to have (lang dune 2.4).
[1]
The version 0.2 requires dune 3.0
$ cat >dune-project <<EOF
> (lang dune 2.9)
> (using mdx 0.2)
> (cram disable)
> EOF
$ dune build @install
File "dune-project", line 2, characters 11-14:
2 | (using mdx 0.2)
^^^
Error: Version 0.2 of mdx extension to verify code blocks in .md files is not
supported until version 3.0 of the dune language.
Supported versions of this extension in version 2.9 of the dune language:
- 0.1
[1]

View file

@ -0,0 +1,6 @@
We can use our local libraries, in our documentation thanks to dune's mdx stanza:
```ocaml
# Private_lib.x;;
- : int = 42
```

View file

@ -0,0 +1,3 @@
(mdx
(files README.md)
(libraries private_lib))

View file

@ -0,0 +1,3 @@
(library
(name private_lib)
(modules private_lib))

View file

@ -0,0 +1,25 @@
Since 0.2 you can use the `libraries` field to have them linked into the test executable
$ cat >dune-project <<EOF
> (lang dune 3.0)
> (using mdx 0.2)
> (cram disable)
> EOF
$ dune runtest
MDX stanza 0.1 does not support linking libraries
$ cat >dune-project <<EOF
> (lang dune 2.8)
> (using mdx 0.1)
> EOF
$ dune runtest
File "dune", line 3, characters 1-24:
3 | (libraries private_lib))
^^^^^^^^^^^^^^^^^^^^^^^
Error: 'libraries' is only available since version 0.2 of mdx extension to
verify code blocks in .md files. Please update your dune-project file to have
(using mdx 0.2).
[1]

View file

@ -0,0 +1,3 @@
```ocaml
This isn't valid OCaml, so running MDX would fail.
```

View file

@ -0,0 +1,3 @@
(mdx
(files README.md)
(packages pkg))

View file

@ -0,0 +1,4 @@
(lang dune 2.4)
(name pkg)
(using mdx 0.1)

View file

@ -0,0 +1,3 @@
(library
(name public_lib)
(public_name pkg))

View file

@ -0,0 +1,13 @@
Dune does not fail if the `packages` are not available at evaluation time
(regression test fixed by ocaml/dune#3650)
$ dune build -p unrelated-package
Dune fails if the `packages` are not available at execution time
$ dune runtest -p unrelated-package
File "dune", line 3, characters 11-14:
3 | (packages pkg))
^^^
Error: Package pkg does not exist
[1]

View file

@ -0,0 +1,14 @@
We can use our local packages, in our documentation thanks to dune's mdx stanza:
```ocaml
# #require "pkg.public-lib";;
# Public_lib.x;;
- : int = 0
```
We can also use our local public executables:
```sh
$ public-bin
Hey!
```

View file

@ -0,0 +1,4 @@
(executable
(name public_bin)
(public_name public-bin)
(modules public_bin))

View file

@ -0,0 +1 @@
let () = Printf.printf "Hey!\n"

View file

@ -0,0 +1,3 @@
(mdx
(files README.md)
(packages pkg))

View file

@ -0,0 +1,4 @@
(lang dune 2.4)
(name pkg)
(using mdx 0.1)

View file

@ -0,0 +1,4 @@
(library
(name public_lib)
(public_name pkg.public-lib)
(modules public_lib))

View file

@ -0,0 +1,4 @@
You can make local packages available to mdx by using the `packages` field of
the stanza
$ dune runtest

View file

@ -0,0 +1,29 @@
Version 0.2 of the mdx stanza does not support (locks):
$ cat > dune-project << EOF
> (lang dune 3.2)
> (using mdx 0.2)
> EOF
$ cat > dune << EOF
> (mdx
> (locks l))
> EOF
$ dune build
File "dune", line 2, characters 1-10:
2 | (locks l))
^^^^^^^^^
Error: 'locks' is only available since version 0.3 of mdx extension to verify
code blocks in .md files. Please update your dune-project file to have (using
mdx 0.3).
[1]
In version 0.3, it is accepted:
$ cat > dune-project << EOF
> (lang dune 3.2)
> (using mdx 0.3)
> EOF
$ dune build

View file

@ -0,0 +1,15 @@
Dependencies for file-include mdx blocks should be computed by dune, using
`ocaml-mdx deps`:
```ocaml file=example.ml
let x = 1
```
Same goes for mdx's `dir` labels:
```sh dir=stuff
$ ls
blah
bleh
blih
```

View file

@ -0,0 +1,2 @@
(mdx
(files README.md))

View file

@ -0,0 +1,3 @@
(lang dune 2.4)
(using mdx 0.1)

View file

@ -0,0 +1,4 @@
Dune should invoke `ocaml-mdx deps` to figure out the files and directories a markdown
or mli to-be-mdxed file depends upon
$ dune runtest

View file

@ -0,0 +1,6 @@
We do some serious OCaml here
```ocaml
# 1 + 1;;
- : int = 3
```

View file

@ -0,0 +1,2 @@
(mdx
(files README.md))

View file

@ -0,0 +1,3 @@
(lang dune 2.4)
(using mdx 0.1)

View file

@ -0,0 +1,7 @@
You can use the mdx stanza to check your documentation in markdown and mli files
$ dune runtest
File "README.md", line 1, characters 0-0:
Error: Files _build/default/README.md and
_build/default/.mdx/README.md.corrected differ.
[1]

View file

@ -0,0 +1,137 @@
The project has an `.mld` file that needs to be fixed. At first, we determine
that that file is not being picked up:
$ cat > dune-project <<EOF
> (lang dune 3.7)
> (using mdx 0.3)
> EOF
$ cat > dune <<EOF
> (mdx)
> EOF
> cat > needs-fixes.md <<'EOF'
> This is a sample md file. It has some code that is invalid.
>
> ```ocaml
> # List.map (fun x -> x * x) [(1 + 9); 2; 3; 4];;
> - : int list = [1; 2; 3; 8]
> ```
>
> A run of MDX should output a fixed version.
> EOF
$ cat > needs-fixes.mld <<EOF
> This is a sample mld file. It has some code that is invalid.
>
> {[
> # List.map (fun x -> x * x) [(1 + 9); 2; 3; 4];;
> - : int list = [1; 2; 3; 8]
> ]}
>
> A run of MDX should output a fixed version.
> EOF
$ dune runtest
File "needs-fixes.md", line 1, characters 0-0:
Error: Files _build/default/needs-fixes.md and
_build/default/.mdx/needs-fixes.md.corrected differ.
[1]
It did pick up the error in the `.md` file, so let's promote the fix.
$ dune promote
Promoting _build/default/.mdx/needs-fixes.md.corrected to needs-fixes.md.
$ dune runtest
So the tests pass now, but it only uses the default `.md` glob for files to
process.
Let's also run the test for `.mld` files. These need to be manually passed, at
least as of mdx stanza version 0.3 and require MDX 2.3.0 at minimum.
$ cat > dune <<EOF
> (mdx
> (files needs-fixes.md needs-fixes.mld))
> EOF
$ dune runtest
File "needs-fixes.mld", line 1, characters 0-0:
Error: Files _build/default/needs-fixes.mld and
_build/default/.mdx/needs-fixes.mld.corrected differ.
[1]
The error in the `.mld` file was found, promoting should work and re-running
the test should succeed this time.
$ dune promote
Promoting _build/default/.mdx/needs-fixes.mld.corrected to needs-fixes.mld.
$ dune runtest
The 0.4 version of the stanza adds support for `.mld` files by default, so bump
the stanza version.
$ cat > dune-project <<EOF
> (lang dune 3.7)
> (using mdx 0.4)
> EOF
$ cat > dune <<EOF
> (mdx)
> EOF
$ cat > needs-fixes.mld <<EOF
> This is a sample mld file. It has some code that is invalid.
>
> {[
> # List.map (fun x -> x * x) [(1 + 9); 2; 3; 4];;
> - : int list = [1; 2; 3; 8]
> ]}
>
> A run of MDX should output a fixed version.
> EOF
0.4 is only supported since dune-lang 3.8, so attempting to use it should fail:
$ dune runtest
File "dune-project", line 2, characters 11-14:
2 | (using mdx 0.4)
^^^
Error: Version 0.4 of mdx extension to verify code blocks in .md files is not
supported until version 3.8 of the dune language.
Supported versions of this extension in version 3.7 of the dune language:
- 0.1 to 0.3
[1]
Updating the dune-lang should make the test run.
$ cat > dune-project <<EOF
> (lang dune 3.8)
> (using mdx 0.4)
> EOF
$ dune runtest
File "needs-fixes.mld", line 1, characters 0-0:
Error: Files _build/default/needs-fixes.mld and
_build/default/.mdx/needs-fixes.mld.corrected differ.
[1]
$ dune promote
Promoting _build/default/.mdx/needs-fixes.mld.corrected to needs-fixes.mld.
$ dune runtest
We also make sure that `:standard` resolves properly:
$ cat > dune <<EOF
> (mdx
> (files :standard))
> EOF
$ cat > needs-fixes.mld <<EOF
> This is a sample mld file. It has some code that is invalid.
>
> {[
> # List.map (fun x -> x * x) [(1 + 9); 2; 3; 4];;
> - : int list = [1; 2; 3; 8]
> ]}
>
> A run of MDX should output a fixed version.
> EOF
$ dune runtest
File "needs-fixes.mld", line 1, characters 0-0:
Error: Files _build/default/needs-fixes.mld and
_build/default/.mdx/needs-fixes.mld.corrected differ.
[1]
$ dune promote
Promoting _build/default/.mdx/needs-fixes.mld.corrected to needs-fixes.mld.
$ dune runtest

View file

@ -0,0 +1,13 @@
We can use our local packages, in our documentation thanks to dune's mdx stanza:
```ocaml
# #require "pkg.public-lib";;
# Public_lib.x;;
- : int = 1
```
```ocaml
# #require "pkg2.public-lib2";;
# Public_lib2.x;;
- : int = 2
```

View file

@ -0,0 +1,4 @@
(mdx
(files README.md)
(deps (package pkg2))
(packages pkg))

View file

@ -0,0 +1,4 @@
(library
(name public_lib)
(public_name pkg.public-lib)
(modules public_lib))

View file

@ -0,0 +1,4 @@
(library
(name public_lib2)
(public_name pkg2.public-lib2)
(modules public_lib2))

View file

@ -0,0 +1,31 @@
Since version 0.2 the mdx stanza supports a more generic `deps` field.
A deprecation warning is raised if using the olf `packages` field.
$ cat >dune-project <<EOF
> (lang dune 3.0)
> (using mdx 0.2)
> (cram disable)
> EOF
$ dune runtest
File "dune", line 4, characters 1-15:
4 | (packages pkg))
^^^^^^^^^^^^^^
Warning: 'packages' was deprecated in version 0.2 of mdx extension to verify
code blocks in .md files.
But using the new field with the old stanza would fail
$ cat >dune-project <<EOF
> (lang dune 2.8)
> (using mdx 0.1)
> EOF
$ dune runtest
File "dune", line 3, characters 1-22:
3 | (deps (package pkg2))
^^^^^^^^^^^^^^^^^^^^^
Error: 'deps' is only available since version 0.2 of mdx extension to verify
code blocks in .md files. Please update your dune-project file to have (using
mdx 0.2).
[1]

View file

@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```

View file

@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```

View file

@ -0,0 +1,10 @@
(mdx
(files doc-a.md)
(package a))
(mdx
(files doc-b.md)
(package b))
(mdx
(files doc-nopkg.md))

View file

@ -0,0 +1,2 @@
(lang dune 2.8)
(using mdx 0.1)

View file

@ -0,0 +1,9 @@
(package) needs a recent (lang dune):
$ dune runtest --only-packages b
File "dune", line 3, characters 1-12:
3 | (package a))
^^^^^^^^^^^
Error: 'package' is only available since version 2.9 of the dune language.
Please update your dune-project file to have (lang dune 2.9).
[1]

View file

@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```

View file

@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```

View file

@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```

View file

@ -0,0 +1,10 @@
(mdx
(files doc-a.md)
(package a))
(mdx
(files doc-b.md)
(package b))
(mdx
(files doc-nopkg.md))

View file

@ -0,0 +1,2 @@
(lang dune 2.9)
(using mdx 0.1)

View file

@ -0,0 +1,23 @@
The mdx stanza supports (package):
$ dune runtest
File "doc-a.md", line 1, characters 0-0:
Error: Files _build/default/doc-a.md and
_build/default/.mdx/doc-a.md.corrected differ.
File "doc-b.md", line 1, characters 0-0:
Error: Files _build/default/doc-b.md and
_build/default/.mdx/doc-b.md.corrected differ.
File "doc-nopkg.md", line 1, characters 0-0:
Error: Files _build/default/doc-nopkg.md and
_build/default/.mdx/doc-nopkg.md.corrected differ.
[1]
In the following test doc-a is not checked because it is not part of package b
$ dune runtest --only-packages b
File "doc-b.md", line 1, characters 0-0:
Error: Files _build/default/doc-b.md and
_build/default/.mdx/doc-b.md.corrected differ.
File "doc-nopkg.md", line 1, characters 0-0:
Error: Files _build/default/doc-nopkg.md and
_build/default/.mdx/doc-nopkg.md.corrected differ.
[1]

View file

@ -0,0 +1,109 @@
Absolute paths cause an error.
$ set_version () {
> sed -i.bak "s/using mdx .../using mdx $1/" dune-project
> }
$ cat > dune-project << EOF
> (lang dune 3.2)
> (using mdx ---)
> EOF
$ set_version 0.2
$ cat > dune << EOF
> (mdx)
> EOF
$ cat > README.md << 'EOF'
> ```ocaml file=/etc/passwd
> ```
> EOF
$ dune runtest
File "dune", line 1, characters 0-5:
1 | (mdx)
^^^^^
Error: Paths referenced in mdx files must be relative. This stanza refers to
the following absolute path:
Source path: README.md
Included path: /etc/passwd
[1]
Relative paths that go over the root cause an error.
$ cat > README.md << 'EOF'
> ```ocaml file=../x
> ```
> EOF
$ dune runtest
File "dune", line 1, characters 0-5:
1 | (mdx)
^^^^^
Error: Paths referenced in mdx files must stay within the workspace. This
stanza refers to the following path which escapes:
Source path: README.md
Included path: ../x
[1]
Relative paths within the workspace do not work.
$ mkdir a b
$ mv dune a/
$ rm README.md
$ cat > b/src.ml << EOF
> let x = 1
> EOF
$ cat > a/README.md << 'EOF'
> ```ocaml file=../b/src.ml
> ```
> EOF
$ dune runtest
File "a/dune", line 1, characters 0-5:
1 | (mdx)
^^^^^
Error: Paths referenced in mdx files cannot escape the directory. This stanza
refers to the following path which escapes:
Source path: a/README.md
Included path: ../b/src.ml
[1]
But this works with stanza 0.3:
$ set_version 0.3
$ dune runtest
File "a/README.md", line 1, characters 0-0:
Error: Files _build/default/a/README.md and
_build/default/a/.mdx/README.md.corrected differ.
[1]
$ set_version 0.2
Files in the same directory work.
$ mv b/src.ml a/
$ cat > a/README.md << 'EOF'
> ```ocaml file=src.ml
> ```
> EOF
$ dune runtest
File "a/README.md", line 1, characters 0-0:
Error: Files _build/default/a/README.md and
_build/default/a/.mdx/README.md.corrected differ.
[1]
From a subdirectory too:
$ mkdir a/sub/
$ mv a/src.ml a/sub/
$ cat > a/README.md << 'EOF'
> ```ocaml file=sub/src.ml
> ```
> EOF
$ dune runtest
File "a/README.md", line 1, characters 0-0:
Error: Files _build/default/a/README.md and
_build/default/a/.mdx/README.md.corrected differ.
[1]

View file

@ -0,0 +1,22 @@
Using the mdx stanza, you can set mdx prelude which are used to evaluate
code in the toplevel environments before checking a document. To do that
you can set the prelude field of the stanza: `(preludes <prelude_file> ...)`.
```ocaml
# x;;
- : int = 1
```
Different environment can have their own prelude. This is set using the
`(env <env_name> <prelude_file>)` syntax for the prelude field.
Here x and why are set in `alt.ml`.
```ocaml env=a
# x + y;;
- : int = 11
```
```ocaml env=b
# x + y;;
- : int = 21
```

View file

@ -0,0 +1,6 @@
(mdx
(files README.md)
(preludes
default.ml
(env a a.ml)
(env b b.ml)))

View file

@ -0,0 +1,3 @@
(lang dune 2.4)
(using mdx 0.1)

View file

@ -0,0 +1,3 @@
You can set MDX preludes using the preludes field of the stanza
$ dune runtest

View file

@ -0,0 +1,41 @@
mdx supports dependencies referencing shared libraries.
See #10582.
$ cat > dune-project << EOF
> (lang dune 3.15)
> (name dune_mdx_test)
> (using mdx 0.4)
> EOF
$ cat > dune << EOF
> (library
> (name public_lib)
> (foreign_archives test))
>
> (rule
> (deps test.c)
> (targets libtest.a dlltest.so)
> (action
> (progn
> (run gcc -c -fPIC test.c -o test.o)
> (run gcc test.o -shared -o dlltest.so)
> (run ar rcs libtest.a test.o))))
>
> (mdx
> (libraries public_lib))
> EOF
$ touch README.md
$ cat > public_lib.ml << EOF
> let foo bar = bar + 1
> EOF
$ cat > test.c << EOF
> int add(int a, int b) {
> return a + b;
> }
> EOF
$ dune runtest

View file

@ -0,0 +1,14 @@
$ cat >dune-project <<EOF
> (lang dune 2.4)
> EOF
To use the mdx stanza you need to explicitly set (using mdx ..) in the
dune-project
$ dune build @install
File "dune", line 1, characters 0-5:
1 | (mdx)
^^^^^
Error: 'mdx' is available only when mdx is enabled in the dune-project file.
You must enable it using (using mdx 0.1) in your dune-project file.
[1]