Martinsos
Martinsos

Reputation: 1693

How can I specify source files for Cabal/Stack package with more control than just hs-source-dirs/source-dirs?

I have a Haskell project that is built using Stack (therefore Cabal). Right now I have src/ directory and tests/ directory, however I would like to mix tests together with the source files, which means everything would go to src/ directory.

For that, I would need to define my tests build-info source files to be files in src/ that have .test.hs extension. However, it seems that only choice for defining source files is source-dirs in stack or hs-source-dirs in cabal, meaning that I have to put src as source-dirs, which seems wrong because it is also capturing the normal source files then.

This is what part of my package.yaml:

tests:
  myapp-test:
    main:                Main.hs
    source-dirs:         test
    ...

While I would like it to be smth like:

tests:
  myapp-test:
    main:                Main.hs
    source-files:         src/**/*.test.hs
    ...

Is there any option like that, like source-files, or any other way to do this? Thanks!

Upvotes: 3

Views: 1927

Answers (1)

Roland Puntaier
Roland Puntaier

Reputation: 3501

Separate src and tests dir is convention, but not a must. hs-source-dirs defaults to ..

Cabal finds all source files by itself. Otherwise there is extra-source-files.

Your choice is on the module level: exposed-modules, other-modules. You can use them for libraries and executables.

In your .cabal file:

  • Use one executable <name> entry per executable. main-is is the file with module Main where ....
  • Use one library <name> per library.
  • Use one test-suite <name> entry per test app.

    • type is required (e.g. type: exitcode-stdio-1.0).
    • main-is is the test file.
    • ghc-options: -main-is <your.hs>
    • in the test files have also a
      main :: IO ()
      main = hspec spec -- or: spec1 >> spec2
    

Cabal usage:

  cabal new-build
  cabal new-test [<which test>]
  cabal new-run <file without path>

Deep in dist-newstyle you will find

  • the executables under an x directory
  • the test executables under a t directory
  • the library .so files under a l directory

Links:

Upvotes: 1

Related Questions