Run (preprocess ..) conditionally in dune

I’m building a library that contains inline tests with ppx_expect, and I’d like to be able to run ppx_expect only when dune runtest is called.

The reason is that I’ve defined ppx-expect as a with-test dependency in my opam file, and I don’t want users to have to install ppx_expect (and all its dependencies) just to use the library. It’s defined in my opam file like:

depends: [
  "dune" {>= "3.4"}
  "yojson"
  "containers"
  "fmt"
  "ppx_deriving"
  "ppx_expect" {with-test}

I’m including ppx_expect it in my dune file, and I wonder if its possible to use a conditional so that ppx_expect isn’t needed outside of the dune runtest alias.

I’ve tried the following, but dune gives a Error: unknown constructor if error:

(library (name my_library)
  (libraries fmt)
  (inline_tests)
  (preprocess (if (= %{inline_tests} enabled) (pps ppx_expect)))
)
1 Like

There’s hopefully a more direct way to do what you want. But when I’ve been in this situation in the past I only add ppx_expect as a library dependency to my tests, and not to my main project. I’m still able to test everything I need, and I don’t pollute the main project deps. The cost is that the expect tests don’t live right alongside the implementation, but I’ve not found that to be a problem.

1 Like

Ppx_expext is required event if you don’t plan to run the test. You need a ppx to transform the extension point let%expect otherwise the compiler with fail on them.

Thanks - I’ll avoid the recommendations to put it into with-test and just include it as a standard dependency