Optional dune dependencies on packages

I have a dune Cram test which optionally depends on a package foo, so that when foo gets updated, the test re-runs:

(cram
  (deps (package foo)
)

It does not strictly require foo to be installed. However, written as it is, if foo is not available, dune runtest will fail with:

Error: Package foo does not exist

How can I tell Dune to just ignore the dependency if the package does not exist?

If I use (enabled_if %{lib-available:foo}), the test will be disabled in its absence, which is not what I want.

I believe I could duplicate the test, by adding:

(cram
  (enabled_if %{lib-available:foo})
  (deps bar baz (package foo))
)

(cram
  (enabled_if (not %{lib-available:foo})
  (deps bar baz)
)

But that would double the size of my dune file, so I’d prefer a more elegant solution…

If it’s the same package, then you could have two cram stanzas for a test. Something like

(cram (enabled_if %{lib-available:foo}) (deps (package foo)))

(cram
 (deps bar baz))

The point is that you can have multiple cram stanzas which affect the same cram test. The various stanzas will get folded together.