Dune shortcut for specifying a dependency between modules in 'src' and 'test'

I am using Dune to build a binary (not a library) whose sources all reside under the src directory. Under the directory test I have a unit test executable which depends on a module src/foo.ml. I managed to get this working by adding a custom rule that copies src/foo.ml, as shown by the file test/jbuild:

(jbuild_version 1)

(executable(
    (name test)
    (libraries (alcotest))
    ))

(rule(
    (targets (foo.ml))
    (deps (../src/foo.ml))
    (action (copy ${<} ${@}))
    ))

(alias(
    (name runtest)
    (deps (test.exe))
    (action (run ${<}))
    ))

This works, but I don’t find it very satisfying. So here’s my question: is there a more elegant way to tell Dune that any modules it can’t find under test should be searched for under src?

Thanks in advance!

In my opinion it would be better to just put the modules as a library, and use another directory to put the executable that depends on the library via some entry point module. That way your test could also depend on the library, preventing you from jumping hoops to make the modules available.

Is there any reason why you can’t depends on the library defined in src? Are you trying to test a private module?

The files in src build an executable program, not a library. So in a sense, src/foo.ml is “private”…

In my opinion it would be better to just put the modules as a library, and use another directory to put the executable that depends on the library via some entry point module. That way your test could also depend on the library, preventing you from jumping hoops to make the modules available.

The modules in src are all part of an executable, and though they could of course be “forced” into a library, I reckon the end result would be confusing… :wink:

1 Like

I then would create a private library for testing purposes. Something like that.

In src/

(library
  ((name foo)
   (wrapped false)
   (modules (:standard \ main))
   ....)))

(executable
  (name main)
  (public_name <usual stuff>)
  (package <usual-stuff)
  (modules (main))
  (libraries (foo)))))

And you can use foo in your tests.

1 Like

OK, thanks for the suggestion! I reckon the best course of action may be indeed to create a private library and add a comment to jbuild explaining that the library exists solely for testing purposes…